Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
fix toHaveLastFetched and toHaveNthFetched
Browse files Browse the repository at this point in the history
Was relying on equality of refernce for different calls, but now fetch-mock builds a new cll object each time, so references are different and need to do a manual comparison
  • Loading branch information
wheresrhys committed Nov 26, 2020
1 parent f6b1024 commit 2a5fe11
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 86 deletions.
16 changes: 14 additions & 2 deletions jest-extensions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
const callsAreEqual = (c1, c2) => {
if (!c1 && !c2) return true;
if (!c1 || !c2) return false;
if (c1[0] !== c2[0]) return false;
if (c1[1] !== c2[1]) return false;
if (c1.request !== c2.request) return false;
if (c1.identifier !== c2.identifier) return false;
if (c1.isUnmatched !== c2.isUnmatched) return false;
if (c1.response !== c2.response) return false;
return true;
};

const methodlessExtensions = {
toHaveFetched: (fetchMock, url, options) => {
if (fetchMock.called(url, options)) {
Expand All @@ -18,7 +30,7 @@ const methodlessExtensions = {
}
const lastCall = [...allCalls].pop();
const lastUrlCall = [...fetchMock.calls(url, options)].pop();
if (lastCall === lastUrlCall) {
if (callsAreEqual(lastCall, lastUrlCall)) {
return { pass: true };
}
return {
Expand All @@ -31,7 +43,7 @@ const methodlessExtensions = {
toHaveNthFetched: (fetchMock, n, url, options) => {
const nthCall = fetchMock.calls()[n - 1];
const urlCalls = fetchMock.calls(url, options);
if (urlCalls.includes(nthCall)) {
if (urlCalls.some((call) => callsAreEqual(call, nthCall))) {
return { pass: true };
}
return {
Expand Down
Loading

0 comments on commit 2a5fe11

Please sign in to comment.