Skip to content

Commit

Permalink
Merge pull request masylum#16 from rdiazv/observable-array-attributes
Browse files Browse the repository at this point in the history
Save nested observable objects
  • Loading branch information
masylum authored Jul 5, 2017
2 parents 95b6198 + 76e9f48 commit 000874d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 deletions.
93 changes: 79 additions & 14 deletions __tests__/Model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Model', () => {
let collection
let model
let item
let spy

function resolve (attr) {
return () => {
Expand All @@ -39,11 +40,30 @@ describe('Model', () => {
}

beforeEach(() => {
item = { id: 1, name: 'miles', album: 'kind of blue' }
item = {
id: 1,
name: 'miles',
album: 'kind of blue',
tracks: [
{ name: 'So What' },
{ name: 'Freddie Freeloader' },
{ name: 'Blue in Green' },
{ name: 'All Blues' },
{ name: 'Flamenco Sketches' }
]
}
collection = new MyCollection([item])
model = collection.at(0)
})

afterEach(() => {
if (spy) {
spy.mockReset()
spy.mockRestore()
spy = null
}
})

describe('isRequest', () => {
it('returns false if there is no request', () => {
const newModel = new MyModel({})
Expand Down Expand Up @@ -143,18 +163,18 @@ describe('Model', () => {

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

delete attributes.id

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'
...attributes,
name: 'dylan'
})

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

Expand All @@ -165,18 +185,18 @@ describe('Model', () => {

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

delete attributes.id

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'
...attributes,
name: 'dylan'
})

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

describe('if its optimistic (default)', () => {
Expand Down Expand Up @@ -282,6 +302,28 @@ describe('Model', () => {
expect(model.get('album')).toBe(item.album)
expect(model.request.label).toBe('updating')
})

it('sends merged attributes on the request', () => {
const adapter = apiClient()

spy = jest.spyOn(adapter, 'put')
model.save({
name,
tracks: [
{ name: 'Track 1' },
{ name: 'Track 2' }
]
})

expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][1]).toEqual({
name: 'dylan',
tracks: [
{ name: 'Track 1' },
{ name: 'Track 2' }
]
})
})
})

describe('and its not patching', () => {
Expand All @@ -291,6 +333,29 @@ describe('Model', () => {
expect(model.get('album')).toBe('kind of blue')
expect(model.request.label).toBe('updating')
})

it('sends merged attributes on the request', () => {
const adapter = apiClient()

spy = jest.spyOn(adapter, 'put')
model.save({
name,
tracks: [
{ name: 'Track 1' },
{ name: 'Track 2' }
]
}, { patch: false })

expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][1]).toEqual({
...item,
name: 'dylan',
tracks: [
{ name: 'Track 1' },
{ name: 'Track 2' }
]
})
})
})

describe('when it fails', () => {
Expand Down Expand Up @@ -417,7 +482,7 @@ describe('Model', () => {
it('rolls back the changes', () => {
return model.destroy().catch(() => {
expect(collection.models.length).toBe(1)
expect(collection.at(0).get('name')).toBe(item.name)
expect(collection.at(0).toJS()).toEqual(item)
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export default class Collection<T: Model> {
): Promise<*> {
let model
let attributes = attributesOrModel instanceof Model
? attributesOrModel.attributes.toJS()
? attributesOrModel.toJS()
: attributesOrModel
const label: Label = 'creating'

Expand Down
4 changes: 2 additions & 2 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ export default class Model {
if (this.collection) {
return this.collection.create(this, { optimistic })
} else {
return this._create(this.attributes.toJS(), { optimistic })
return this._create(this.toJS(), { optimistic })
}
}

let newAttributes
let data
const label: Label = 'updating'
const originalAttributes = this.attributes.toJS()
const originalAttributes = this.toJS()

if (patch) {
newAttributes = Object.assign({}, originalAttributes, attributes)
Expand Down

0 comments on commit 000874d

Please sign in to comment.