@@ -32,7 +32,9 @@ import {
32
32
PrivateSettings ,
33
33
SnapshotListenOptions ,
34
34
newTestFirestore ,
35
- newTestApp
35
+ newTestApp ,
36
+ writeBatch ,
37
+ WriteBatch
36
38
} from './firebase_export' ;
37
39
import {
38
40
ALT_PROJECT_ID ,
@@ -315,11 +317,34 @@ export function withTestCollectionSettings<T>(
315
317
const collectionId = 'test-collection-' + doc ( collection ( testDb , 'x' ) ) . id ;
316
318
const testCollection = collection ( testDb , collectionId ) ;
317
319
const setupCollection = collection ( setupDb , collectionId ) ;
318
- const sets : Array < Promise < void > > = [ ] ;
319
- Object . keys ( docs ) . forEach ( key => {
320
- sets . push ( setDoc ( doc ( setupCollection , key ) , docs [ key ] ) ) ;
321
- } ) ;
322
- return Promise . all ( sets ) . then ( ( ) => fn ( testCollection , testDb ) ) ;
320
+
321
+ const writeBatchCommits : Array < Promise < void > > = [ ] ;
322
+ let writeBatch_ : WriteBatch | null = null ;
323
+ let writeBatchSize = 0 ;
324
+
325
+ for ( const key in docs ) {
326
+ if ( writeBatch_ === null ) {
327
+ writeBatch_ = writeBatch ( setupDb ) ;
328
+ }
329
+
330
+ writeBatch_ . set ( doc ( setupCollection , key ) , docs [ key ] ) ;
331
+ writeBatchSize ++ ;
332
+
333
+ // Write batches are capped at 500 writes. Use 400 just to be safe.
334
+ if ( writeBatchSize === 400 ) {
335
+ writeBatchCommits . push ( writeBatch_ . commit ( ) ) ;
336
+ writeBatch_ = null ;
337
+ writeBatchSize = 0 ;
338
+ }
339
+ }
340
+
341
+ if ( writeBatch_ !== null ) {
342
+ writeBatchCommits . push ( writeBatch_ . commit ( ) ) ;
343
+ }
344
+
345
+ return Promise . all ( writeBatchCommits ) . then ( ( ) =>
346
+ fn ( testCollection , testDb )
347
+ ) ;
323
348
}
324
349
) ;
325
350
}
0 commit comments