Skip to content

Commit 4f7328a

Browse files
committed
fix unit tests
1 parent bb2cb84 commit 4f7328a

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/libs/actions/PersistedRequests.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ function clear() {
183183
persistedRequests = [];
184184
pendingSaveOperations = [];
185185
knownRequestIDs.clear();
186-
pendingOnyxWrites = 0;
187186
Onyx.set(ONYXKEYS.PERSISTED_ONGOING_REQUESTS, null);
188-
return Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, []);
187+
return trackOnyxWrite(Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, []));
189188
}
190189

191190
function getLength(): number {
@@ -442,6 +441,16 @@ function getOngoingRequest(): AnyRequest | null {
442441
return ongoingRequest;
443442
}
444443

444+
/**
445+
* Reset the pending Onyx write counter. Used ONLY in tests to simulate
446+
* a clean state before cross-tab event simulations. In production,
447+
* cross-tab updates arrive via storage events which are independent of
448+
* the Onyx.set promise lifecycle, so the counter is always 0 at that point.
449+
*/
450+
function resetPendingWritesForTest() {
451+
pendingOnyxWrites = 0;
452+
}
453+
445454
export {
446455
clear,
447456
save,
@@ -457,4 +466,5 @@ export {
457466
deleteRequestsByIndices,
458467
onInitialization,
459468
onCrossTabRequestsMerged,
469+
resetPendingWritesForTest,
460470
};

tests/unit/PersistedRequests.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,14 @@ describe('PersistedRequests persistence guarantees', () => {
266266
await waitForBatchedUpdates();
267267
expect(PersistedRequests.getAll()).toHaveLength(2);
268268

269+
// Reset the pending write counter before simulating a cross-tab event.
270+
// In production, cross-tab updates arrive via storage events which are
271+
// independent of Onyx.set promise timing, so the counter is always 0.
272+
// In tests, promise resolution timing is unpredictable relative to
273+
// Onyx callbacks, so we reset explicitly.
274+
PersistedRequests.resetPendingWritesForTest();
275+
269276
// Simulate a cross-tab callback: leader processed requestA and removed it.
270-
// After waitForBatchedUpdates, pendingOnyxWrites is 0, so the callback
271-
// will reconcile deletions (requestA no longer on disk → removed from memory).
272277
await Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, [requestB]);
273278
await waitForBatchedUpdates();
274279

0 commit comments

Comments
 (0)