Skip to content

Commit

Permalink
Merge pull request #44 from artemis-ag/fix-create-or-update-snapshot-…
Browse files Browse the repository at this point in the history
…persistence-issue

Fix create or update snapshot persistence issue
  • Loading branch information
Joe Torreggiani authored Apr 15, 2020
2 parents 5ea18c8 + 6af7817 commit 70b66bb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
4 changes: 3 additions & 1 deletion dist/mobx-async-store.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1816,7 +1816,9 @@ function () {
});
}

record.setPreviousSnapshot();
record._takeSnapshot({
persisted: true
});
} else {
record = this.createModel(type, id, {
attributes: attributes,
Expand Down
4 changes: 3 additions & 1 deletion dist/mobx-async-store.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,9 @@ function () {
});
}

record.setPreviousSnapshot();
record._takeSnapshot({
persisted: true
});
} else {
record = this.createModel(type, id, {
attributes: attributes,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
41 changes: 26 additions & 15 deletions spec/Store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 70b66bb

Please sign in to comment.