Skip to content

Commit 5a43533

Browse files
Add one more unit test
1 parent 966a6ba commit 5a43533

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

packages/firestore/test/unit/local/persistence_transaction.test.ts

+46-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { expect } from 'chai';
2020
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
2121
import { Persistence } from '../../../src/local/persistence';
2222
import { PersistencePromise } from '../../../src/local/persistence_promise';
23+
import { DbTarget, DbTargetKey } from '../../../src/local/indexeddb_schema';
24+
import { TargetId } from '../../../src/core/types';
2325

2426
let persistence: Persistence;
2527

@@ -48,27 +50,63 @@ describe('IndexedDbTransaction', () => {
4850
afterEach(() => persistence.shutdown());
4951

5052
genericTransactionTests();
53+
54+
it('only invokes onCommittedListener once with retries', async () => {
55+
let runCount = 0;
56+
let commitCount = 0;
57+
await persistence.runTransaction(
58+
'onCommitted',
59+
'readwrite-idempotent',
60+
txn => {
61+
const targetsStore = IndexedDbPersistence.getStore<
62+
DbTargetKey,
63+
{ targetId: TargetId }
64+
>(txn, DbTarget.store);
65+
66+
txn.addOnCommittedListener(() => {
67+
++commitCount;
68+
});
69+
70+
++runCount;
71+
if (runCount == 1) {
72+
// Trigger a unique key violation
73+
return targetsStore
74+
.add({ targetId: 1 })
75+
.next(() => targetsStore.add({ targetId: 1 }));
76+
} else {
77+
return PersistencePromise.resolve(0);
78+
}
79+
}
80+
);
81+
82+
expect(runCount).to.be.equal(2);
83+
expect(commitCount).to.be.equal(1);
84+
});
5185
});
5286

5387
function genericTransactionTests(): void {
5488
it('invokes onCommittedListener when transaction succeeds', async () => {
5589
let onCommitted = false;
56-
await persistence.runTransaction('onCommitted', 'readonly', txn => {
57-
txn.addOnCommittedListener(() => {
58-
onCommitted = true;
59-
});
90+
await persistence.runTransaction(
91+
'onCommitted',
92+
'readonly-idempotent',
93+
txn => {
94+
txn.addOnCommittedListener(() => {
95+
onCommitted = true;
96+
});
6097

61-
expect(onCommitted).to.be.false;
62-
return PersistencePromise.resolve();
63-
});
98+
expect(onCommitted).to.be.false;
99+
return PersistencePromise.resolve();
100+
}
101+
);
64102

65103
expect(onCommitted).to.be.true;
66104
});
67105

68106
it('does not invoke onCommittedListener when transaction fails', async () => {
69107
let onCommitted = false;
70108
await persistence
71-
.runTransaction('onCommitted', 'readonly', txn => {
109+
.runTransaction('onCommitted', 'readonly-idempotent', txn => {
72110
txn.addOnCommittedListener(() => {
73111
onCommitted = true;
74112
});

0 commit comments

Comments
 (0)