3 * Copyright (C) 2009-2020 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.
21 import { get, remove, save } from 'sonar-ui-common/helpers/storage';
22 import { getIndexationStatus } from '../../../../api/ce';
23 import { IndexationStatus } from '../../../../types/indexation';
24 import IndexationNotificationHelper from '../IndexationNotificationHelper';
31 jest.mock('../../../../api/ce', () => ({
32 getIndexationStatus: jest.fn()
35 jest.mock('sonar-ui-common/helpers/storage', () => ({
41 it('should properly start & stop polling for indexation status', async () => {
42 const onNewStatus = jest.fn();
43 const newStatus: IndexationStatus = {
45 percentCompleted: 100,
48 (getIndexationStatus as jest.Mock).mockResolvedValueOnce(newStatus);
50 IndexationNotificationHelper.startPolling(onNewStatus);
51 expect(getIndexationStatus).toHaveBeenCalled();
53 await new Promise(setImmediate);
54 expect(onNewStatus).toHaveBeenCalledWith(newStatus);
56 jest.runOnlyPendingTimers();
57 expect(getIndexationStatus).toHaveBeenCalledTimes(2);
59 (getIndexationStatus as jest.Mock).mockClear();
61 IndexationNotificationHelper.stopPolling();
64 expect(getIndexationStatus).not.toHaveBeenCalled();
67 it('should properly handle the flag to show the completed banner', () => {
68 IndexationNotificationHelper.markInProgressNotificationAsDisplayed();
70 expect(save).toHaveBeenCalledWith(expect.any(String), 'true');
72 (get as jest.Mock).mockReturnValueOnce('true');
73 let shouldDisplay = IndexationNotificationHelper.shouldDisplayCompletedNotification();
75 expect(shouldDisplay).toBe(true);
76 expect(get).toHaveBeenCalled();
78 IndexationNotificationHelper.markCompletedNotificationAsDismissed();
80 expect(remove).toHaveBeenCalled();
82 shouldDisplay = IndexationNotificationHelper.shouldDisplayCompletedNotification();
84 expect(shouldDisplay).toBe(false);