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.
20 import { shallow } from 'enzyme';
21 import * as React from 'react';
22 import { get } from 'sonar-ui-common/helpers/storage';
23 import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
24 import { generateToken, getTokens } from '../../../../api/user-tokens';
25 import { mockComponent, mockLoggedInUser } from '../../../../helpers/testMocks';
26 import { getUniqueTokenName } from '../../utils';
27 import AnalyzeTutorialSonarCloud from '../AnalyzeTutorialSonarCloud';
29 jest.mock('sonar-ui-common/helpers/storage', () => ({
35 jest.mock('../../../../api/alm-integration', () => ({
36 getGithubLanguages: jest.fn().mockResolvedValue({
46 jest.mock('../../../../api/user-tokens', () => ({
47 generateToken: jest.fn().mockResolvedValue({
49 createdAt: '2019-01-21T08:06:00+0100',
53 getTokens: jest.fn().mockResolvedValue([
56 createdAt: '2019-01-15T15:06:33+0100',
57 lastConnectionDate: '2019-01-18T15:06:33+0100'
59 { name: 'bar', createdAt: '2019-01-18T15:06:33+0100' }
61 revokeToken: jest.fn().mockResolvedValue(Promise.resolve())
64 jest.mock('../../utils', () => ({
65 getUniqueTokenName: jest.fn().mockReturnValue('lightsaber-9000')
68 const component = mockComponent();
76 alm: { key: 'github', url: 'https://github.com/luke/lightsaber' }
79 it('shows a loading screen', () => {
80 const wrapper = shallowRender({ component: compGitHub });
81 expect(wrapper.find('DeferredSpinner').prop('loading') as boolean).toBe(true);
84 it('renders for GitHub', async () => {
85 const wrapper = shallowRender({ component: compGitHub });
86 await waitAndUpdate(wrapper);
88 expect(wrapper).toMatchSnapshot();
94 expect(wrapper.state('mode')).toEqual({
96 name: 'SonarCloud Automatic Analysis'
100 it('renders for BitBucket', () => {
103 alm: { key: 'bitbucket', url: 'https://bitbucket.com/luke/lightsaber' }
105 const wrapper = shallowRender({ component: comp });
107 expect(wrapper).toMatchSnapshot();
109 (wrapper.find('TokenStep').prop('onContinue') as Function)('abc123');
111 expect(wrapper.state('token')).toBe('abc123');
112 expect(wrapper.state('step')).toBe('ANALYSIS');
115 it('renders for Azure', () => {
118 alm: { key: 'microsoft', url: 'https://azuredevops.com/luke/lightsaber' }
120 expect(shallowRender({ component: comp })).toMatchSnapshot();
123 it('renders for a manual project', () => {
124 expect(shallowRender()).toMatchSnapshot();
127 it('renders the finished state', async () => {
128 (get as jest.Mock).mockReturnValue('true');
132 alm: { key: 'github', url: 'https://github.com/luke/lightsaber' }
134 const wrapper = shallowRender({ component: comp });
135 await waitAndUpdate(wrapper);
136 expect(wrapper.find('AnalyzeTutorialDone').exists()).toBe(true);
139 it('should get tokens and unique name', async () => {
140 const wrapper = shallowRender();
141 await waitAndUpdate(wrapper);
143 expect(getTokens).toHaveBeenCalled();
144 expect(getUniqueTokenName).toHaveBeenCalled();
145 expect(generateToken).toHaveBeenCalled();
146 expect(wrapper.state('token')).toBe('token_value');
149 it('should set tutorial done', () => {
150 const wrapper = shallowRender();
151 const instance = wrapper.instance();
153 instance.setTutorialDone(false);
154 expect(wrapper.state('isTutorialDone')).toBe(false);
156 instance.setTutorialDone(true);
157 expect(wrapper.state('isTutorialDone')).toBe(true);
160 it('should have a spinner', () => {
161 const wrapper = shallowRender();
162 const instance = wrapper.instance();
163 expect(instance.spinner()).toMatchSnapshot();
166 function shallowRender(props: Partial<AnalyzeTutorialSonarCloud['props']> = {}) {
167 return shallow<AnalyzeTutorialSonarCloud>(
168 <AnalyzeTutorialSonarCloud
169 component={component}
170 currentUser={mockLoggedInUser({ externalProvider: 'github' })}