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 { shallow } from 'enzyme';
21 import * as React from 'react';
22 import { FormattedMessage } from 'react-intl';
23 import { Alert } from '../../../../../components/ui/Alert';
24 import { hasMessage } from '../../../../../helpers/l10n';
25 import { mockComponent } from '../../../../../helpers/mocks/component';
26 import { mockTask } from '../../../../../helpers/mocks/tasks';
27 import { mockLocation } from '../../../../../helpers/testMocks';
28 import { Task, TaskStatuses, TaskTypes } from '../../../../../types/tasks';
29 import { ComponentNavBgTaskNotif } from '../ComponentNavBgTaskNotif';
31 jest.mock('../../../../../helpers/l10n', () => ({
32 ...jest.requireActual('../../../../../helpers/l10n'),
33 hasMessage: jest.fn().mockReturnValue(true),
36 const UNKNOWN_TASK_TYPE: TaskTypes = 'UNKOWN' as TaskTypes;
38 it('renders correctly', () => {
39 expect(shallowRender()).toMatchSnapshot('default');
42 currentTask: mockTask({
43 status: TaskStatuses.Failed,
44 errorType: 'LICENSING',
48 ).toMatchSnapshot('license issue');
49 expect(shallowRender({ currentTask: undefined }).type()).toBeNull(); // No task.
55 'component_navigation.status.failed',
57 mockTask({ status: TaskStatuses.Failed, type: UNKNOWN_TASK_TYPE }),
64 'component_navigation.status.failed_X',
66 mockTask({ status: TaskStatuses.Failed }),
73 'component_navigation.status.failed.admin.link',
75 mockTask({ status: TaskStatuses.Failed, type: UNKNOWN_TASK_TYPE }),
82 'component_navigation.status.failed_X.admin.link',
84 mockTask({ status: TaskStatuses.Failed }),
91 'component_navigation.status.failed.admin.help',
93 mockTask({ status: TaskStatuses.Failed, type: UNKNOWN_TASK_TYPE }),
100 'component_navigation.status.failed_X.admin.help',
102 mockTask({ status: TaskStatuses.Failed }),
110 'component_navigation.status.failed_branch',
112 mockTask({ status: TaskStatuses.Failed, branch: 'foo', type: UNKNOWN_TASK_TYPE }),
119 'component_navigation.status.failed_branch_X',
121 mockTask({ status: TaskStatuses.Failed, branch: 'foo' }),
128 'component_navigation.status.failed_branch.admin.link',
130 mockTask({ status: TaskStatuses.Failed, branch: 'foo', type: UNKNOWN_TASK_TYPE }),
137 'component_navigation.status.failed_branch_X.admin.link',
139 mockTask({ status: TaskStatuses.Failed, branch: 'foo' }),
146 'component_navigation.status.failed_branch.admin.help',
148 mockTask({ status: TaskStatuses.Failed, branch: 'foo', type: UNKNOWN_TASK_TYPE }),
155 'component_navigation.status.failed_branch_X.admin.help',
157 mockTask({ status: TaskStatuses.Failed, branch: 'foo' }),
165 'component_navigation.status.pending',
167 mockTask({ type: UNKNOWN_TASK_TYPE }),
173 ['component_navigation.status.pending_X', 'info', mockTask(), true, false, false, false],
175 'component_navigation.status.pending.admin.link',
177 mockTask({ type: UNKNOWN_TASK_TYPE }),
184 'component_navigation.status.pending_X.admin.link',
193 'component_navigation.status.pending.admin.help',
195 mockTask({ type: UNKNOWN_TASK_TYPE }),
202 'component_navigation.status.pending_X.admin.help',
204 mockTask({ status: TaskStatuses.Failed }),
212 'component_navigation.status.in_progress',
214 mockTask({ type: UNKNOWN_TASK_TYPE }),
220 ['component_navigation.status.in_progress_X', 'info', mockTask(), true, true, false, false],
222 'component_navigation.status.in_progress.admin.link',
224 mockTask({ type: UNKNOWN_TASK_TYPE }),
231 'component_navigation.status.in_progress_X.admin.link',
240 'component_navigation.status.in_progress.admin.help',
242 mockTask({ type: UNKNOWN_TASK_TYPE }),
249 'component_navigation.status.in_progress_X.admin.help',
251 mockTask({ status: TaskStatuses.Failed }),
258 'should render the expected message=%p',
260 expectedMessage: string,
261 alertVariant: string,
264 isInProgress: boolean,
265 showBackgroundTasks: boolean,
266 onBackgroudTaskPage: boolean
268 if (currentTask.type === UNKNOWN_TASK_TYPE) {
269 (hasMessage as jest.Mock).mockReturnValueOnce(false);
272 const wrapper = shallowRender({
273 component: mockComponent({ configuration: { showBackgroundTasks } }),
275 currentTaskOnSameBranch: !currentTask.branch,
278 location: mockLocation({
279 pathname: onBackgroudTaskPage ? '/project/background_tasks' : '/foo/bar',
282 const messageProps = wrapper.find<FormattedMessage>(FormattedMessage).props();
285 expect(messageProps.defaultMessage).toBe(expectedMessage);
288 expect(wrapper.find(Alert).props().variant).toBe(alertVariant);
290 // Formatted message values prop.
291 // eslint-disable-next-line jest/no-conditional-in-test
292 if (/_X/.test(expectedMessage)) {
293 // eslint-disable-next-line jest/no-conditional-expect
294 expect(messageProps.values?.type).toBe(`background_task.type.${currentTask.type}`);
296 // eslint-disable-next-line jest/no-conditional-expect
297 expect(messageProps.values?.type).toBeUndefined();
300 // eslint-disable-next-line jest/no-conditional-in-test
301 if (currentTask.branch) {
302 // eslint-disable-next-line jest/no-conditional-expect
303 expect(messageProps.values?.branch).toBe(currentTask.branch);
305 // eslint-disable-next-line jest/no-conditional-expect
306 expect(messageProps.values?.branch).toBeUndefined();
309 // eslint-disable-next-line jest/no-conditional-in-test
310 if (showBackgroundTasks) {
311 // eslint-disable-next-line jest/no-conditional-in-test
312 if (onBackgroudTaskPage) {
313 // eslint-disable-next-line jest/no-conditional-expect
314 expect(messageProps.values?.url).toBeUndefined();
315 // eslint-disable-next-line jest/no-conditional-expect
316 expect(messageProps.values?.stacktrace).toBe('background_tasks.show_stacktrace');
318 // eslint-disable-next-line jest/no-conditional-expect
319 expect(messageProps.values?.url).toBeDefined();
320 // eslint-disable-next-line jest/no-conditional-expect
321 expect(messageProps.values?.stacktrace).toBeUndefined();
324 // eslint-disable-next-line jest/no-conditional-expect
325 expect(messageProps.values?.url).toBeUndefined();
326 // eslint-disable-next-line jest/no-conditional-expect
327 expect(messageProps.values?.stacktrace).toBeUndefined();
332 function shallowRender(props: Partial<ComponentNavBgTaskNotif['props']> = {}) {
333 return shallow<ComponentNavBgTaskNotif>(
334 <ComponentNavBgTaskNotif
335 component={mockComponent()}
336 currentTask={mockTask({ status: TaskStatuses.Failed })}
337 location={mockLocation()}