Skip to content

Commit

Permalink
Add isRequest to model too
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Mar 19, 2017
1 parent 3c41210 commit ba0c226
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
21 changes: 20 additions & 1 deletion __tests__/Model.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, Model, apiClient } from '../src'
import { Collection, Model, apiClient, Request } from '../src'
import MockApi from './mocks/api'

const error = 'boom!'
Expand Down Expand Up @@ -41,6 +41,25 @@ describe('Model', () => {
model = collection.at(0)
})

describe('isRequest', () => {
it('returns false if there is no request', () => {
const newModel = new MyModel({})
expect(newModel.isRequest('fetching')).toBe(false)
})

it('return false if the request is something different', () => {
const newModel = new MyModel({})
newModel.request = new Request('creating', null, 0)
expect(newModel.isRequest('fetching')).toBe(false)
})

it('return true if the request is matching', () => {
const newModel = new MyModel({})
newModel.request = new Request('fetching', null, 0)
expect(newModel.isRequest('fetching')).toBe(true)
})
})

describe('isNew', () => {
it('returns true if it does not have an id', () => {
const newModel = new MyModel({})
Expand Down
10 changes: 10 additions & 0 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ export default class Model {
}
}

/**
* Questions whether the request exists
* and matches a certain label
*/
isRequest (label: Label): boolean {
if (!this.request) return false

return this.request.label === label
}

/**
* Wether the resource is new or not
*
Expand Down

0 comments on commit ba0c226

Please sign in to comment.