Skip to content

Commit 0524900

Browse files
committedOct 10, 2023
add startsWith, endsWith and includes method
1 parent d5d14a9 commit 0524900

File tree

4 files changed

+110
-29
lines changed

4 files changed

+110
-29
lines changed
 

‎CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.1.0
2+
3+
### Added
4+
5+
- add `startsWith`, `endsWith` and `includes` helper methods.
6+
17
## 1.0.0
28

3-
Initial version
9+
Initial version

‎README.md

+12
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ describe('some test file', () => {
6868

6969
fetchMock.get('https://match.get/test', expected);
7070
fetchMock.put(/https:\/\/match.put\//, expected);
71+
fetchMock.post.startsWith('https://match.post/', expected);
7172

7273
const r1 = await doFetchWithToken('https://match.get/test');
7374

@@ -78,6 +79,12 @@ describe('some test file', () => {
7879
});
7980

8081
expect(r2).toBe(expected);
82+
83+
const r3 = await doFetchWithToken('https://match.post/test', {
84+
method: 'POST',
85+
});
86+
87+
expect(r3).toBe(expected);
8188
});
8289

8390
test('some test with a complex matcher', () => {
@@ -119,6 +126,11 @@ function fetchMock.post(url: string | RegExp, response: Response): void;
119126
function fetchMock.put(url: string | RegExp, response: Response): void;
120127
function fetchMock.patch(url: string | RegExp, response: Response): void;
121128
function fetchMock.delete(url: string | RegExp, response: Response): void;
129+
130+
// foreach methods, there are string helper too
131+
function fetchMock.<httpVerb>.startsWith(url: string, response: Response): void;
132+
function fetchMock.<httpVerb>.endsWith(url: string, response: Response): void;
133+
function fetchMock.<httpVerb>.includes(url: string, response: Response): void;
122134
```
123135

124136
Utility functions:

‎src/fetchMock.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,39 @@ describe('fetchMock.method', () => {
160160
);
161161
});
162162
});
163+
164+
describe('fetchMock.method.stringMethod', () => {
165+
test('startsWith', async () => {
166+
const response = new Response('Hello world !');
167+
168+
fetchMock.delete.startsWith('https://www.mapado.com', response);
169+
170+
const result = await fetch('https://www.mapado.com/foo.bar', {
171+
method: 'DELETE',
172+
});
173+
174+
expect(result).toBe(response);
175+
});
176+
177+
test('endsWith', async () => {
178+
const response = new Response('Hello world !');
179+
180+
fetchMock.patch.endsWith('foo.bar', response);
181+
182+
const result = await fetch('https://www.mapado.com/foo.bar', {
183+
method: 'PATCH',
184+
});
185+
186+
expect(result).toBe(response);
187+
});
188+
189+
test('includes', async () => {
190+
const response = new Response('Hello world !');
191+
192+
fetchMock.get.includes('mapado', response);
193+
194+
const result = await fetch('https://www.mapado.com/foo.bar');
195+
196+
expect(result).toBe(response);
197+
});
198+
});

‎src/fetchMock.ts

+55-28
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ export function resetMocks(): void {
2121
matchers = [];
2222
}
2323

24-
function fetchMock(matcher: MatcherFunction, response: Response): void {
25-
matchers.push(new Matcher(matcher, response));
26-
27-
overrideFetch();
28-
}
29-
30-
function overrideFetch() {
24+
function overrideFetch(): void {
3125
globalThis.fetch = async (
3226
input: URL | RequestInfo,
3327
options: RequestInit | undefined,
@@ -46,11 +40,36 @@ function overrideFetch() {
4640
};
4741
}
4842

43+
type FetchMockHelper = {
44+
(url: string | RegExp, response: Response): void;
45+
startsWith: FetchMockStringHelper;
46+
endsWith: FetchMockStringHelper;
47+
includes: FetchMockStringHelper;
48+
};
49+
type FetchMockStringHelper = (url: string, response: Response) => void;
50+
type ValidStringMethod = 'startsWith' | 'endsWith' | 'includes';
51+
52+
type FetchMockFunction = {
53+
(matcher: MatcherFunction, response: Response): void;
54+
get: FetchMockHelper;
55+
post: FetchMockHelper;
56+
put: FetchMockHelper;
57+
patch: FetchMockHelper;
58+
delete: FetchMockHelper;
59+
};
60+
61+
const fetchMock: FetchMockFunction = (matcher, response): void => {
62+
matchers.push(new Matcher(matcher, response));
63+
64+
overrideFetch();
65+
};
66+
4967
/**
50-
* Simple matcher for quick mocking
68+
* Simple matcher for quick mocking.
69+
* For example `fetchMock.post(url: string | RegExp, response: Response)` helper.
5170
*/
5271
function generateFetchMockHelper(method: string): FetchMockHelper {
53-
return (url: string | RegExp, response: Response) => {
72+
const fn: FetchMockHelper = (url: string | RegExp, response: Response) => {
5473
return fetchMock((input, options) => {
5574
if (getOptionMethod(options) !== method) {
5675
return false;
@@ -69,28 +88,36 @@ function generateFetchMockHelper(method: string): FetchMockHelper {
6988
);
7089
}, response);
7190
};
72-
}
73-
74-
type FetchMockHelper = (url: string | RegExp, response: Response) => void;
75-
76-
const get: FetchMockHelper = (url, response): void =>
77-
generateFetchMockHelper('GET')(url, response);
7891

79-
const post: FetchMockHelper = (url, response): void =>
80-
generateFetchMockHelper('POST')(url, response);
92+
fn.startsWith = generateFetchMockStringHelper(method, 'startsWith');
93+
fn.endsWith = generateFetchMockStringHelper(method, 'endsWith');
94+
fn.includes = generateFetchMockStringHelper(method, 'includes');
8195

82-
const put: FetchMockHelper = (url, response): void =>
83-
generateFetchMockHelper('PUT')(url, response);
96+
return fn;
97+
}
8498

85-
const patch: FetchMockHelper = (url, response): void =>
86-
generateFetchMockHelper('PATCH')(url, response);
99+
/**
100+
* This function generate string helper functions.
101+
* For example `fetchMock.post.startsWith(startUrl: string, response: Response)` helper.
102+
*/
103+
function generateFetchMockStringHelper(
104+
method: string,
105+
methodName: ValidStringMethod,
106+
): FetchMockStringHelper {
107+
return (url, response) => {
108+
return fetchMock(
109+
(input, options) =>
110+
getOptionMethod(options) === method &&
111+
getInputUrl(input)[methodName](url),
112+
response,
113+
);
114+
};
115+
}
87116

88-
const deleteFn: FetchMockHelper = (url, response): void =>
89-
generateFetchMockHelper('DELETE')(url, response);
90-
fetchMock.get = get;
91-
fetchMock.post = post;
92-
fetchMock.put = put;
93-
fetchMock.patch = patch;
94-
fetchMock.delete = deleteFn;
117+
fetchMock.get = generateFetchMockHelper('GET');
118+
fetchMock.post = generateFetchMockHelper('POST');
119+
fetchMock.put = generateFetchMockHelper('PUT');
120+
fetchMock.patch = generateFetchMockHelper('PATCH');
121+
fetchMock.delete = generateFetchMockHelper('DELETE');
95122

96123
export default fetchMock;

0 commit comments

Comments
 (0)