-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathaddon.test.ts
69 lines (59 loc) · 1.88 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
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
import nock from 'nock';
import noLogger from '../../test/fixtures/no-logger';
import SlackAddon from './slack';
import type { IAddonConfig, IFlagResolver } from '../types';
import type { IntegrationEventsService } from '../services';
import type EventEmitter from 'events';
beforeEach(() => {
nock.disableNetConnect();
});
const url = 'https://test.some.com';
const ARGS: IAddonConfig = {
getLogger: noLogger,
unleashUrl: url,
integrationEventsService: {} as IntegrationEventsService,
flagResolver: {} as IFlagResolver,
eventBus: {} as EventEmitter,
};
test('Does not retry if request succeeds', async () => {
const url = 'https://test.some.com';
const addon = new SlackAddon(ARGS);
nock(url).get('/').reply(201);
const res = await addon.fetchRetry(url);
expect(res.ok).toBe(true);
});
test('Retries once, and succeeds', async () => {
const addon = new SlackAddon(ARGS);
nock(url).get('/').replyWithError('testing retry');
nock(url).get('/').reply(200);
const res = await addon.fetchRetry(url);
expect(res.ok).toBe(true);
expect(nock.isDone()).toBe(true);
});
test('Does not throw if response is error', async () => {
const addon = new SlackAddon(ARGS);
nock(url).get('/').twice().replyWithError('testing retry');
const res = await addon.fetchRetry(url);
expect(res.ok).toBe(false);
});
test('Supports custom number of retries', async () => {
const addon = new SlackAddon(ARGS);
let retries = 0;
nock(url).get('/').twice().replyWithError('testing retry');
nock(url).get('/').reply(201);
const res = await addon.fetchRetry(
url,
{
onRetry: () => {
retries = retries + 1;
},
},
2,
);
expect(retries).toBe(2);
expect(res.ok).toBe(true);
expect(nock.isDone()).toBe(true);
});
afterEach(() => {
nock.enableNetConnect();
});