Skip to content

Commit 06d859e

Browse files
committed
Add a test case to set a deleted doc
1 parent f88805e commit 06d859e

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

packages/firestore/test/integration/api/transactions.test.ts

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
QueryDocumentSnapshot,
2424
Transaction,
2525
collection,
26+
deleteDoc,
2627
doc,
2728
DocumentReference,
2829
DocumentSnapshot,
@@ -87,6 +88,16 @@ apiDescribe('Database transactions', (persistence: boolean) => {
8788
await transaction.get(docRef);
8889
}
8990

91+
enum FromDocumentType {
92+
// The operation will be performed on a document that exists.
93+
EXISTING = 'existing',
94+
// The operation will be performed on a document that has never existed.
95+
NON_EXISTENT = 'non_existent',
96+
// The operation will be performed on a document that existed, but was
97+
// deleted.
98+
DELETED = 'deleted'
99+
}
100+
90101
/**
91102
* Used for testing that all possible combinations of executing transactions
92103
* result in the desired document value or error.
@@ -101,16 +112,21 @@ apiDescribe('Database transactions', (persistence: boolean) => {
101112
constructor(readonly db: Firestore) {}
102113

103114
private docRef!: DocumentReference;
104-
private fromExistingDoc: boolean = false;
115+
private fromDocumentType: FromDocumentType = FromDocumentType.NON_EXISTENT;
105116
private stages: TransactionStage[] = [];
106117

107118
withExistingDoc(): this {
108-
this.fromExistingDoc = true;
119+
this.fromDocumentType = FromDocumentType.EXISTING;
109120
return this;
110121
}
111122

112123
withNonexistentDoc(): this {
113-
this.fromExistingDoc = false;
124+
this.fromDocumentType = FromDocumentType.NON_EXISTENT;
125+
return this;
126+
}
127+
128+
withDeletedDoc(): this {
129+
this.fromDocumentType = FromDocumentType.DELETED;
114130
return this;
115131
}
116132

@@ -176,8 +192,17 @@ apiDescribe('Database transactions', (persistence: boolean) => {
176192

177193
private async prepareDoc(): Promise<void> {
178194
this.docRef = doc(collection(this.db, 'tester-docref'));
179-
if (this.fromExistingDoc) {
180-
await setDoc(this.docRef, { foo: 'bar0' });
195+
switch (this.fromDocumentType) {
196+
case FromDocumentType.EXISTING:
197+
await setDoc(this.docRef, { foo: 'bar0' });
198+
break;
199+
case FromDocumentType.NON_EXISTENT:
200+
// Nothing to do; document does not exist.
201+
break;
202+
case FromDocumentType.DELETED:
203+
await setDoc(this.docRef, { foo: 'bar0' });
204+
await deleteDoc(this.docRef);
205+
break;
181206
}
182207
}
183208

@@ -289,6 +314,42 @@ apiDescribe('Database transactions', (persistence: boolean) => {
289314
});
290315
});
291316

317+
it('runs transactions after getting a deleted document', async () => {
318+
return withTestDb(persistence, async db => {
319+
const tt = new TransactionTester(db);
320+
321+
await tt.withDeletedDoc().run(get, delete1, delete1).expectNoDoc();
322+
await tt
323+
.withDeletedDoc()
324+
.run(get, delete1, update2)
325+
.expectError('invalid-argument');
326+
await tt
327+
.withDeletedDoc()
328+
.run(get, delete1, set2)
329+
.expectDoc({ foo: 'bar2' });
330+
331+
await tt
332+
.withDeletedDoc()
333+
.run(get, update1, delete1)
334+
.expectError('invalid-argument');
335+
await tt
336+
.withDeletedDoc()
337+
.run(get, update1, update2)
338+
.expectError('invalid-argument');
339+
await tt
340+
.withDeletedDoc()
341+
.run(get, update1, set1)
342+
.expectError('invalid-argument');
343+
344+
await tt.withDeletedDoc().run(get, set1, delete1).expectNoDoc();
345+
await tt
346+
.withDeletedDoc()
347+
.run(get, set1, update2)
348+
.expectDoc({ foo: 'bar2' });
349+
await tt.withDeletedDoc().run(get, set1, set2).expectDoc({ foo: 'bar2' });
350+
});
351+
});
352+
292353
it('runs transactions on existing document', async () => {
293354
return withTestDb(persistence, async db => {
294355
const tt = new TransactionTester(db);

0 commit comments

Comments
 (0)