3 * Copyright (C) 2009-2021 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 Toggler from 'sonar-ui-common/components/controls/Toggler';
23 import { click } from 'sonar-ui-common/helpers/testUtils';
24 import { mockSetOfBranchAndPullRequest } from '../../../../../../helpers/mocks/branch-like';
25 import { mockAppState, mockComponent } from '../../../../../../helpers/testMocks';
26 import { BranchLikeNavigation, BranchLikeNavigationProps } from '../BranchLikeNavigation';
28 it('should render correctly', () => {
29 const wrapper = shallowRender();
30 expect(wrapper).toMatchSnapshot();
33 it('should not render', () => {
34 // CE && main branch not analyzed yet
35 const wrapper = shallowRender({
36 appState: mockAppState({ branchesEnabled: false }),
37 component: mockComponent({ analysisDate: undefined })
39 expect(wrapper.type()).toBeNull();
41 // DE+ && main branch not analyzed yet && no other branches
42 const wrapper1 = shallowRender({
43 appState: mockAppState({ branchesEnabled: true }),
44 component: mockComponent({ analysisDate: undefined }),
47 expect(wrapper1.type()).toBeNull();
50 it('should render the menu trigger if branches are enabled', () => {
51 const wrapper = shallowRender({ appState: mockAppState({ branchesEnabled: true }) });
52 expect(wrapper).toMatchSnapshot();
55 it('should properly toggle menu opening when clicking the anchor', () => {
56 const wrapper = shallowRender({ appState: mockAppState({ branchesEnabled: true }) });
57 expect(wrapper.find(Toggler).props().open).toBe(false);
59 click(wrapper.find('a'));
60 expect(wrapper.find(Toggler).props().open).toBe(true);
62 click(wrapper.find('a'));
63 expect(wrapper.find(Toggler).props().open).toBe(false);
66 it('should properly close menu when toggler asks for', () => {
67 const wrapper = shallowRender({ appState: mockAppState({ branchesEnabled: true }) });
68 expect(wrapper.find(Toggler).props().open).toBe(false);
70 click(wrapper.find('a'));
71 expect(wrapper.find(Toggler).props().open).toBe(true);
77 expect(wrapper.find(Toggler).props().open).toBe(false);
80 function shallowRender(props?: Partial<BranchLikeNavigationProps>) {
81 const branchLikes = mockSetOfBranchAndPullRequest();
85 appState={mockAppState()}
86 branchLikes={branchLikes}
87 component={mockComponent({ analysisDate: '2021-01-01 01:01:01' })}
88 currentBranchLike={branchLikes[0]}