-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentstack-core.spec.ts
185 lines (146 loc) · 5.03 KB
/
contentstack-core.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
import { AxiosInstance } from '../src';
import { httpClient } from '../src/lib/contentstack-core';
import MockAdapter from 'axios-mock-adapter';
describe('contentstackCore', () => {
it('should return default config when no config is passed', (done) => {
const client = httpClient({});
done();
});
describe('logHandler', () => {
it('should log an error message when level is "error" and data is provided', () => {
const error = {
name: 'Error',
message: 'Something went wrong',
};
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
httpClient({}).defaults.logHandler('error', error);
expect(consoleErrorSpy).toHaveBeenCalledWith('[error] Error - Something went wrong');
consoleErrorSpy.mockRestore();
});
it('should not log anything when level is "error" and no data is provided', () => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
httpClient({}).defaults.logHandler('error');
expect(consoleErrorSpy).not.toHaveBeenCalled();
consoleErrorSpy.mockRestore();
});
it('should log a message with the provided level and data', () => {
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
httpClient({}).defaults.logHandler('info', 'Some message');
expect(consoleLogSpy).toHaveBeenCalledWith('[info] Some message');
consoleLogSpy.mockRestore();
});
});
describe('retryCondition', () => {
it('should return true when error response status is 429', () => {
const error = {
response: {
status: 429,
},
};
const shouldRetry = httpClient({}).defaults.retryCondition(error);
expect(shouldRetry).toBe(true);
});
it('should return false when error response status is not 429', () => {
const error = {
response: {
status: 400,
},
};
const shouldRetry = httpClient({}).defaults.retryCondition(error);
expect(shouldRetry).toBe(false);
});
it('should return false when error response is not present', () => {
const error = {};
const shouldRetry = httpClient({}).defaults.retryCondition(error);
expect(shouldRetry).toBe(false);
});
});
describe('config.headers', () => {
it('should include apiKey in headers when provided', () => {
const options = {
apiKey: 'my-api-key',
accessToken: 'my-access-token',
insecure: true,
defaultHostname: 'example.com',
port: 443,
endpoint: 'https://example.com/api',
basePath: '/v1',
};
const instance = httpClient(options);
expect(instance.defaults.headers).toEqual(
expect.objectContaining({
api_key: 'my-api-key',
access_token: 'my-access-token',
})
);
});
it('should include accessToken in headers when provided', () => {
const options = {
apiKey: 'my-api-key',
accessToken: 'my-access-token',
insecure: false,
defaultHostname: 'example.com',
port: 443,
endpoint: 'https://example.com/api',
basePath: '/v1',
};
const instance = httpClient(options);
expect(instance.defaults.headers).toEqual(
expect.objectContaining({
api_key: 'my-api-key',
access_token: 'my-access-token',
})
);
});
it('should not include apiKey in headers when not provided', () => {
const options = {
insecure: false,
defaultHostname: 'example.com',
port: 443,
endpoint: 'https://example.com/api',
basePath: '/v1',
};
const instance = httpClient(options);
expect(instance.defaults.headers.api_key).toBeUndefined();
});
it('should not include accessToken in headers when not provided', () => {
const options = {
insecure: false,
defaultHostname: 'example.com',
port: 443,
endpoint: 'https://example.com/api',
basePath: '/v1',
};
const instance = httpClient(options);
expect(instance.defaults.headers.access_token).toBeUndefined();
});
});
describe('config.onError', () => {
it('should call the onError function when an error occurs', async () => {
const onError = jest.fn();
const options = {
onError,
};
const instance = httpClient(options);
try {
await instance.get('/');
} catch (error: unknown) {
expect(onError).toBeCalledWith(error);
}
});
it('should not call the onError function when no error occurs', async () => {
const HOST_URL = 'cdn.contentstack.io';
const client: AxiosInstance = httpClient({
defaultHostname: HOST_URL,
params: { environment: 'env' },
onError: jest.fn(),
});
const mockClient = new MockAdapter(client);
mockClient.onGet('/').reply(200, {
data: 'Hello, World!',
});
await client.get('/');
expect(client.httpClientParams.onError).not.toBeCalled();
});
});
});