Skip to content

Commit

Permalink
Merge pull request masylum#9 from p3drosola/clear-errors
Browse files Browse the repository at this point in the history
Clear error after successful request
  • Loading branch information
masylum authored Apr 18, 2017
2 parents 6354eeb + 1e580fa commit bf11d2f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
21 changes: 21 additions & 0 deletions __tests__/Collection.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Collection, apiClient, Request } from '../src'
import MockApi from './mocks/api'
import ErrorObject from '../src/ErrorObject'

const error = 'boom!'
const errorObject = new ErrorObject('fetch', error)

apiClient(MockApi)

class MyCollection extends Collection {
Expand All @@ -27,6 +30,7 @@ describe('Collection', () => {
beforeEach(() => {
item = { id: 1, name: 'miles' }
collection = new MyCollection([item])
collection.error = errorObject
})

describe('at', () => {
Expand Down Expand Up @@ -148,6 +152,11 @@ describe('Collection', () => {
expect(collection.models.length).toBe(2)
expect(collection.at(1).request).toBe(null)
})

it('clears the error', async () => {
await collection.create(newItem)
expect(collection.error).toBe(null)
})
})
})

Expand Down Expand Up @@ -203,6 +212,7 @@ describe('Collection', () => {

describe('when it succeeds', () => {
beforeEach(() => {
collection.error = errorObject
resolve([item, { id: 2, name: 'bob' }])()
})

Expand All @@ -211,6 +221,11 @@ describe('Collection', () => {
expect(collection.models.length).toBe(2)
expect(collection.at(1).get('name')).toBe('bob')
})

it('clears the error', async () => {
await collection.fetch()
expect(collection.error).toBe(null)
})
})
})

Expand Down Expand Up @@ -297,13 +312,19 @@ describe('Collection', () => {
const mockResponse = [item, { id: 2, name: 'bob' }]

beforeEach(() => {
collection.error = errorObject
resolve(mockResponse)()
})

it('return the data', async () => {
const data = await collection.rpc('foo')
expect(data).toBe(mockResponse)
})

it('clears the error', async () => {
await collection.rpc('foo')
expect(collection.error).toBe(null)
})
})
})
})
Expand Down
67 changes: 63 additions & 4 deletions __tests__/Model.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Collection, Model, apiClient, Request } from '../src'
import MockApi from './mocks/api'
import ErrorObject from '../src/ErrorObject'

const error = 'boom!'
const errorObject = new ErrorObject('fetch', error)

apiClient(MockApi)

class MyCollection extends Collection {
Expand Down Expand Up @@ -171,6 +174,7 @@ describe('Model', () => {

describe('when it succeeds', () => {
beforeEach(() => {
model.error = errorObject
resolve({ id: 1, name: 'coltrane' })()
})

Expand All @@ -185,6 +189,11 @@ describe('Model', () => {
expect(model.request).toBe(null)
})
})

it('clears the error', async () => {
await model.save({ name })
expect(model.error).toBe(null)
})
})
})

Expand All @@ -208,6 +217,7 @@ describe('Model', () => {

describe('when it succeeds', () => {
beforeEach(() => {
model.error = errorObject
resolve({ id: 2, name: 'dylan' })()
})

Expand All @@ -222,6 +232,11 @@ describe('Model', () => {
expect(model.request).toBe(null)
})
})

it('clears the error', async () => {
await model.save({ name })
expect(model.error).toBe(null)
})
})
})
})
Expand Down Expand Up @@ -273,6 +288,7 @@ describe('Model', () => {

describe('when it succeeds', () => {
beforeEach(() => {
model.error = errorObject
resolve({ id: 1, name: 'coltrane' })()
})

Expand All @@ -287,6 +303,11 @@ describe('Model', () => {
expect(model.request).toBe(null)
})
})

it('clears the error', async () => {
await model.save({ name })
expect(model.error).toBe(null)
})
})
})

