Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions tests/unit/MiddlewareTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ import waitForNetworkPromises from '../utils/waitForNetworkPromises';

type FormDataObject = {body: TestHelper.FormData};

const waitForFetchCallCount = async (expectedCallCount: number, maxAttempts = 5) => {
for (let attempt = 0; attempt < maxAttempts; attempt++) {
if ((global.fetch as jest.Mock).mock.calls.length === expectedCallCount) {
return;
}
await waitForNetworkPromises();
}
expect(global.fetch).toHaveBeenCalledTimes(expectedCallCount);
};

Onyx.init({
keys: ONYXKEYS,
});
Expand Down Expand Up @@ -93,7 +103,7 @@ describe('Middleware', () => {
SequentialQueue.unpause();
await waitForNetworkPromises();

expect(global.fetch).toHaveBeenCalledTimes(2);
await waitForFetchCallCount(2);
expect(global.fetch).toHaveBeenLastCalledWith('https://www.expensify.com.dev/api/AddComment?', expect.anything());
TestHelper.assertFormDataMatchesObject(
{
Expand Down Expand Up @@ -340,7 +350,7 @@ describe('Middleware', () => {
SequentialQueue.unpause();
await waitForBatchedUpdates();

expect(global.fetch).toHaveBeenCalledTimes(2);
await waitForFetchCallCount(2);

const optimisticReportUpdated = await new Promise<OnyxEntry<OnyxReport>>((resolve) => {
const connection = Onyx.connect({
Expand Down