Skip to content

Commit b24d582

Browse files
committed
Merge branch '1.x-dev' of https://github.com/jsonapi-suite/jsorm into 1.x-dev
* '1.x-dev' of https://github.com/jsonapi-suite/jsorm: Fix spec (#61) Split isPersisted() and reset()
2 parents ab1229b + 1e170f5 commit b24d582

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

src/model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ export class JSORMBase {
405405
}
406406
set isPersisted(val: boolean) {
407407
this._persisted = val
408+
this.reset()
409+
}
410+
411+
reset() : void {
408412
this._originalAttributes = cloneDeep(this._attributes)
409413
this._originalRelationships = this.relationshipResourceIdentifiers(
410414
Object.keys(this.relationships)

src/util/deserialize.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class Deserializer {
148148

149149
// came from server, must be persisted
150150
instance.isPersisted = true
151+
instance.reset()
151152

152153
return instance
153154
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { sinon, expect, fetchMock } from "../test-helper"
2+
import { Person, Author, Book } from "../fixtures"
3+
import { IResultProxy } from "../../src/proxies/index"
4+
5+
// This is a Vue-specific test. Since isPersisted is already true,
6+
// Vue will prevent the setter from firing. We cannot rely on
7+
// side-effect behavior of model.isPersisted = true
8+
// So, ensure we at least call reset() explicitly
9+
describe("Dirty tracking", () => {
10+
let responsePayload = (firstName: string) => {
11+
return {
12+
data: {
13+
id: "1",
14+
type: "people",
15+
attributes: { firstName }
16+
}
17+
}
18+
}
19+
20+
afterEach(() => {
21+
fetchMock.restore()
22+
})
23+
24+
beforeEach(() => {
25+
let url = "http://example.com/api/v1/authors"
26+
fetchMock.post(url, responsePayload('John'))
27+
fetchMock.put(`${url}/1`, responsePayload('Jake'))
28+
})
29+
30+
describe("when persisted, dirty, updated", () => {
31+
it("calls reset()", async () => {
32+
let instance = new Author({ firstName: 'John' })
33+
await instance.save()
34+
expect(instance.isPersisted).to.eq(true)
35+
expect(instance.isDirty()).to.eq(false)
36+
instance.firstName = 'Jake'
37+
expect(instance.isDirty()).to.eq(true)
38+
let spy = sinon.spy()
39+
instance.reset = spy
40+
await instance.save()
41+
expect(spy.callCount).to.eq(2)
42+
})
43+
})
44+
})

test/unit/model.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,19 @@ describe("Model", () => {
817817
})
818818
})
819819

820+
describe('when previously persisted, dirty, then persisted again', () => {
821+
it('is no longer dirty', () => {
822+
const instance = new Author({ id: 1 })
823+
instance.firstName = "foo"
824+
instance.isPersisted = true
825+
expect(instance.isDirty()).to.eq(false)
826+
instance.firstName = "bar"
827+
expect(instance.isDirty()).to.eq(true)
828+
instance.isPersisted = true
829+
expect(instance.isDirty()).to.eq(false)
830+
})
831+
})
832+
820833
describe("when marked for destruction", () => {
821834
it("is dirty", () => {
822835
const instance = new Author()

0 commit comments

Comments
 (0)