@@ -29,6 +29,7 @@ import {
29
29
Firestore ,
30
30
FirestoreError ,
31
31
getDoc ,
32
+ deleteDoc ,
32
33
runTransaction ,
33
34
setDoc
34
35
} from '../util/firebase_export' ;
@@ -610,6 +611,57 @@ apiDescribe('Database transactions', (persistence: boolean) => {
610
611
} ) ;
611
612
} ) ;
612
613
614
+ it ( 'can set and delete inside a transaction on a deleted document' , ( ) => {
615
+ return withTestDb ( persistence , db => {
616
+ const docRef = doc ( collection ( db , 'foo' ) ) ;
617
+
618
+ // A function that reads a document then sets some data for it inside the
619
+ // context of the given transaction.
620
+ const readAndSet : ( txn : Transaction ) => Promise < Transaction > = (
621
+ txn : Transaction
622
+ ) => txn . get ( docRef ) . then ( ( ) => txn . set ( docRef , { count : 16 } ) ) ;
623
+
624
+ // A function that reads a document then deletes it inside the context of
625
+ // the given transaction.
626
+ const readAndDelete : ( txn : Transaction ) => Promise < Transaction > = (
627
+ txn : Transaction
628
+ ) => txn . get ( docRef ) . then ( ( ) => txn . delete ( docRef ) ) ;
629
+
630
+ return (
631
+ // Perform a readAndSet, then delete the document, then perform readAndSet
632
+ // again. This sequence of operations is valid and should succeed.
633
+ runTransaction ( db , txn => readAndSet ( txn ) )
634
+ . then ( ( ) => getDoc ( docRef ) )
635
+ . then ( snapshot => {
636
+ expect ( snapshot . exists ( ) ) . to . equal ( true ) ;
637
+ expect ( snapshot . data ( ) ) . to . deep . equal ( { count : 16 } ) ;
638
+ } )
639
+ . then ( ( ) => deleteDoc ( docRef ) )
640
+ . then ( ( ) => getDoc ( docRef ) )
641
+ . then ( snapshot => expect ( snapshot . exists ( ) ) . to . equal ( false ) )
642
+ // Perform a transaction to read and set for the second time.
643
+ . then ( ( ) => runTransaction ( db , txn => readAndSet ( txn ) ) )
644
+ . then ( ( ) => getDoc ( docRef ) )
645
+ . then ( snapshot => {
646
+ expect ( snapshot . exists ( ) ) . to . equal ( true ) ;
647
+ expect ( snapshot . data ( ) ) . to . deep . equal ( { count : 16 } ) ;
648
+ } )
649
+ // Perform readAndDelete twice. This is also valid and should succeed.
650
+ . then ( ( ) => runTransaction ( db , txn => readAndDelete ( txn ) ) )
651
+ . then ( ( ) => getDoc ( docRef ) )
652
+ . then ( snapshot => expect ( snapshot . exists ( ) ) . to . equal ( false ) )
653
+ . then ( ( ) => runTransaction ( db , txn => readAndDelete ( txn ) ) )
654
+ . then ( ( ) => getDoc ( docRef ) )
655
+ . then ( snapshot => {
656
+ expect ( snapshot . exists ( ) ) . to . equal ( false ) ;
657
+ } )
658
+ . catch ( ( err : FirestoreError ) => {
659
+ expect . fail ( 'Expected the transaction to succeed, but got ' + err ) ;
660
+ } )
661
+ ) ;
662
+ } ) ;
663
+ } ) ;
664
+
613
665
// PORTING NOTE: These tests are for FirestoreDataConverter support and apply
614
666
// only to web.
615
667
apiDescribe ( 'withConverter() support' , ( persistence : boolean ) => {
0 commit comments