diff --git a/__tests__/Collection.spec.js b/__tests__/Collection.spec.js index 6f1f985..a4c16f1 100644 --- a/__tests__/Collection.spec.js +++ b/__tests__/Collection.spec.js @@ -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 { @@ -27,6 +30,7 @@ describe('Collection', () => { beforeEach(() => { item = { id: 1, name: 'miles' } collection = new MyCollection([item]) + collection.error = errorObject }) describe('at', () => { @@ -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) + }) }) }) @@ -203,6 +212,7 @@ describe('Collection', () => { describe('when it succeeds', () => { beforeEach(() => { + collection.error = errorObject resolve([item, { id: 2, name: 'bob' }])() }) @@ -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) + }) }) }) @@ -297,6 +312,7 @@ describe('Collection', () => { const mockResponse = [item, { id: 2, name: 'bob' }] beforeEach(() => { + collection.error = errorObject resolve(mockResponse)() }) @@ -304,6 +320,11 @@ describe('Collection', () => { const data = await collection.rpc('foo') expect(data).toBe(mockResponse) }) + + it('clears the error', async () => { + await collection.rpc('foo') + expect(collection.error).toBe(null) + }) }) }) }) diff --git a/__tests__/Model.spec.js b/__tests__/Model.spec.js index 13be4ea..fb483de 100644 --- a/__tests__/Model.spec.js +++ b/__tests__/Model.spec.js @@ -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 { @@ -171,6 +174,7 @@ describe('Model', () => { describe('when it succeeds', () => { beforeEach(() => { + model.error = errorObject resolve({ id: 1, name: 'coltrane' })() }) @@ -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) + }) }) }) @@ -208,6 +217,7 @@ describe('Model', () => { describe('when it succeeds', () => { beforeEach(() => { + model.error = errorObject resolve({ id: 2, name: 'dylan' })() }) @@ -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) + }) }) }) }) @@ -273,6 +288,7 @@ describe('Model', () => { describe('when it succeeds', () => { beforeEach(() => { + model.error = errorObject resolve({ id: 1, name: 'coltrane' })() }) @@ -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) + }) }) }) @@ -310,6 +331,7 @@ describe('Model', () => { describe('when it succeeds', () => { beforeEach(() => { + model.error = errorObject resolve({ id: 2, name: 'dylan' })() }) @@ -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) + }) }) }) }) @@ -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) + }) }) }) @@ -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(() => { @@ -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) + }) }) }) }) @@ -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) => { @@ -463,6 +509,11 @@ describe('Model', () => { expect(model.request).toBe(null) }) }) + + it('clears the error', async () => { + await model.fetch() + expect(model.error).toBe(null) + }) }) }) @@ -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) => { @@ -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) + }) }) }) }) diff --git a/src/Collection.js b/src/Collection.js index 0d29703..7b7d6a9 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -235,6 +235,7 @@ export default class Collection { this.add([data]) } this.request = null + this.error = null }) return data @@ -269,6 +270,7 @@ export default class Collection { runInAction('fetch-done', () => { this.set(data, options) this.request = null + this.error = null }) return data @@ -302,6 +304,7 @@ export default class Collection { } this.request = null + this.error = null return response } diff --git a/src/Model.js b/src/Model.js index 21b6386..5cf4daa 100644 --- a/src/Model.js +++ b/src/Model.js @@ -174,6 +174,7 @@ export default class Model { runInAction('fetch-done', () => { this.set(data) this.request = null + this.error = null }) return data @@ -249,6 +250,7 @@ export default class Model { runInAction('save-done', () => { this.request = null + this.error = null this.set(response) }) @@ -298,6 +300,7 @@ export default class Model { runInAction('create-done', () => { this.set(data) this.request = null + this.error = null }) return data @@ -344,6 +347,7 @@ export default class Model { this.collection.remove([this.id]) } this.request = null + this.error = null }) return null @@ -377,6 +381,7 @@ export default class Model { } this.request = null + this.error = null return response }