Skip to content

Commit

Permalink
Merge pull request #853 from wheresrhys/rhys/fix-852
Browse files Browse the repository at this point in the history
fix: clearHistory() can deal with unmatched calls
  • Loading branch information
wheresrhys authored Oct 27, 2024
2 parents df91c9c + 012e9ca commit d40b614
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/fetch-mock/src/CallHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ class CallHistory {
}

clear() {
this.callLogs.forEach(({ route }) => route.reset());
this.callLogs.forEach(({ route }) => {
if (route) {
route.reset();
}
});
this.callLogs = [];
}

Expand Down
33 changes: 32 additions & 1 deletion packages/fetch-mock/src/__tests__/CallHistory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,38 @@ describe('CallHistory', () => {
const fetchTheseUrls = (...urls) =>
Promise.all(urls.map((url) => fm.fetchHandler(url)));

describe('helper methods', () => {
describe('clear', () => {
it('should clear all call logs', async () => {
fm.route('http://a.com', 200).route('http://b.com', 200);
fm.fetchHandler('http://a.com');
fm.fetchHandler('http://b.com');

expect(fm.callHistory.callLogs.length).toEqual(2);
fm.callHistory.clear();
expect(fm.callHistory.callLogs.length).toEqual(0);
});
it('should clear counter in routes that are set to only respond a set number of times', async () => {
fm.once('http://a.com', 200);
fm.fetchHandler('http://a.com');
fm.callHistory.clear();
await expect(fm.fetchHandler('http://a.com')).resolves.not.toThrow();
});
it('should not error if there are unmatched calls', async () => {
try {
await fm.fetchHandler('https://example.com');
} catch {
expect(() => fm.callHistory.clear()).not.toThrow(); // Error
}
});

it('is aliased to fetchMock.clearHistory()', async () => {
vi.spyOn(fm.callHistory, 'clear');
fm.clearHistory();
expect(fm.callHistory.clear).toHaveBeenCalled();
fm.callHistory.clear.mockRestore();
});
});
describe('inspecting call history methods', () => {
describe('called()', () => {
it('returns a suitable boolean', () => {
fm.catch();
Expand Down

0 comments on commit d40b614

Please sign in to comment.