Skip to content

Commit

Permalink
Fix specs, fix Model error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
p3drosola committed Apr 6, 2017
1 parent e449fa8 commit 096ea0f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 37 deletions.
50 changes: 17 additions & 33 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 @@ -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)
Expand All @@ -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)
})
})
})
Expand All @@ -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', () => {
Expand Down Expand Up @@ -222,6 +212,7 @@ describe('Collection', () => {

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

Expand All @@ -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)
})
Expand Down Expand Up @@ -319,28 +306,25 @@ 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)()
})

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)
})
})
})
})
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 096ea0f

Please sign in to comment.