Skip to content

Commit 9e79d2c

Browse files
committed
test: Add tests for state reference equality
1 parent 6d96861 commit 9e79d2c

File tree

1 file changed

+58
-44
lines changed

1 file changed

+58
-44
lines changed

packages/cozy-client/src/store/documents.spec.js

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import {
66
describe('extractAndMerge', () => {
77
const data = [
88
{
9-
id: 'b6ff135b34e041ffb2d4a4865f3e0a53',
109
_id: 'b6ff135b34e041ffb2d4a4865f3e0a53',
11-
type: 'io.cozy.files',
1210
_type: 'io.cozy.files',
1311
blabla: 'new field',
1412
cozyMetadata: {
@@ -19,32 +17,26 @@ describe('extractAndMerge', () => {
1917
}
2018
},
2119
{
22-
id: 'b6ff135b34e041ffb2d4a4865f3e235f',
23-
type: 'io.cozy.files',
24-
_type: 'io.cozy.files',
2520
_id: 'b6ff135b34e041ffb2d4a4865f3e235f',
21+
_type: 'io.cozy.files',
2622
blibli: 'another new field'
2723
}
2824
]
2925
const updatedStateWithIncluded = {
3026
'io.cozy.files': {
3127
b6ff135b34e041ffb2d4a4865f3e0a53: {
32-
attributes: {
33-
type: 'file',
34-
name: 'IMG_0016.PNG',
35-
dir_id: '7d2f9c24cd345ce3171bf71f401e80c6'
36-
},
37-
id: 'b6ff135b34e041ffb2d4a4865f3e0a53',
3828
_id: 'b6ff135b34e041ffb2d4a4865f3e0a53',
29+
_type: 'io.cozy.files',
30+
name: 'IMG_0016.PNG',
31+
type: 'file',
32+
dir_id: '7d2f9c24cd345ce3171bf71f401e80c6',
3933
links: { self: '/files/b6ff135b34e041ffb2d4a4865f3e0a53' },
4034
meta: { rev: '5-87840eceaab358e38aa8b6bb0d4577b1' },
4135
relationships: {
4236
parent: {
4337
links: { related: '/files/7d2f9c24cd345ce3171bf71f401e80c6' }
4438
}
4539
},
46-
type: 'io.cozy.files',
47-
_type: 'io.cozy.files',
4840
cozyMetadata: {
4941
updated_at: '987654',
5042
updatedByApps: {
@@ -53,22 +45,18 @@ describe('extractAndMerge', () => {
5345
}
5446
},
5547
b6ff135b34e041ffb2d4a4865f3e235f: {
56-
attributes: {
57-
type: 'file',
58-
name: 'IMG_0054.PNG',
59-
dir_id: '7d2f9c24cd345ce3171bf71f401e80c6'
60-
},
61-
id: 'b6ff135b34e041ffb2d4a4865f3e235f',
6248
_id: 'b6ff135b34e041ffb2d4a4865f3e235f',
49+
_type: 'io.cozy.files',
50+
type: 'file',
51+
name: 'IMG_0054.PNG',
52+
dir_id: '7d2f9c24cd345ce3171bf71f401e80c6',
6353
links: { self: '/files/b6ff135b34e041ffb2d4a4865f3e235f' },
6454
meta: { rev: '5-ca91c0dc02dafb38eb56070c1d80d62c' },
6555
relationships: {
6656
parent: {
6757
links: { related: '/files/7d2f9c24cd345ce3171bf71f401e80c6' }
6858
}
69-
},
70-
type: 'io.cozy.files',
71-
_type: 'io.cozy.files'
59+
}
7260
}
7361
}
7462
}
@@ -84,11 +72,9 @@ describe('extractAndMerge', () => {
8472
b6ff135b34e041ffb2d4a4865f3e0a53: {
8573
_id: 'b6ff135b34e041ffb2d4a4865f3e0a53',
8674
_type: 'io.cozy.files',
87-
attributes: {
88-
dir_id: '7d2f9c24cd345ce3171bf71f401e80c6',
89-
name: 'IMG_0016.PNG',
90-
type: 'file'
91-
},
75+
dir_id: '7d2f9c24cd345ce3171bf71f401e80c6',
76+
name: 'IMG_0016.PNG',
77+
type: 'file',
9278
blabla: 'new field',
9379
cozyMetadata: {
9480
created_at: '123456',
@@ -98,34 +84,28 @@ describe('extractAndMerge', () => {
9884
date: '2019-06-11T01:02:03Z'
9985
}
10086
},
101-
id: 'b6ff135b34e041ffb2d4a4865f3e0a53',
10287
links: { self: '/files/b6ff135b34e041ffb2d4a4865f3e0a53' },
10388
meta: { rev: '5-87840eceaab358e38aa8b6bb0d4577b1' },
10489
relationships: {
10590
parent: {
10691
links: { related: '/files/7d2f9c24cd345ce3171bf71f401e80c6' }
10792
}
108-
},
109-
type: 'io.cozy.files'
93+
}
11094
},
11195
b6ff135b34e041ffb2d4a4865f3e235f: {
11296
_id: 'b6ff135b34e041ffb2d4a4865f3e235f',
11397
_type: 'io.cozy.files',
114-
attributes: {
115-
dir_id: '7d2f9c24cd345ce3171bf71f401e80c6',
116-
name: 'IMG_0054.PNG',
117-
type: 'file'
118-
},
98+
dir_id: '7d2f9c24cd345ce3171bf71f401e80c6',
99+
name: 'IMG_0054.PNG',
100+
type: 'file',
119101
blibli: 'another new field',
120-
id: 'b6ff135b34e041ffb2d4a4865f3e235f',
121102
links: { self: '/files/b6ff135b34e041ffb2d4a4865f3e235f' },
122103
meta: { rev: '5-ca91c0dc02dafb38eb56070c1d80d62c' },
123104
relationships: {
124105
parent: {
125106
links: { related: '/files/7d2f9c24cd345ce3171bf71f401e80c6' }
126107
}
127-
},
128-
type: 'io.cozy.files'
108+
}
129109
}
130110
}
131111
}
@@ -173,16 +153,12 @@ describe('extractAndMerge', () => {
173153
() => {
174154
const dataAlreadyIncludedInUpdatedStateWithIncluded = [
175155
{
176-
id: 'b6ff135b34e041ffb2d4a4865f3e0a53',
177156
_id: 'b6ff135b34e041ffb2d4a4865f3e0a53',
178-
type: 'io.cozy.files',
179157
_type: 'io.cozy.files'
180158
},
181159
{
182-
id: 'b6ff135b34e041ffb2d4a4865f3e235f',
183-
type: 'io.cozy.files',
184-
_type: 'io.cozy.files',
185-
_id: 'b6ff135b34e041ffb2d4a4865f3e235f'
160+
_id: 'b6ff135b34e041ffb2d4a4865f3e235f',
161+
_type: 'io.cozy.files'
186162
}
187163
]
188164

@@ -202,6 +178,44 @@ describe('extractAndMerge', () => {
202178
)
203179
}
204180
)
181+
it('should keep state reference in case no document has changed', () => {
182+
const newDoc = { ...data[0] }
183+
184+
const mergedState = extractAndMergeDocument(
185+
[newDoc],
186+
updatedStateWithIncluded
187+
)
188+
expect(mergedState === updatedStateWithIncluded)
189+
})
190+
191+
it('should have different state reference if a new document is added', () => {
192+
const newDoc = {
193+
_id: '1234',
194+
_type: 'io.cozy.files',
195+
name: 'New doc'
196+
}
197+
const mergedState = extractAndMergeDocument(
198+
[newDoc],
199+
updatedStateWithIncluded
200+
)
201+
expect(mergedState !== updatedStateWithIncluded)
202+
})
203+
204+
it('should have different state reference if an existing document have changed', () => {
205+
const newDoc = {
206+
...data[0],
207+
cozyMetadata: {
208+
...data[0].cozyMetadata,
209+
updatedByApps: '2025-01-01'
210+
}
211+
}
212+
213+
const mergedState = extractAndMergeDocument(
214+
[newDoc],
215+
updatedStateWithIncluded
216+
)
217+
expect(mergedState !== updatedStateWithIncluded)
218+
})
205219
})
206220

207221
describe('mergeDocumentsWithRelationships', () => {

0 commit comments

Comments
 (0)