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