-
Notifications
You must be signed in to change notification settings - Fork 5
/
DopplerRestApiClientImpl.test.ts
207 lines (181 loc) · 5.35 KB
/
DopplerRestApiClientImpl.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
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
import { AppConfiguration } from "../abstractions";
import { AxiosStatic } from "axios";
import { DopplerRestApiClientImpl } from "./DopplerRestApiClientImpl";
import { AppSessionStateAccessor } from "../abstractions/app-session";
describe(DopplerRestApiClientImpl.name, () => {
describe("getFields", () => {
it("should request API with the right parameters and return API result as it is", async () => {
// Arrange
const jwtToken = "jwtToken";
const dopplerAccountName = "dopplerAccountName";
const dopplerRestApiBaseUrl = "dopplerRestApiBaseUrl";
const authenticatedSession = {
status: "authenticated",
jwtToken,
dopplerAccountName,
};
const appSessionStateAccessor = {
getCurrentSessionState: () => authenticatedSession,
} as AppSessionStateAccessor;
const field1Name = "field1";
const field1Predefined = true;
const field1Type = "boolean";
const field2Name = "field2";
const field2Predefined = true;
const field2Type = "string";
const apiResponse = {
items: [
{
name: field1Name,
predefined: field1Predefined,
private: true,
readonly: false,
type: field1Type,
sample: "string",
permissionHTML: "string",
_links: [
{
href: "string",
description: "string",
rel: "string",
},
],
},
{
name: field2Name,
predefined: field2Predefined,
private: true,
readonly: true,
type: field2Type,
sample: "string",
permissionHTML: "string",
_links: [
{
href: "string",
description: "string",
rel: "string",
},
],
},
],
_links: [
{
href: "string",
description: "string",
rel: "string",
},
],
};
const appConfiguration = {
dopplerRestApiBaseUrl: dopplerRestApiBaseUrl,
} as AppConfiguration;
const request = jest.fn(() =>
Promise.resolve({
data: apiResponse,
}),
);
const create = jest.fn(() => ({
request,
}));
const axiosStatic = {
create,
} as unknown as AxiosStatic;
const sut = new DopplerRestApiClientImpl({
axiosStatic,
appSessionStateAccessor,
appConfiguration,
});
// Act
const result = await sut.getFields();
// Assert
expect(create).toBeCalledWith({
baseURL: dopplerRestApiBaseUrl,
});
expect(request).toBeCalledWith({
headers: { Authorization: `Bearer ${jwtToken}` },
method: "GET",
url: `/accounts/${dopplerAccountName}/fields`,
});
expect(result).toEqual({
success: true,
value: [
{
name: field1Name,
predefined: field1Predefined,
type: field1Type,
},
{
name: field2Name,
predefined: field2Predefined,
type: field2Type,
},
],
});
});
it("should throw error result when an unexpected error occurs", async () => {
// Arrange
const error = new Error("Network error");
const appSessionStateAccessor = {
getCurrentSessionState: () => ({
status: "authenticated",
jwtToken: "jwtToken",
dopplerAccountName: "dopplerAccountName",
}),
} as AppSessionStateAccessor;
const appConfiguration = {
dopplerRestApiBaseUrl: "dopplerRestApiBaseUrl",
} as AppConfiguration;
const axiosStatic = {
create: () => ({
request: () => Promise.reject(error),
}),
} as unknown as AxiosStatic;
const sut = new DopplerRestApiClientImpl({
axiosStatic,
appSessionStateAccessor,
appConfiguration,
});
// Assert
await expect(async () => {
// Act
await sut.getFields();
}).rejects.toThrowError(error);
});
it.each([
{ sessionStatus: "non-authenticated" },
{ sessionStatus: "unknown" },
{ sessionStatus: "weird inexistent status" },
])(
"should throw error result when the session is not authenticated ($sessionStatus)",
async ({ sessionStatus }) => {
// Arrange
const appSessionStateAccessor = {
getCurrentSessionState: () => ({
status: sessionStatus,
}),
} as AppSessionStateAccessor;
const appConfiguration = {
dopplerRestApiBaseUrl: "dopplerRestApiBaseUrl",
} as AppConfiguration;
const request = jest.fn(() => {});
const axiosStatic = {
create: () => ({
request,
}),
} as unknown as AxiosStatic;
const sut = new DopplerRestApiClientImpl({
axiosStatic,
appSessionStateAccessor,
appConfiguration,
});
// Assert
await expect(async () => {
// Act
await sut.getFields();
}).rejects.toThrowError(new Error("Authenticated session required"));
// Assert
expect(request).not.toBeCalled();
},
);
});
});