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
|
import MockAdapter from 'axios-mock-adapter';
import Visibility from 'visibilityjs';
import { createAlert } from '~/flash';
import { STATUSES } from '~/import_entities/constants';
import { StatusPoller } from '~/import_entities/import_groups/services/status_poller';
import axios from '~/lib/utils/axios_utils';
import Poll from '~/lib/utils/poll';
jest.mock('visibilityjs');
jest.mock('~/flash');
jest.mock('~/lib/utils/poll');
const FAKE_POLL_PATH = '/fake/poll/path';
describe('Bulk import status poller', () => {
let poller;
let mockAdapter;
let updateImportStatus;
const getPollHistory = () => mockAdapter.history.get.filter((x) => x.url === FAKE_POLL_PATH);
beforeEach(() => {
mockAdapter = new MockAdapter(axios);
mockAdapter.onGet(FAKE_POLL_PATH).reply(200, {});
updateImportStatus = jest.fn();
poller = new StatusPoller({ updateImportStatus, pollPath: FAKE_POLL_PATH });
});
it('creates poller with proper config', () => {
expect(Poll.mock.calls).toHaveLength(1);
const [[pollConfig]] = Poll.mock.calls;
expect(typeof pollConfig.method).toBe('string');
const pollOperation = pollConfig.resource[pollConfig.method];
expect(typeof pollOperation).toBe('function');
});
it('invokes axios when polling is performed', async () => {
const [[pollConfig]] = Poll.mock.calls;
const pollOperation = pollConfig.resource[pollConfig.method];
expect(getPollHistory()).toHaveLength(0);
pollOperation();
await axios.waitForAll();
expect(getPollHistory()).toHaveLength(1);
});
it('subscribes to visibility changes', () => {
expect(Visibility.change).toHaveBeenCalled();
});
it.each`
isHidden | action
${true} | ${'stop'}
${false} | ${'restart'}
`('$action polling when hidden is $isHidden', ({ action, isHidden }) => {
const [pollInstance] = Poll.mock.instances;
const [[changeHandler]] = Visibility.change.mock.calls;
Visibility.hidden.mockReturnValue(isHidden);
expect(pollInstance[action]).not.toHaveBeenCalled();
changeHandler();
expect(pollInstance[action]).toHaveBeenCalled();
});
it('does not perform polling when constructed', async () => {
await axios.waitForAll();
expect(getPollHistory()).toHaveLength(0);
});
it('immediately start polling when requested', async () => {
const [pollInstance] = Poll.mock.instances;
poller.startPolling();
await Promise.resolve();
expect(pollInstance.makeRequest).toHaveBeenCalled();
});
it('when error occurs shows flash with error', () => {
const [[pollConfig]] = Poll.mock.calls;
pollConfig.errorCallback();
expect(createAlert).toHaveBeenCalled();
});
it('when success response arrives updates relevant group status', () => {
const FAKE_ID = 5;
const [[pollConfig]] = Poll.mock.calls;
const FAKE_RESPONSE = { id: FAKE_ID, status_name: STATUSES.FINISHED };
pollConfig.successCallback({ data: [FAKE_RESPONSE] });
expect(updateImportStatus).toHaveBeenCalledWith(FAKE_RESPONSE);
});
});
|