-
Notifications
You must be signed in to change notification settings - Fork 576
/
Copy pathapi.test.js
148 lines (132 loc) · 3.78 KB
/
api.test.js
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
/* @flow */
import { describe, expect, vi } from "vitest";
import { request } from "@krakenjs/belter/src";
import { callRestAPI } from "../lib";
import { HEADERS } from "../constants/api";
import { RestClient, callGraphQLAPI, HTTPClient } from "./api";
vi.mock("@krakenjs/belter/src", async () => {
return {
...(await vi.importActual("@krakenjs/belter/src")),
request: vi.fn(),
};
});
vi.mock("@paypal/sdk-client/src", async () => {
return {
...(await vi.importActual("@paypal/sdk-client/src")),
getSessionID: () => "session_id_123",
getPartnerAttributionID: () => "partner_attr_123",
};
});
vi.mock("../lib", () => ({
callRestAPI: vi.fn(),
}));
describe("API", () => {
const accessToken = "access_token";
const baseURL = "http://test.paypal.com:port";
afterEach(() => {
vi.clearAllMocks();
});
describe("HTTPClient", () => {
it("should set access token and base url in constructor", () => {
const client = new HTTPClient({ accessToken, baseURL });
expect(client.accessToken).toBe(accessToken);
expect(client.baseURL).toBe(baseURL);
});
it("should set access token", () => {
const client = new HTTPClient();
client.setAccessToken(accessToken);
expect(client.accessToken).toBe(accessToken);
});
});
describe("RestClient", () => {
it("should make a REST API call with correct params", () => {
const data = { test: "data" };
const requestOptions = {
data,
baseURL,
};
const client = new RestClient({ accessToken });
client.request(requestOptions);
expect(callRestAPI).toHaveBeenCalledWith({
accessToken,
data,
url: baseURL,
});
});
});
describe("callGraphQLAPI", () => {
const query = '{ "test": "data" }';
const variables = { option: "param1" };
const gqlQuery = { query, variables };
const response = { data: { test: "data" } };
it("should throw error if no access token is provided", () => {
expect(() =>
callGraphQLAPI({
accessToken: null,
baseURL,
data: gqlQuery,
headers: {},
})
).toThrowError(
new Error(
`No access token passed to GraphQL request ${baseURL}/graphql`
)
);
});
it("should make a GraphQL API call with correct params", () => {
vi.mocked(request).mockResolvedValue({
status: 200,
body: response,
});
callGraphQLAPI({
accessToken,
baseURL,
data: gqlQuery,
headers: {},
});
expect(request).toHaveBeenCalledWith({
method: "post",
url: `${baseURL}/graphql`,
headers: {
[HEADERS.AUTHORIZATION]: `Bearer ${accessToken}`,
[HEADERS.CONTENT_TYPE]: "application/json",
[HEADERS.PARTNER_ATTRIBUTION_ID]: "partner_attr_123",
[HEADERS.CLIENT_METADATA_ID]: "session_id_123",
},
json: gqlQuery,
});
});
it("should resolve with response body on success", async () => {
vi.mocked(request).mockResolvedValue({
status: 200,
body: response,
});
const resp = await callGraphQLAPI({
accessToken,
baseURL,
data: gqlQuery,
headers: {},
});
expect(resp).toEqual(response);
});
it("should throw error on error status", async () => {
const status = 400;
vi.mocked(request).mockResolvedValue({
status,
body: { message: "Something went wrong" },
});
try {
await callGraphQLAPI({
accessToken,
baseURL,
data: gqlQuery,
headers: {},
});
} catch (error) {
expect(error.message).toBe(
`${baseURL}/graphql returned status ${status}`
);
}
});
});
});