From 096ea0f78c6208cea16aa432998e4440b4d9ac72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Sola=CC=81?= Date: Thu, 6 Apr 2017 19:28:38 +0200 Subject: [PATCH] Fix specs, fix Model error handling --- __tests__/Collection.spec.js | 50 +++++++++------------------ __tests__/Model.spec.js | 67 +++++++++++++++++++++++++++++++++--- src/Model.js | 5 +++ 3 files changed, 85 insertions(+), 37 deletions(-) diff --git a/__tests__/Collection.spec.js b/__tests__/Collection.spec.js index 05b7b43..bbc0705 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', () => { @@ -123,16 +127,6 @@ describe('Collection', () => { } }) - it('clears the error', async () => { - reject() - try { - await collection.fetch() - } catch (e) {} - resolve([])() - await collection.fetch() - expect(collection.error).toBe(null) - }) - it('removes the model', async () => { try { await collection.create(newItem) @@ -156,7 +150,12 @@ describe('Collection', () => { it('nullifies the request', async () => { await collection.create(newItem) expect(collection.models.length).toBe(2) - expect(collection.at(1).request).toBe(null) + expect(collection.request).toBe(null) + }) + + it('clears the error', async () => { + await collection.create(newItem) + expect(collection.error).toBe(null) }) }) }) @@ -173,15 +172,6 @@ describe('Collection', () => { expect(collection.error.body).toBe(error) } }) - it('clears the error', async () => { - reject() - try { - await collection.fetch() - } catch (e) {} - resolve([])() - await collection.fetch() - expect(collection.error).toBe(null) - }) }) describe('when it succeeds', () => { @@ -222,6 +212,7 @@ describe('Collection', () => { describe('when it succeeds', () => { beforeEach(() => { + collection.error = errorObject resolve([item, { id: 2, name: 'bob' }])() }) @@ -232,10 +223,6 @@ describe('Collection', () => { }) it('clears the error', async () => { - try { - await collection.fetch() - } catch (e) {} - resolve([item, { id: 2, name: 'bob' }])() await collection.fetch() expect(collection.error).toBe(null) }) @@ -319,21 +306,13 @@ describe('Collection', () => { expect(collection.error.body).toBe(error) } }) - - it('clears the error', async () => { - try { - await collection.fetch() - } catch (e) {} - resolve([])() - await collection.fetch() - expect(collection.error).toBe(null) - }) }) describe('when it succeeds', () => { const mockResponse = [item, { id: 2, name: 'bob' }] beforeEach(() => { + collection.error = errorObject resolve(mockResponse)() }) @@ -341,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/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 }