3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 import { setImmediate } from 'timers';
21 import { getIndexationStatus } from '../../../../api/ce';
22 import { get, remove, save } from '../../../../helpers/storage';
23 import { IndexationStatus } from '../../../../types/indexation';
24 import IndexationNotificationHelper from '../IndexationNotificationHelper';
32 jest.runOnlyPendingTimers();
36 jest.mock('../../../../api/ce', () => ({
37 getIndexationStatus: jest.fn().mockResolvedValue({}),
40 jest.mock('../../../../helpers/storage', () => ({
46 it('should properly start & stop polling for indexation status', async () => {
47 const onNewStatus = jest.fn();
48 const newStatus: IndexationStatus = {
50 percentCompleted: 100,
53 (getIndexationStatus as jest.Mock).mockResolvedValueOnce(newStatus);
55 IndexationNotificationHelper.startPolling(onNewStatus);
56 expect(getIndexationStatus).toHaveBeenCalled();
58 await new Promise(setImmediate);
59 expect(onNewStatus).toHaveBeenCalledWith(newStatus);
61 jest.runOnlyPendingTimers();
62 expect(getIndexationStatus).toHaveBeenCalledTimes(2);
64 (getIndexationStatus as jest.Mock).mockClear();
66 IndexationNotificationHelper.stopPolling();
69 expect(getIndexationStatus).not.toHaveBeenCalled();
72 it('should properly handle the flag to show the completed banner', () => {
73 IndexationNotificationHelper.markCompletedNotificationAsToDisplay();
75 expect(save).toHaveBeenCalledWith(expect.any(String), 'true');
77 (get as jest.Mock).mockReturnValueOnce('true');
78 let shouldDisplay = IndexationNotificationHelper.shouldDisplayCompletedNotification();
80 expect(shouldDisplay).toBe(true);
81 expect(get).toHaveBeenCalled();
83 IndexationNotificationHelper.markCompletedNotificationAsDisplayed();
85 expect(remove).toHaveBeenCalled();
87 shouldDisplay = IndexationNotificationHelper.shouldDisplayCompletedNotification();
89 expect(shouldDisplay).toBe(false);