Skip to content

Commit 04448c0

Browse files
improved code coverage by adding test case
1 parent c7e30e3 commit 04448c0

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

test/request.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,68 @@ describe('Request tests', () => {
3737
};
3838
await expect(getData(client, url, {})).rejects.toThrowError('Host is required for live preview');
3939
});
40+
41+
it('should handle live_preview with enable=true and live_preview=init', async () => {
42+
const client = httpClient({});
43+
const mock = new MockAdapter(client as any);
44+
const url = '/your-api-endpoint';
45+
const mockResponse = { data: 'mocked' };
46+
47+
client.stackConfig = {
48+
live_preview: {
49+
enable: true,
50+
preview_token: 'someToken',
51+
live_preview: 'init',
52+
},
53+
};
54+
55+
mock.onGet(url).reply(200, mockResponse);
56+
57+
const result = await getData(client, url, {});
58+
expect(result).toEqual(mockResponse);
59+
});
60+
61+
it('should set baseURL correctly when host is provided without https://', async () => {
62+
const client = httpClient({});
63+
const mock = new MockAdapter(client as any);
64+
const url = '/your-api-endpoint';
65+
const mockResponse = { data: 'mocked' };
66+
67+
client.stackConfig = {
68+
live_preview: {
69+
enable: true,
70+
preview_token: 'someToken',
71+
live_preview: 'someHash',
72+
host: 'example.com',
73+
},
74+
};
75+
76+
mock.onGet(url).reply(200, mockResponse);
77+
78+
const result = await getData(client, url, {});
79+
expect(client.defaults.baseURL).toBe('https://example.com');
80+
expect(result).toEqual(mockResponse);
81+
});
82+
83+
it('should not modify baseURL when host is already prefixed with https://', async () => {
84+
const client = httpClient({});
85+
const mock = new MockAdapter(client as any);
86+
const url = '/your-api-endpoint';
87+
const mockResponse = { data: 'mocked' };
88+
89+
client.stackConfig = {
90+
live_preview: {
91+
enable: true,
92+
preview_token: 'someToken',
93+
live_preview: 'someHash',
94+
host: 'https://example.com',
95+
},
96+
};
97+
98+
mock.onGet(url).reply(200, mockResponse);
99+
100+
const result = await getData(client, url, {});
101+
expect(client.stackConfig.live_preview.host).toBe('https://example.com');
102+
expect(result).toEqual(mockResponse);
103+
});
40104
});

test/retryPolicy/delivery-sdk-handlers.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,34 @@ describe('retryResponseErrorHandler', () => {
183183

184184
await expect(retryResponseErrorHandler(error, config, client)).rejects.toBe(error);
185185
});
186+
187+
it('should retry when response status is 429 and retryCount is less than retryLimit', async () => {
188+
const error = {
189+
config: { retryOnError: true, retryCount: 1 },
190+
response: { status: 429, statusText: 'Rate limit exceeded' },
191+
};
192+
const config = { retryLimit: 3 };
193+
const client = axios.create();
194+
195+
mock.onAny().reply(200);
196+
197+
const response: any = await retryResponseErrorHandler(error, config, client);
198+
expect(response.status).toBe(200);
199+
});
200+
201+
it('should retry when retryCondition is true', async () => {
202+
const error = {
203+
config: { retryOnError: true, retryCount: 1 },
204+
response: { status: 500, statusText: 'Internal Server Error' },
205+
};
206+
const retryCondition = jest.fn().mockReturnValue(true);
207+
const config = { retryLimit: 3, retryCondition, retryDelay: 100 };
208+
const client = axios.create();
209+
210+
mock.onAny().reply(200);
211+
212+
const response: any = await retryResponseErrorHandler(error, config, client);
213+
expect(response.status).toBe(200);
214+
expect(retryCondition).toHaveBeenCalledWith(error);
215+
});
186216
});

0 commit comments

Comments
 (0)