diff --git a/src/spy-middleware.ts b/src/spy-middleware.ts index c93bf09..8ab1ade 100644 --- a/src/spy-middleware.ts +++ b/src/spy-middleware.ts @@ -12,7 +12,7 @@ export interface Entry { } export default class SpyMiddleware { - private readonly entries: Entry[]; + private entries: Entry[]; constructor() { this.middleware = this.middleware.bind(this); @@ -28,6 +28,10 @@ export default class SpyMiddleware { return response; } + reset() { + this.entries = []; + } + calls(matcher: RouteMatcher = allMatcher) { return this.entries.filter(entry => matcher.test(entry.request.input, entry.request.init)); } diff --git a/src/yet-another-fetch-mock.ts b/src/yet-another-fetch-mock.ts index aee7e7a..03d5065 100644 --- a/src/yet-another-fetch-mock.ts +++ b/src/yet-another-fetch-mock.ts @@ -66,6 +66,10 @@ class FetchMock { this.routes.push({ matcher, handler: toMockHandlerFunction(handler) }); } + reset() { + this.routes = []; + } + private fetchproxy(input: RequestInfo, init?: RequestInit): Promise { const matchingRoute: Route | undefined = this.findMatchingRoute(input, init); const url: RequestUrl = findRequestUrl(input, init); diff --git a/test/spy-middleware.test.ts b/test/spy-middleware.test.ts index db60c3a..7e4c131 100644 --- a/test/spy-middleware.test.ts +++ b/test/spy-middleware.test.ts @@ -110,4 +110,20 @@ describe('SpyMiddlware', () => { done(); }); }); + + it('should remove all entries on reset', done => { + mock.get('/test/:id', { data: 'test' }); + Promise.all([fetch('/test/121'), fetch('/test/122')]).then(() => { + expect(spy.size()).toBe(2); + expect(spy.lastCall()).toBeDefined(); + expect(spy.lastUrl()).toBe('/test/122'); + expect(spy.called(MatcherUtils.url('/test/:id'))).toBe(true); + + spy.reset(); + expect(spy.lastCall()).toBeUndefined(); + expect(spy.lastUrl()).toBeUndefined(); + expect(spy.called(MatcherUtils.url('/test/:id'))).toBe(false); + done(); + }); + }); }); diff --git a/test/yet-another-fetch-mock.test.ts b/test/yet-another-fetch-mock.test.ts index 31d1dc4..1983c3a 100644 --- a/test/yet-another-fetch-mock.test.ts +++ b/test/yet-another-fetch-mock.test.ts @@ -134,9 +134,25 @@ describe('FetchMock', () => { }); it('should support fallback to realFetch', done => { - mock.get('/testurl', { key: 'testurl' }); + mock.get('/testurl', { num: 'testurl' }); + + const mocked = fetchToJson('/testurl').then(json => expect(json.num).toBe('testurl')); + const fallback = fetchToJson('https://xkcd.com/info.0.json').then(json => + expect(json.num).toBeDefined() + ); + + Promise.all([mocked, fallback]).then(() => done()); + }); + + it('should remove all mocks on reset', done => { + mock.get('https://xkcd.com/info.0.json', { num: 'testurl' }); + + const mocked = fetchToJson('https://xkcd.com/info.0.json').then(json => + expect(json.num).toBe('testurl') + ); + + mock.reset(); - const mocked = fetchToJson('/testurl').then(json => expect(json.key).toBe('testurl')); const fallback = fetchToJson('https://xkcd.com/info.0.json').then(json => expect(json.num).toBeDefined() );