Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/spy-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
}
Expand Down
4 changes: 4 additions & 0 deletions src/yet-another-fetch-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class FetchMock {
this.routes.push({ matcher, handler: toMockHandlerFunction(handler) });
}

reset() {
this.routes = [];
}

private fetchproxy(input: RequestInfo, init?: RequestInit): Promise<Response> {
const matchingRoute: Route | undefined = this.findMatchingRoute(input, init);
const url: RequestUrl = findRequestUrl(input, init);
Expand Down
16 changes: 16 additions & 0 deletions test/spy-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
20 changes: 18 additions & 2 deletions test/yet-another-fetch-mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down