From 8c07fdf6200b5a566bed7a1666f7500f798548f5 Mon Sep 17 00:00:00 2001 From: Joe Torreggiani Date: Tue, 14 Apr 2020 14:31:20 -0400 Subject: [PATCH 1/2] Fix create or update snap persistence issue --- dist/mobx-async-store.cjs.js | 4 +++- dist/mobx-async-store.esm.js | 4 +++- spec/Store.spec.js | 41 +++++++++++++++++++++++------------- src/Store.js | 2 +- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/dist/mobx-async-store.cjs.js b/dist/mobx-async-store.cjs.js index 49bec6b6..16d92194 100644 --- a/dist/mobx-async-store.cjs.js +++ b/dist/mobx-async-store.cjs.js @@ -1816,7 +1816,9 @@ function () { }); } - record.setPreviousSnapshot(); + record._takeSnapshot({ + persisted: true + }); } else { record = this.createModel(type, id, { attributes: attributes, diff --git a/dist/mobx-async-store.esm.js b/dist/mobx-async-store.esm.js index d7da4d7a..2b9ef2a4 100644 --- a/dist/mobx-async-store.esm.js +++ b/dist/mobx-async-store.esm.js @@ -1810,7 +1810,9 @@ function () { }); } - record.setPreviousSnapshot(); + record._takeSnapshot({ + persisted: true + }); } else { record = this.createModel(type, id, { attributes: attributes, diff --git a/spec/Store.spec.js b/spec/Store.spec.js index 97d7d835..bf1429a6 100644 --- a/spec/Store.spec.js +++ b/spec/Store.spec.js @@ -73,19 +73,7 @@ const mockTodoData = { } const mockTodoResponse = JSON.stringify(mockTodoData) - -const mockTodosResponse = JSON.stringify({ - data: [ - { - id: '1', - type: 'todos', - attributes: { - id: 1, - title: 'Do taxes' - } - } - ] -}) +const mockTodosResponse = JSON.stringify({ data: [mockTodoData.data] }) describe('Store', () => { beforeEach(() => { @@ -324,6 +312,21 @@ describe('Store', () => { const cache = toJS(store.data.todos.cache) expect(cache['/example_api/todos']).toEqual(['1']) }) + + it('fetched data snapshots are marked as persisted', async () => { + expect.assertions(1) + fetch.mockResponse(mockTodosResponse) + + // Create an existing todo + store.add('todos', { + id: mockTodoData.data.id, + attributes: mockTodoData.data.attributes + }) + + const todos = await store.findAll('todos', { fromServer: true }) + + expect(todos[0].previousSnapshot.persisted).toBeTruthy() + }) }) describe('when "fromServer" is not explicitly set', () => { @@ -479,19 +482,27 @@ describe('Store', () => { }) describe('createOrUpdateModel', () => { - it('sets previous snapshot', () => { + let record + + beforeEach(() => { store.add('notes', { id: 3, text: 'hi' }) - const record = store.createOrUpdateModel({ + record = store.createOrUpdateModel({ id: 3, type: 'notes', attributes: { text: 'yo' } }) + }) + it('sets previous snapshot', () => { expect(record.previousSnapshot.attributes.text).toEqual('yo') }) + + it('sets previous snapshot as persisted', () => { + expect(record.previousSnapshot.persisted).toBeTruthy() + }) }) describe('createModelsFromData', () => { diff --git a/src/Store.js b/src/Store.js index 49d5616f..6cf2184e 100644 --- a/src/Store.js +++ b/src/Store.js @@ -541,7 +541,7 @@ class Store { }) } - record.setPreviousSnapshot() + record._takeSnapshot({ persisted: true }) } else { record = this.createModel(type, id, { attributes, relationships }) this.data[type].records[record.id] = record From 6af78177a279c7f0a11ae370ced32c7f61e32063 Mon Sep 17 00:00:00 2001 From: Joe Torreggiani Date: Wed, 15 Apr 2020 09:45:40 -0400 Subject: [PATCH 2/2] Bump version in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 42949349..fa53e418 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@artemisag/mobx-async-store", - "version": "1.0.20", + "version": "1.0.21", "module": "dist/mobx-async-store.esm.js", "browser": "dist/mobx-async-store.cjs.js", "main": "dist/mobx-async-store.cjs.js",