Skip to content

Commit

Permalink
Add test for cancellable requests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaaron committed May 19, 2022
1 parent 88a0e32 commit 37f69e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/libs/Network/MainQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@ function clear() {
HttpUtils.cancelPendingRequests();
}

/**
* @returns {Array}
*/
function getAll() {
return networkRequestQueue;
}

export {
clear,
replay,
push,
process,
getAll,
};
24 changes: 24 additions & 0 deletions tests/unit/NetworkTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,27 @@ test('persistable request will move directly to the SequentialQueue when we are
expect(thirdRequestCommandName).toBe('MockCommandTwo');
});
});

test('cancelled requests should not be retried', () => {
const xhr = jest.spyOn(HttpUtils, 'xhr');

// GIVEN a mock that will return a "cancelled" request error
global.fetch = jest.fn()
.mockRejectedValue(new DOMException('Aborted', 'AbortError'));

return Onyx.set(ONYXKEYS.NETWORK, {isOffline: false})
.then(() => {
// WHEN we make a few requests and then cancel them
Network.post('MockCommandOne');
Network.post('MockCommandTwo');
Network.post('MockCommandThree');

// WHEN we wait for the requests to all cancel
return waitForPromisesToResolve();
})
.then(() => {
// THEN expect our queue to be empty and for no requests to have been retried
expect(MainQueue.getAll().length).toBe(0);
expect(xhr.mock.calls.length).toBe(3);
});
});

0 comments on commit 37f69e6

Please sign in to comment.