2022-04-28 10:57:52 +02:00
|
|
|
import nock from 'nock';
|
2021-08-12 15:04:37 +02:00
|
|
|
import noLogger from '../../test/fixtures/no-logger';
|
|
|
|
|
|
|
|
import SlackAddon from './slack';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-04-28 10:57:52 +02:00
|
|
|
nock.disableNetConnect();
|
2021-08-12 15:04:37 +02:00
|
|
|
});
|
|
|
|
|
2022-04-28 10:57:52 +02:00
|
|
|
test('Does not retry if request succeeds', async () => {
|
2021-08-12 15:04:37 +02:00
|
|
|
const url = 'https://test.some.com';
|
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: url,
|
|
|
|
});
|
2022-04-28 10:57:52 +02:00
|
|
|
nock(url).get('/').reply(201);
|
|
|
|
const res = await addon.fetchRetry(url);
|
|
|
|
expect(res.ok).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Retries once, and succeeds', async () => {
|
|
|
|
const url = 'https://test.some.com';
|
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: url,
|
2021-08-12 15:04:37 +02:00
|
|
|
});
|
2022-04-28 10:57:52 +02:00
|
|
|
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);
|
2021-08-12 15:04:37 +02:00
|
|
|
});
|
|
|
|
|
2022-04-28 10:57:52 +02:00
|
|
|
test('Does not throw if response is error', async () => {
|
2021-08-12 15:04:37 +02:00
|
|
|
const url = 'https://test.some.com';
|
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: url,
|
|
|
|
});
|
2022-04-28 10:57:52 +02:00
|
|
|
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 url = 'https://test.some.com';
|
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: url,
|
2021-08-12 15:04:37 +02:00
|
|
|
});
|
2022-04-28 10:57:52 +02:00
|
|
|
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();
|
2021-08-12 15:04:37 +02:00
|
|
|
});
|