@@ -6,18 +6,19 @@ afterEach(() => {
6
6
ApplicationRecord . store . data = { }
7
7
} )
8
8
9
+ let responsePayload = ( firstName : string ) => {
10
+ return {
11
+ data : {
12
+ id : "1" ,
13
+ type : "authors" ,
14
+ attributes : { firstName }
15
+ }
16
+ }
17
+ }
18
+
9
19
describe ( "ID Map" , ( ) => {
10
20
beforeEach ( ( ) => {
11
21
ApplicationRecord . sync = true
12
- let responsePayload = ( firstName : string ) => {
13
- return {
14
- data : {
15
- id : "1" ,
16
- type : "authors" ,
17
- attributes : { firstName }
18
- }
19
- }
20
- }
21
22
22
23
fetchMock . get (
23
24
"http://example.com/api/v1/authors/1" ,
@@ -38,7 +39,7 @@ describe("ID Map", () => {
38
39
} )
39
40
40
41
afterEach ( ( ) => {
41
- fetchMock . reset ( )
42
+ fetchMock . restore ( )
42
43
} )
43
44
44
45
describe ( "when fetching from server" , ( ) => {
@@ -61,36 +62,71 @@ describe("ID Map", () => {
61
62
expect ( stored [ "authors-1" ] ) . to . deep . eq ( data . attributes )
62
63
} )
63
64
64
- it ( "syncs with id map" , async ( ) => {
65
+ it ( "syncs non-dirty attributes with id map" , async ( ) => {
65
66
let author1 = ( await Author . find ( 1 ) ) . data
66
- author1 . firstName = "updated"
67
+ expect ( author1 . firstName ) . to . eq ( "John" )
68
+
69
+ fetchMock . restore ( )
70
+ fetchMock . get (
71
+ "http://example.com/api/v1/authors/1" ,
72
+ responsePayload ( "Jane" )
73
+ )
74
+
67
75
let author2 = ( await Author . find ( 1 ) ) . data
76
+ expect ( author2 . firstName ) . to . eq ( "Jane" )
77
+ expect ( author1 . firstName ) . to . eq ( "Jane" )
78
+ } )
79
+
80
+ it ( "does not see mark newly-synced attributes as dirty" , async ( ) => {
81
+ let author1 = ( await Author . find ( 1 ) ) . data
68
82
expect ( author1 . firstName ) . to . eq ( "John" )
83
+
84
+ fetchMock . restore ( )
85
+ fetchMock . get (
86
+ "http://example.com/api/v1/authors/1" ,
87
+ responsePayload ( "Jane" )
88
+ )
89
+
90
+ await Author . find ( 1 )
91
+ expect ( author1 . firstName ) . to . eq ( "Jane" )
92
+ expect ( author1 . isDirty ( ) ) . to . eq ( false )
69
93
} )
70
94
71
- describe ( "when implementing afterSync hook" , ( ) => {
72
- let originalAfterSync : any
95
+ it ( "does not sync dirty attributes with id map" , async ( ) => {
96
+ let author1 = ( await Author . find ( 1 ) ) . data
97
+ author1 . firstName = "updated"
73
98
74
- beforeEach ( ( ) => {
75
- originalAfterSync = Author . prototype . afterSync
76
- Author . prototype . afterSync = sinon . spy ( )
77
- } )
99
+ fetchMock . restore ( )
100
+ fetchMock . get (
101
+ "http://example.com/api/v1/authors/1" ,
102
+ responsePayload ( "Jane" )
103
+ )
78
104
79
- afterEach ( ( ) => {
80
- Author . prototype . afterSync = originalAfterSync
81
- } )
105
+ let author2 = ( await Author . find ( 1 ) ) . data
106
+ expect ( author2 . firstName ) . to . eq ( "Jane" )
107
+ expect ( author1 . firstName ) . to . eq ( "updated" )
108
+ } )
82
109
110
+ describe ( "when implementing afterSync hook" , ( ) => {
83
111
it ( "fires after sync, passing changes as an argument" , async ( ) => {
84
112
let author1 = ( await Author . find ( 1 ) ) . data
85
- author1 . firstName = 'updated'
113
+ author1 . afterSync = sinon . spy ( )
114
+
115
+ fetchMock . restore ( )
116
+ fetchMock . get (
117
+ "http://example.com/api/v1/authors/1" ,
118
+ responsePayload ( "Jane" )
119
+ )
120
+
86
121
let author2 = ( await Author . find ( 1 ) ) . data
87
122
expect ( author1 . afterSync ) . to
88
- . have . been . calledWith ( { firstName : [ "updated " , "John " ] } )
123
+ . have . been . calledWith ( { firstName : [ "John " , "Jane " ] } )
89
124
} )
90
125
91
126
describe ( "when store updates, but no changes" , ( ) => {
92
127
it ( "does not fire the hook" , async ( ) => {
93
128
let author1 = ( await Author . find ( 1 ) ) . data
129
+ author1 . afterSync = sinon . spy ( )
94
130
let author2 = ( await Author . find ( 1 ) ) . data
95
131
let called = ( < any > author1 . afterSync ) . called
96
132
expect ( called ) . to . eq ( false )
0 commit comments