From 282286d45fefaee416154e08aad6c0185bf7c13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Andre=CC=81s=20Di=CC=81az=20Vergara?= Date: Fri, 30 Jun 2017 16:37:41 -0400 Subject: [PATCH 1/5] Resets the spy even if the test fails --- __tests__/Model.spec.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/__tests__/Model.spec.js b/__tests__/Model.spec.js index 0656043..8c8354e 100644 --- a/__tests__/Model.spec.js +++ b/__tests__/Model.spec.js @@ -27,6 +27,7 @@ describe('Model', () => { let collection let model let item + let spy function resolve (attr) { return () => { @@ -44,6 +45,14 @@ describe('Model', () => { 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({}) @@ -143,8 +152,8 @@ describe('Model', () => { it('sends merged attributes on the request', () => { const adapter = apiClient() - const spy = jest.spyOn(adapter, 'post') + spy = jest.spyOn(adapter, 'post') model.save({ name }) expect(spy).toHaveBeenCalledTimes(1) @@ -152,9 +161,6 @@ describe('Model', () => { name: 'dylan', album: 'kind of blue' }) - - spy.mockReset() - spy.mockRestore() }) }) @@ -165,8 +171,8 @@ describe('Model', () => { it('sends merged attributes on the request', () => { const adapter = apiClient() - const spy = jest.spyOn(adapter, 'post') + spy = jest.spyOn(adapter, 'post') model.save({ name }) expect(spy).toHaveBeenCalledTimes(1) @@ -174,9 +180,6 @@ describe('Model', () => { name: 'dylan', album: 'kind of blue' }) - - spy.mockReset() - spy.mockRestore() }) describe('if its optimistic (default)', () => { From ece3b14c8b09da0a08f1eef715ff31ad1c4c03cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Andre=CC=81s=20Di=CC=81az=20Vergara?= Date: Fri, 30 Jun 2017 16:43:16 -0400 Subject: [PATCH 2/5] Adds a nested array to the model attributes to raise an error --- __tests__/Model.spec.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/__tests__/Model.spec.js b/__tests__/Model.spec.js index 8c8354e..3d8c00c 100644 --- a/__tests__/Model.spec.js +++ b/__tests__/Model.spec.js @@ -40,7 +40,18 @@ 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) }) @@ -152,14 +163,17 @@ describe('Model', () => { it('sends merged attributes on the request', () => { const adapter = apiClient() + 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' }) }) }) @@ -171,14 +185,17 @@ describe('Model', () => { it('sends merged attributes on the request', () => { const adapter = apiClient() + 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' }) }) From cc5e55a98f27a18a6ba099e6d28886608208994e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Andre=CC=81s=20Di=CC=81az=20Vergara?= Date: Fri, 30 Jun 2017 16:45:15 -0400 Subject: [PATCH 3/5] Fixes creation with nested observable attributes --- src/Collection.js | 2 +- src/Model.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Collection.js b/src/Collection.js index 4bdb0bb..450ba73 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -210,7 +210,7 @@ export default class Collection { ): Promise<*> { let model let attributes = attributesOrModel instanceof Model - ? attributesOrModel.attributes.toJS() + ? attributesOrModel.toJS() : attributesOrModel const label: Label = 'creating' diff --git a/src/Model.js b/src/Model.js index e0d541b..19c7145 100644 --- a/src/Model.js +++ b/src/Model.js @@ -203,7 +203,7 @@ 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 }) } } From 14d334186e6ba621a3d368e3afdf020bf5a24395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Andre=CC=81s=20Di=CC=81az=20Vergara?= Date: Fri, 30 Jun 2017 17:30:30 -0400 Subject: [PATCH 4/5] Fixes update with nested observable attributes --- __tests__/Model.spec.js | 45 +++++++++++++++++++++++++++++++++++++++++ src/Model.js | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/__tests__/Model.spec.js b/__tests__/Model.spec.js index 3d8c00c..4fab61c 100644 --- a/__tests__/Model.spec.js +++ b/__tests__/Model.spec.js @@ -302,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', () => { @@ -311,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', () => { diff --git a/src/Model.js b/src/Model.js index 19c7145..2126abe 100644 --- a/src/Model.js +++ b/src/Model.js @@ -210,7 +210,7 @@ export default class Model { let newAttributes let data const label: Label = 'updating' - const originalAttributes = this.attributes.toJS() + const originalAttributes = this.toJS() if (patch) { newAttributes = Object.assign({}, originalAttributes, attributes) From 76e9f48cb1445eab4e9c03867d9a054130f3d023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Andre=CC=81s=20Di=CC=81az=20Vergara?= Date: Fri, 30 Jun 2017 17:32:38 -0400 Subject: [PATCH 5/5] Check all fields in the destroy rollback test --- __tests__/Model.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/Model.spec.js b/__tests__/Model.spec.js index 4fab61c..3ba7ff2 100644 --- a/__tests__/Model.spec.js +++ b/__tests__/Model.spec.js @@ -482,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) }) })