Skip to content

Commit ed0a4e3

Browse files
committed
feat(reset): added reset functionality to mock and spy
simplifies usage in testing as one doesnt need to recreate the mock or spy between tests
1 parent 4c32bbd commit ed0a4e3

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/spy-middleware.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface Entry {
1212
}
1313

1414
export default class SpyMiddleware {
15-
private readonly entries: Entry[];
15+
private entries: Entry[];
1616

1717
constructor() {
1818
this.middleware = this.middleware.bind(this);
@@ -28,6 +28,10 @@ export default class SpyMiddleware {
2828
return response;
2929
}
3030

31+
reset() {
32+
this.entries = [];
33+
}
34+
3135
calls(matcher: RouteMatcher = allMatcher) {
3236
return this.entries.filter(entry => matcher.test(entry.request.input, entry.request.init));
3337
}

src/yet-another-fetch-mock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class FetchMock {
6666
this.routes.push({ matcher, handler: toMockHandlerFunction(handler) });
6767
}
6868

69+
reset() {
70+
this.routes = [];
71+
}
72+
6973
private fetchproxy(input: RequestInfo, init?: RequestInit): Promise<Response> {
7074
const matchingRoute: Route | undefined = this.findMatchingRoute(input, init);
7175
const url: RequestUrl = findRequestUrl(input, init);

test/spy-middleware.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,20 @@ describe('SpyMiddlware', () => {
110110
done();
111111
});
112112
});
113+
114+
it('should remove all entries on reset', done => {
115+
mock.get('/test/:id', { data: 'test' });
116+
Promise.all([fetch('/test/121'), fetch('/test/122')]).then(() => {
117+
expect(spy.size()).toBe(2);
118+
expect(spy.lastCall()).toBeDefined();
119+
expect(spy.lastUrl()).toBe('/test/122');
120+
expect(spy.called(MatcherUtils.url('/test/:id'))).toBe(true);
121+
122+
spy.reset();
123+
expect(spy.lastCall()).toBeUndefined();
124+
expect(spy.lastUrl()).toBeUndefined();
125+
expect(spy.called(MatcherUtils.url('/test/:id'))).toBe(false);
126+
done();
127+
});
128+
});
113129
});

test/yet-another-fetch-mock.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,25 @@ describe('FetchMock', () => {
134134
});
135135

136136
it('should support fallback to realFetch', done => {
137-
mock.get('/testurl', { key: 'testurl' });
137+
mock.get('/testurl', { num: 'testurl' });
138+
139+
const mocked = fetchToJson('/testurl').then(json => expect(json.num).toBe('testurl'));
140+
const fallback = fetchToJson('https://xkcd.com/info.0.json').then(json =>
141+
expect(json.num).toBeDefined()
142+
);
143+
144+
Promise.all([mocked, fallback]).then(() => done());
145+
});
146+
147+
it('should remove all mocks on reset', done => {
148+
mock.get('https://xkcd.com/info.0.json', { num: 'testurl' });
149+
150+
const mocked = fetchToJson('https://xkcd.com/info.0.json').then(json =>
151+
expect(json.num).toBe('testurl')
152+
);
153+
154+
mock.reset();
138155

139-
const mocked = fetchToJson('/testurl').then(json => expect(json.key).toBe('testurl'));
140156
const fallback = fetchToJson('https://xkcd.com/info.0.json').then(json =>
141157
expect(json.num).toBeDefined()
142158
);

0 commit comments

Comments
 (0)