-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ApiUtils.test.ts
122 lines (109 loc) · 3.82 KB
/
ApiUtils.test.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
import {
apiRequestInterceptor,
apiSuccessResponseInterceptor,
apiFailureResponseInterceptor,
axiosConnectionAbortedCode,
} from "./ApiUtils";
import type { AxiosRequestConfig, AxiosResponse } from "axios";
import type { ActionExecutionResponse } from "api/ActionAPI";
import {
createMessage,
ERROR_0,
SERVER_API_TIMEOUT_ERROR,
} from "@appsmith/constants/messages";
import { ERROR_CODES } from "@appsmith/constants/ApiConstants";
import * as Sentry from "@sentry/react";
describe("axios api interceptors", () => {
describe("Axios api request interceptor", () => {
it("adds timer to the request object", () => {
const request: AxiosRequestConfig = {
url: "https://app.appsmith.com/v1/api/actions/execute",
};
const interceptedRequest = apiRequestInterceptor(request);
expect(interceptedRequest).toHaveProperty("timer");
});
});
describe("Axios api response success interceptor", () => {
it("transforms an action execution response", () => {
const response: AxiosResponse = {
data: "Test data",
headers: {
"content-length": 123,
"content-type": "application/json",
},
config: {
url: "https://app.appsmith.com/v1/api/actions/execute",
// @ts-expect-error: type mismatch
timer: 0,
},
};
const interceptedResponse: ActionExecutionResponse =
apiSuccessResponseInterceptor(response);
expect(interceptedResponse).toHaveProperty("clientMeta");
expect(interceptedResponse.clientMeta).toHaveProperty("size");
expect(interceptedResponse.clientMeta.size).toBe(123);
expect(interceptedResponse.clientMeta).toHaveProperty("duration");
});
it("just returns the response data for other requests", () => {
const response: AxiosResponse = {
data: "Test data",
headers: {
"content-type": "application/json",
},
config: {
url: "https://app.appsmith.com/v1/api/actions",
//@ts-expect-error: type mismatch
timer: 0,
},
};
const interceptedResponse: ActionExecutionResponse =
apiSuccessResponseInterceptor(response);
expect(interceptedResponse).toBe("Test data");
});
});
describe("Api response failure interceptor", () => {
beforeEach(() => {
jest.restoreAllMocks();
});
it("checks for no internet errors", () => {
jest.spyOn(navigator, "onLine", "get").mockReturnValue(false);
const interceptedResponse = apiFailureResponseInterceptor({});
expect(interceptedResponse).rejects.toStrictEqual({
message: createMessage(ERROR_0),
});
});
it.todo("handles axios cancel gracefully");
it("handles timeout errors", () => {
const error = {
code: axiosConnectionAbortedCode,
message: "timeout of 10000ms exceeded",
};
const interceptedResponse = apiFailureResponseInterceptor(error);
expect(interceptedResponse).rejects.toStrictEqual({
message: createMessage(SERVER_API_TIMEOUT_ERROR),
code: ERROR_CODES.REQUEST_TIMEOUT,
});
});
it("checks for response meta", () => {
const sentrySpy = jest.spyOn(Sentry, "captureException");
const response: AxiosResponse = {
data: "Test data",
headers: {
"content-type": "application/json",
},
config: {
url: "https://app.appsmith.com/v1/api/user",
//@ts-expect-error: type mismatch
timer: 0,
},
};
apiSuccessResponseInterceptor(response);
expect(sentrySpy).toHaveBeenCalled();
const interceptedFailureResponse = apiFailureResponseInterceptor({
response,
});
expect(interceptedFailureResponse).rejects.toStrictEqual("Test data");
expect(sentrySpy).toHaveBeenCalled();
});
});
});