-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
axios.spec.ts
230 lines (192 loc) · 5.55 KB
/
axios.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import { useAxios } from "../src";
import { promisedTimeout } from "@vue-composable/core";
jest.mock("axios");
const __axios = jest.requireActual("axios");
describe("axios", () => {
let request = jest.fn();
beforeEach(() => {
axios.create = jest.fn(() => ({
request
})) as any;
axios.CancelToken = __axios.CancelToken;
});
it("should create axios client", () => {
const options: AxiosRequestConfig = {
baseURL: "./api"
};
useAxios(options);
expect(axios.create).toBeCalledWith(options);
});
it("should call axios", async () => {
const { exec, client } = useAxios(false);
const request: AxiosRequestConfig = {
method: "GET",
url: "./api/1"
};
await exec(request);
expect(client.value.request).toBeCalledWith(
expect.objectContaining(request)
);
});
it("should call axios using string and options", async () => {
const url = "./api/1";
const request: AxiosRequestConfig = {
method: "GET"
};
const { exec, client } = useAxios(url, request);
await exec(url);
expect(client.value.request).toBeCalledWith(
expect.objectContaining({ url, ...request })
);
});
it("should call axios using string", async () => {
const { exec, client } = useAxios();
const url = "./api/1";
await exec(url);
expect(client.value.request).toBeCalledWith(
expect.objectContaining({ url })
);
});
it("should set response", async () => {
const { exec, client, result, data, status, statusText } = useAxios();
const request: AxiosRequestConfig = {
method: "GET",
url: "./api/1"
};
const response: AxiosResponse = {
data: {
test: 1
},
status: 200,
statusText: "OK",
headers: {},
config: {}
};
(client.value.request as jest.Mock).mockImplementationOnce(() => response);
const r = await exec(request);
expect(r).toBe(response);
expect(result.value).toBe(response);
expect(data.value).toBe(response.data);
expect(status.value).toBe(response.status);
expect(statusText.value).toBe(response.statusText);
});
it("should set data, status and statusText on error request", async () => {
const { exec, client, error, data, status, statusText } = useAxios();
const request: AxiosRequestConfig = {
method: "GET",
url: "./api/1"
};
const response: AxiosResponse = {
data: {
test: 1
},
status: 200,
statusText: "OK",
headers: {},
config: {}
};
(client.value.request as jest.Mock).mockImplementationOnce(() => {
throw {
response
};
});
await exec(request);
expect(error.value).toMatchObject({ response });
expect(data.value).toBe(response.data);
expect(status.value).toBe(response.status);
expect(statusText.value).toBe(response.statusText);
});
it("should set set data, status and statusText to null, if Error has been thrown", async () => {
const { exec, client, data, status, statusText } = useAxios();
const request: AxiosRequestConfig = {
method: "GET",
url: "./api/1"
};
(client.value.request as jest.Mock).mockImplementationOnce(() => {
throw new Error("invalid");
});
await exec(request);
expect(data.value).toBeNull();
expect(status.value).toBeNull();
expect(statusText.value).toBeNull();
});
it("should get null on status if exec not been called", () => {
const { result, data, status, statusText } = useAxios();
expect(result.value).toBeNull();
expect(data.value).toBeNull();
expect(status.value).toBeNull();
expect(statusText.value).toBeNull();
});
it("should cancel the request", async () => {
const {
exec,
error,
client,
cancel,
cancelledMessage,
isCancelled
} = useAxios();
const message = "cancelled ";
(client.value.request as jest.Mock).mockImplementationOnce(
async (x: AxiosRequestConfig) => {
expect(x).toBeDefined();
expect(x.cancelToken).toBeDefined();
const r = await Promise.race([
promisedTimeout(5000),
x.cancelToken!.promise
]);
x.cancelToken!.throwIfRequested();
return r;
}
);
try {
const execPromise = exec({});
cancel(message);
await execPromise;
} catch (e) {
expect(e).toMatchObject({ rr: 1 });
}
expect({
cancelledMessage,
isCancelled,
error
}).toMatchObject({
cancelledMessage: {
value: message
},
isCancelled: {
value: true
},
error: {
value: {
message
}
}
});
});
it("should execute request if request is passed", () => {
const req: Partial<AxiosRequestConfig> = {
url: "./api/1"
};
useAxios(req, false);
expect(request).toBeCalledWith(expect.objectContaining(req));
});
it("should execute request if url is passed", () => {
const req = "./api/1";
useAxios(req);
expect(request).toBeCalledWith(expect.objectContaining({ url: req }));
});
it("should warn if cancel is called before any request has been made", () => {
const { cancel } = useAxios();
expect(cancel).toThrowError(
"Cannot cancel because no request has been made"
);
});
it("should warn if cancel is called before any request has been made", () => {
const { cancel } = useAxios(true);
expect(cancel).toThrowError(
"Cannot cancel because no request has been made"
);
});
});