1
- import { expect , test } from '@jest/globals'
1
+ import { expect , jest , test } from '@jest/globals'
2
2
import { type PatchMutationOperation } from '@sanity/types'
3
3
4
4
import { Mutation } from '../src/document/Mutation'
@@ -109,7 +109,39 @@ test('de-duplicate createIfNotExists', () => {
109
109
expect ( tx2 && tx2 . mutations . length ) . toBe ( 1 )
110
110
} )
111
111
112
+ test . each ( [ 'create' , 'createIfNotExists' , 'createOrReplace' ] ) (
113
+ '%s defaults to current created at time' ,
114
+ ( createFnc ) => {
115
+ const globalMockDate = new Date ( '2020-01-01T12:34:55.000Z' )
116
+ const globalDateSpy = jest . spyOn ( global , 'Date' ) . mockReturnValue ( globalMockDate )
117
+
118
+ const sb = new SquashingBuffer ( null )
119
+
120
+ add ( sb , { [ createFnc ] : { _id : '1' , _type : 'test' , a : 'A string value' } } )
121
+
122
+ const tx = sb . purge ( 'txn_id' )
123
+ if ( ! tx ) {
124
+ throw new Error ( 'buffer purge did not result in a mutation' )
125
+ }
126
+
127
+ const final = tx . apply ( null )
128
+
129
+ expect ( final ) . toEqual ( {
130
+ _id : '1' ,
131
+ _rev : 'txn_id' ,
132
+ _createdAt : '2020-01-01T12:34:55.000Z' ,
133
+ _type : 'test' ,
134
+ a : 'A string value' ,
135
+ } )
136
+
137
+ globalDateSpy . mockRestore ( )
138
+ } ,
139
+ )
140
+
112
141
test ( 'de-duplicate create respects deletes' , ( ) => {
142
+ const globalMockDate = new Date ( '2020-01-01T12:34:55.000Z' )
143
+ const globalDateSpy = jest . spyOn ( global , 'Date' ) . mockReturnValue ( globalMockDate )
144
+
113
145
const initial = { _id : '1' , _type : 'test' , a : 'A string value' , c : 'Some value' }
114
146
const sb = new SquashingBuffer ( initial )
115
147
add ( sb , { createIfNotExists : { _id : '1' , _type : 'test' , a : 'A string value' , c : 'Some value' } } )
@@ -124,7 +156,7 @@ test('de-duplicate create respects deletes', () => {
124
156
if ( ! tx ) {
125
157
throw new Error ( 'buffer purge did not result in a mutation' )
126
158
}
127
- tx . params . timestamp = '2021-01-01T12:34:55Z '
159
+ tx . params . timestamp = '2021-01-01T12:34:55.000Z '
128
160
129
161
const creates = tx . mutations . filter ( ( mut ) => ! ! mut . createIfNotExists )
130
162
expect ( creates . length ) . toBe ( 2 ) // Only a single create mutation expected (note: bn - is this correct?)
@@ -134,14 +166,16 @@ test('de-duplicate create respects deletes', () => {
134
166
expect ( final ) . toEqual ( {
135
167
_id : '1' ,
136
168
_type : 'test' ,
137
- _createdAt : '2021 -01-01T12:34:55Z ' ,
138
- _updatedAt : '2021-01-01T12:34:55Z ' ,
169
+ _createdAt : '2020 -01-01T12:34:55.000Z ' ,
170
+ _updatedAt : '2021-01-01T12:34:55.000Z ' ,
139
171
_rev : 'txn_id' ,
140
172
a : {
141
173
b : 'A wrapped value' ,
142
174
} ,
143
175
c : 'Changed' ,
144
176
} )
177
+
178
+ globalDateSpy . mockRestore ( )
145
179
} )
146
180
147
181
test ( 'de-duplicate create respects rebasing' , ( ) => {
0 commit comments