Skip to content

Commit

Permalink
Merge pull request masylum#15 from rdiazv/create-model-attributes
Browse files Browse the repository at this point in the history
Fixes model creation attributes
  • Loading branch information
masylum authored Jun 29, 2017
2 parents c197efe + 9a1d10c commit 8340c62
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions __tests__/Model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,45 @@ describe('Model', () => {
model.save(item)
expect(collection.create).toBeCalledWith(model, { optimistic: true })
})

it('sends merged attributes on the request', () => {
const adapter = apiClient()
const spy = jest.spyOn(adapter, 'post')

model.save({ name })

expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][1]).toEqual({
name: 'dylan',
album: 'kind of blue'
})

spy.mockReset()
spy.mockRestore()
})
})

describe('and it does not have a collection', () => {
beforeEach(() => {
model.collection = null
})

it('sends merged attributes on the request', () => {
const adapter = apiClient()
const spy = jest.spyOn(adapter, 'post')

model.save({ name })

expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][1]).toEqual({
name: 'dylan',
album: 'kind of blue'
})

spy.mockReset()
spy.mockRestore()
})

describe('if its optimistic (default)', () => {
it('it sets model straight away', () => {
model.save({ name })
Expand Down
2 changes: 1 addition & 1 deletion src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default class Model {
if (this.collection) {
return this.collection.create(this, { optimistic })
} else {
return this._create(attributes, { optimistic })
return this._create(this.attributes.toJS(), { optimistic })
}
}

Expand Down

0 comments on commit 8340c62

Please sign in to comment.