UNPKG

5.53 kBPlain TextView Raw
1import fetchMock = require('..');
2
3fetchMock.mock();
4fetchMock.mock("http://test.com", 200);
5fetchMock.mock("http://test.com", 200, {
6 headers: {
7 test: "header"
8 }
9});
10fetchMock.mock("http://test.com", 200, {
11 body: {
12 test: [{
13 string: "value",
14 number: 1.34,
15 bool: true,
16 }]
17 }
18});
19fetchMock.mock("http//test.com", 200, {
20 query: {
21 searchValue: "apples"
22 }
23});
24fetchMock.mock("express:/users/:user", 200, {
25 params: {
26 user: "someone"
27 }
28});
29fetchMock.mock("http://test.com", 200, {
30 functionMatcher: (url, opts) => {
31 return url.includes("test.com");
32 }
33});
34fetchMock.mock("http://test.com", 200, {
35 repeat: 2
36});
37fetchMock.mock("http://test.com", 200, {
38 delay: 10
39});
40fetchMock.mock(/test\.com/, 200);
41fetchMock.mock(() => true, 200);
42fetchMock.mock((url, opts) => true, 200);
43fetchMock.once("http://test.com", 200);
44
45fetchMock.mock(/test/, "test").mock(/test/, { a: "b" });
46fetchMock.mock(/test/, {
47 status: 200,
48 headers: {
49 test: "test"
50 },
51 body: {
52 a: "b"
53 }
54});
55
56fetchMock.mock({
57 url: "http://test.com",
58 response: 200,
59 headers: {},
60 query: {},
61 params: {},
62 body: {},
63 repeat: 1,
64 delay: 500,
65 functionMatcher: () => true
66});
67
68fetchMock.mock({
69 url: "http://test.com",
70}, 200);
71
72fetchMock.restore().reset().resetHistory().resetBehavior();
73
74let calls: fetchMock.MockCall[] = fetchMock.calls(/https?:\/\/test.com/, {
75 method: 'GET',
76});
77calls[0][0].toUpperCase();
78calls[0].identifier.toUpperCase();
79calls[0].isUnmatched;
80calls = fetchMock.calls();
81calls = fetchMock.calls(true);
82calls = fetchMock.calls("http://test.com", "GET");
83
84let doneStatus: boolean = fetchMock.done();
85doneStatus = fetchMock.done(true);
86doneStatus = fetchMock.done("http://test.com");
87doneStatus = fetchMock.done(/https?:\/\/test.com/);
88
89let calledStatus: boolean = fetchMock.called();
90calledStatus = fetchMock.called(true);
91calledStatus = fetchMock.called("http://test.com");
92calledStatus = fetchMock.called(/https?:\/\/test.com/);
93calledStatus = fetchMock.called("http://test.com", "GET");
94calledStatus = fetchMock.called("http://test.com", {
95 method: "GET",
96});
97calledStatus = fetchMock.called((url: string, opts: fetchMock.MockRequest): boolean => {
98 return true;
99});
100calledStatus = fetchMock.called(fetchMock.MATCHED);
101calledStatus = fetchMock.called(fetchMock.UNMATCHED);
102
103let lastCall: fetchMock.MockCall = fetchMock.lastCall();
104lastCall = fetchMock.lastCall(/https?:\/\/test.com/, {
105 method: "GET",
106});
107lastCall = fetchMock.lastCall("https://test.com", "GET");
108
109let lastUrl: string = fetchMock.lastUrl();
110lastUrl = fetchMock.lastUrl(true);
111lastUrl = fetchMock.lastUrl("http://test.com");
112lastUrl = fetchMock.lastUrl(/https?:\/\/test.com/);
113lastUrl = fetchMock.lastUrl("http://test.com", "GET");
114lastUrl = fetchMock.lastUrl("http://test.com", {
115 method: "GET",
116});
117let lastOptions: fetchMock.MockOptions = fetchMock.lastOptions();
118lastOptions = fetchMock.lastOptions(true);
119lastOptions = fetchMock.lastOptions("http://test.com");
120lastOptions = fetchMock.lastOptions(/https?:\/\/test.com/);
121lastOptions = fetchMock.lastOptions("http://test.com", "GET");
122lastOptions = fetchMock.lastOptions("http://test.com", {
123 method: "GET",
124});
125
126fetchMock.get("http://test.com", 200);
127fetchMock.getOnce("http://test.com", 200);
128fetchMock.post("http://test.com", 200);
129fetchMock.postOnce("http://test.com", 200);
130fetchMock.put("http://test.com", 200);
131fetchMock.putOnce("http://test.com", 200);
132fetchMock.delete("http://test.com", 200);
133fetchMock.deleteOnce("http://test.com", 200);
134fetchMock.head("http://test.com", 200);
135fetchMock.headOnce("http://test.com", 200);
136fetchMock.patch("http://test.com", 200);
137fetchMock.patchOnce("http://test.com", 200);
138
139fetchMock.get("http://test.com", 200, {method: "GET"});
140fetchMock.get("http://test.com", 200, {method: "GET", overwriteRoutes: true});
141fetchMock.get("http://test.com", 200, {overwriteRoutes: true});
142fetchMock.post("http://test.com", 200, {method: "POST"});
143fetchMock.put("http://test.com", 200, {method: "PUT"});
144fetchMock.delete("http://test.com", 200, {method: "DELETE"});
145fetchMock.head("http://test.com", 200, {method: "HEAD"});
146
147fetchMock
148 .mock("http://test.com", 200)
149 .catch(503);
150
151fetchMock
152 .mock("http://test.com", 200)
153 .spy();
154
155const myMatcher: fetchMock.MockMatcherFunction = (
156 url: string,
157 opts: fetchMock.MockRequest
158) => true;
159
160fetchMock.flush().then(resolved => resolved.forEach(console.log));
161fetchMock.flush().catch(r => r);
162fetchMock.flush(true).catch(r => r);
163
164fetchMock.get("http://test.com", {
165 body: 'abc',
166 includeContentLength: false
167});
168
169fetchMock.get("http://test.com", {
170 body: 'abc',
171 redirectUrl: "http://example.org"
172});
173
174const sandbox = fetchMock.sandbox();
175sandbox.get("http://test.com", {
176 body: 'abc',
177 redirectUrl: "http://example.org"
178});
179
180const response: fetchMock.MockResponseObject = {
181 throws: new Error('error'),
182};
183
184fetchMock.config.sendAsJson = true;
185fetchMock.config.includeContentLength = true;
186fetchMock.config.fallbackToNetwork = true;
187fetchMock.config.fallbackToNetwork = 'always';
188fetchMock.config.overwriteRoutes = true;
189fetchMock.config.overwriteRoutes = undefined;
190fetchMock.config.warnOnFallback = true;
191fetchMock.config.Promise = Promise;
192fetchMock.config.fetch = (): Promise<Response> => new Promise(() => { });
193fetchMock.config.Headers = Headers;
194fetchMock.config.Request = Request;
195fetchMock.config.Response = Response;