@@ -54,6 +54,7 @@ import {
54
54
updateDoc
55
55
} from '../../lite/src/api/reference' ;
56
56
import { QuerySnapshot } from '../src/api/snapshot' ;
57
+ import { writeBatch } from '../src/api/write_batch' ;
57
58
58
59
use ( chaiAsPromised ) ;
59
60
@@ -173,6 +174,69 @@ genericMutationTests({
173
174
delete : deleteDoc
174
175
} ) ;
175
176
177
+ describe ( 'WriteBatch' , ( ) => {
178
+ class WriteBatchTester implements MutationTester {
179
+ delete ( ref : firestore . DocumentReference < unknown > ) : Promise < void > {
180
+ const batch = writeBatch ( ref . firestore ) ;
181
+ batch . delete ( ref ) ;
182
+ return batch . commit ( ) ;
183
+ }
184
+
185
+ set < T > (
186
+ ref : firestore . DocumentReference < T > ,
187
+ data : T | Partial < T > ,
188
+ options ?: firestore . SetOptions
189
+ ) : Promise < void > {
190
+ const batch = writeBatch ( ref . firestore ) ;
191
+ // TODO(mrschmidt): Find a way to remove the `any` cast here
192
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
193
+ ( batch . set as any ) . apply ( batch , Array . from ( arguments ) ) ;
194
+ return batch . commit ( ) ;
195
+ }
196
+
197
+ update (
198
+ ref : firestore . DocumentReference < unknown > ,
199
+ dataOrField : firestore . UpdateData | string | firestore . FieldPath ,
200
+ value ?: unknown ,
201
+ ...moreFieldsAndValues : unknown [ ]
202
+ ) : Promise < void > {
203
+ const batch = writeBatch ( ref . firestore ) ;
204
+ // TODO(mrschmidt): Find a way to remove the `any` cast here
205
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
206
+ ( batch . update as any ) . apply ( batch , Array . from ( arguments ) ) ;
207
+ return batch . commit ( ) ;
208
+ }
209
+ }
210
+
211
+ genericMutationTests ( new WriteBatchTester ( ) ) ;
212
+
213
+ it ( 'can add multiple operations' , ( ) => {
214
+ return withTestCollection ( async coll => {
215
+ const batch = writeBatch ( coll . firestore ) ;
216
+ batch . set ( doc ( coll ) , { doc : 1 } ) ;
217
+ batch . set ( doc ( coll ) , { doc : 2 } ) ;
218
+ await batch . commit ( ) ;
219
+
220
+ // TODO(firestorelite): Verify collection contents once getQuery is added
221
+ } ) ;
222
+ } ) ;
223
+
224
+ it ( 'cannot add write after commit' , ( ) => {
225
+ return withTestDoc ( async doc => {
226
+ const batch = writeBatch ( doc . firestore ) ;
227
+ batch . set ( doc , { doc : 1 } ) ;
228
+ const op = batch . commit ( ) ;
229
+ expect ( ( ) => batch . delete ( doc ) ) . to . throw (
230
+ 'A write batch can no longer be used after commit() has been called.'
231
+ ) ;
232
+ await op ;
233
+ expect ( ( ) => batch . delete ( doc ) ) . to . throw (
234
+ 'A write batch can no longer be used after commit() has been called.'
235
+ ) ;
236
+ } ) ;
237
+ } ) ;
238
+ } ) ;
239
+
176
240
function genericMutationTests (
177
241
op : MutationTester ,
178
242
validationUsesPromises : boolean = false
0 commit comments