Expand All @@ -310,6 +331,7 @@ describe('Model', () => {

describe('when it succeeds', () => {
beforeEach(() => {
model.error = errorObject
resolve({ id: 2, name: 'dylan' })()
})

Expand All @@ -324,6 +346,11 @@ describe('Model', () => {
expect(model.request).toBe(null)
})
})

it('clears the error', async () => {
await model.save({ name })
expect(model.error).toBe(null)
})
})
})
})
Expand Down Expand Up @@ -373,13 +400,21 @@ describe('Model', () => {
})

describe('when it succeeds', () => {
beforeEach(resolve())
beforeEach(() => {
model.error = errorObject
resolve()()
})

it('nullifies the request', () => {
return model.destroy().then(() => {
expect(model.request).toBe(null)
})
})

it('clears the error', async () => {
await model.save({ name })
expect(model.error).toBe(null)
})
})
})

Expand Down Expand Up @@ -408,7 +443,10 @@ describe('Model', () => {
})

describe('when it succeeds', () => {
beforeEach(resolve())
beforeEach(() => {
model.error = errorObject
resolve()()
})

it('applies changes', () => {
return model.destroy({ optimistic: false }).then(() => {
Expand All @@ -421,6 +459,11 @@ describe('Model', () => {
expect(model.request).toBe(null)
})
})

it('clears the error', async () => {
await model.destroy({ optimistic: false })
expect(model.error).toBe(null)
})
})
})
})
Expand All @@ -444,7 +487,10 @@ describe('Model', () => {
})

describe('when it succeeds', () => {
beforeEach(resolve({ name: 'bill' }))
beforeEach(() => {
model.error = errorObject
resolve({ name: 'bill' })()
})

it('returns the response', () => {
return model.fetch().then((response) => {
Expand All @@ -463,6 +509,11 @@ describe('Model', () => {
expect(model.request).toBe(null)
})
})

it('clears the error', async () => {
await model.fetch()
expect(model.error).toBe(null)
})
})
})

Expand All @@ -485,7 +536,10 @@ describe('Model', () => {
})

describe('when it succeeds', () => {
beforeEach(resolve('foo'))
beforeEach(() => {
model.error = errorObject
resolve('foo')()
})

it('returns the response', () => {
return model.rpc('approve').then((response) => {
Expand All @@ -498,6 +552,11 @@ describe('Model', () => {
expect(model.request).toBe(null)
})
})

it('clears the error', async () => {
await model.rpc('approve')
expect(model.error).toBe(null)
})
})
})
})
3 changes: 3 additions & 0 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export default class Collection<T: Model> {
this.add([data])
}
this.request = null
this.error = null
})

return data
Expand Down Expand Up @@ -269,6 +270,7 @@ export default class Collection<T: Model> {
runInAction('fetch-done', () => {
this.set(data, options)
this.request = null
this.error = null
})

return data
Expand Down Expand Up @@ -302,6 +304,7 @@ export default class Collection<T: Model> {
}

this.request = null
this.error = null

return response
}
Expand Down
5 changes: 5 additions & 0 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export default class Model {
runInAction('fetch-done', () => {
this.set(data)
this.request = null
this.error = null
})

return data
Expand Down Expand Up @@ -249,6 +250,7 @@ export default class Model {

runInAction('save-done', () => {
this.request = null
this.error = null
this.set(response)
})

Expand Down Expand Up @@ -298,6 +300,7 @@ export default class Model {
runInAction('create-done', () => {
this.set(data)
this.request = null
this.error = null
})

return data
Expand Down Expand Up @@ -344,6 +347,7 @@ export default class Model {
this.collection.remove([this.id])
}
this.request = null
this.error = null
})

return null
Expand Down Expand Up @@ -377,6 +381,7 @@ export default class Model {
}

this.request = null
this.error = null

return response
}
Expand Down

0 comments on commit bf11d2f

Please sign in to comment.