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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
import MockAdapter from 'axios-mock-adapter';
import Visibility from 'visibilityjs';
import createFlash from '~/flash';
import { STATUSES } from '~/import_entities/constants';
import { SourceGroupsManager } from '~/import_entities/import_groups/graphql/services/source_groups_manager';
import { StatusPoller } from '~/import_entities/import_groups/graphql/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');
jest.mock('~/import_entities/import_groups/graphql/services/source_groups_manager', () => ({
SourceGroupsManager: jest.fn().mockImplementation(function mock() {
this.setImportStatus = jest.fn();
this.findByImportId = jest.fn();
}),
}));
const FAKE_POLL_PATH = '/fake/poll/path';
const CLIENT_MOCK = {};
describe('Bulk import status poller', () => {
let poller;
let mockAdapter;
const getPollHistory = () => mockAdapter.history.get.filter((x) => x.url === FAKE_POLL_PATH);
beforeEach(() => {
mockAdapter = new MockAdapter(axios);
mockAdapter.onGet(FAKE_POLL_PATH).reply(200, {});
poller = new StatusPoller({ client: CLIENT_MOCK, pollPath: FAKE_POLL_PATH });
});
it('creates source group manager with proper client', () => {
expect(SourceGroupsManager.mock.calls).toHaveLength(1);
const [[{ client }]] = SourceGroupsManager.mock.calls;
expect(client).toBe(CLIENT_MOCK);
});
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();
expect(pollInstance.makeRequest).toHaveBeenCalled();
});
it('when error occurs shows flash with error', () => {
const [[pollConfig]] = Poll.mock.calls;
pollConfig.errorCallback();
expect(createFlash).toHaveBeenCalled();
});
it('when success response arrives updates relevant group status', () => {
const FAKE_ID = 5;
const [[pollConfig]] = Poll.mock.calls;
const [managerInstance] = SourceGroupsManager.mock.instances;
managerInstance.findByImportId.mockReturnValue({ id: FAKE_ID });
pollConfig.successCallback({ data: [{ id: FAKE_ID, status_name: STATUSES.FINISHED }] });
expect(managerInstance.setImportStatus).toHaveBeenCalledWith(
expect.objectContaining({ id: FAKE_ID }),
STATUSES.FINISHED,
);
});
});
|