/
addon.test.ts
40 lines (36 loc) · 1.05 KB
/
addon.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
import fetchMock from 'jest-fetch-mock';
import noLogger from '../../test/fixtures/no-logger';
import SlackAddon from './slack';
beforeEach(() => {
fetchMock.resetMocks();
});
test('Retries if fetch throws', async () => {
const url = 'https://test.some.com';
jest.useFakeTimers('modern');
const addon = new SlackAddon({
getLogger: noLogger,
unleashUrl: url,
});
fetchMock.mockIf('https://test.some.com', 'OK', {
status: 201,
statusText: 'ACCEPTED',
});
await addon.fetchRetry(url);
jest.advanceTimersByTime(1000);
jest.useRealTimers();
});
test('does not crash even if fetch throws', async () => {
const url = 'https://test.some.com';
jest.useFakeTimers('modern');
const addon = new SlackAddon({
getLogger: noLogger,
unleashUrl: url,
});
fetchMock.mockResponse(() => {
throw new Error('Network error');
});
await addon.fetchRetry(url);
jest.advanceTimersByTime(1000);
expect(fetchMock.mock.calls).toHaveLength(2);
jest.useRealTimers();
});