3 * Copyright (C) 2009-2022 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 { getProjectBadgesToken } from '../../../../../../../api/project-badges';
23 import CodeSnippet from '../../../../../../../components/common/CodeSnippet';
24 import { mockBranch } from '../../../../../../../helpers/mocks/branch-like';
25 import { mockComponent } from '../../../../../../../helpers/mocks/component';
26 import { mockMetric } from '../../../../../../../helpers/testMocks';
27 import { waitAndUpdate } from '../../../../../../../helpers/testUtils';
28 import { Location } from '../../../../../../../helpers/urls';
29 import { ComponentQualifier } from '../../../../../../../types/component';
30 import { MetricKey } from '../../../../../../../types/metrics';
31 import BadgeButton from '../BadgeButton';
32 import ProjectBadges from '../ProjectBadges';
34 jest.mock('../../../../../../../helpers/urls', () => ({
35 getHostUrl: () => 'host',
36 getPathUrlAsString: (l: Location) => l.pathname,
37 getProjectUrl: () => ({ pathname: '/dashboard' } as Location)
40 jest.mock('../../../../../../../api/project-badges', () => ({
41 getProjectBadgesToken: jest.fn().mockResolvedValue('foo'),
42 renewProjectBadgesToken: jest.fn().mockResolvedValue({})
45 it('should display correctly', async () => {
46 const wrapper = shallowRender();
47 await waitAndUpdate(wrapper);
48 expect(wrapper).toMatchSnapshot();
51 it('should renew token', async () => {
52 (getProjectBadgesToken as jest.Mock).mockResolvedValueOnce('foo').mockResolvedValueOnce('bar');
53 const wrapper = shallowRender({
54 component: mockComponent({ configuration: { showSettings: true } })
56 await waitAndUpdate(wrapper);
57 wrapper.find('.it__project-info-renew-badge').simulate('click');
59 // it shoud be loading
60 expect(wrapper.find('.it__project-info-renew-badge').props().disabled).toBe(true);
62 await waitAndUpdate(wrapper);
63 const buttons = wrapper.find(BadgeButton);
64 expect(buttons.at(0).props().url).toMatch('token=bar');
65 expect(buttons.at(1).props().url).toMatch('token=bar');
66 expect(wrapper.find(CodeSnippet).props().snippet).toMatch('token=bar');
68 // let's check that the loading has correclty ends.
69 expect(wrapper.find('.it__project-info-renew-badge').props().disabled).toBe(false);
72 function shallowRender(overrides = {}) {
75 branchLike={mockBranch()}
77 [MetricKey.coverage]: mockMetric({ key: MetricKey.coverage }),
78 [MetricKey.new_code_smells]: mockMetric({ key: MetricKey.new_code_smells })
80 component={mockComponent({ key: 'foo', qualifier: ComponentQualifier.Project })}