aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ui-common/components/controls/__tests__/Dropdown-test.tsx
blob: 0595d595a27ff02f897048c4137e36871e9c0504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Sonar UI Common
 * Copyright (C) 2019-2020 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { mount, shallow, ShallowWrapper } from 'enzyme';
import * as React from 'react';
import { click } from '../../../helpers/testUtils';
import { Popup, PopupPlacement } from '../../ui/popups';
import { Button } from '../buttons';
import Dropdown, { DropdownOverlay } from '../Dropdown';
import ScreenPositionFixer from '../ScreenPositionFixer';

describe('Dropdown', () => {
  it('renders', () => {
    expect(
      shallow(<Dropdown overlay={<div id="overlay" />}>{() => <div />}</Dropdown>)
        .find('div')
        .exists()
    ).toBe(true);
  });

  it('toggles with element child', () => {
    checkToggle(
      shallow(
        <Dropdown overlay={<div id="overlay" />}>
          <Button />
        </Dropdown>
      )
    );

    checkToggle(
      shallow(
        <Dropdown overlay={<div id="overlay" />}>
          <a href="#">click me!</a>
        </Dropdown>
      ),
      'a'
    );
  });

  it('toggles with render prop', () => {
    checkToggle(
      shallow(
        <Dropdown overlay={<div id="overlay" />}>
          {({ onToggleClick }) => <Button onClick={onToggleClick} />}
        </Dropdown>
      )
    );
  });

  it('should call onOpen', () => {
    const onOpen = jest.fn();
    const wrapper = mount(
      <Dropdown onOpen={onOpen} overlay={<div id="overlay" />}>
        <Button />
      </Dropdown>
    );
    expect(onOpen).not.toBeCalled();
    click(wrapper.find('Button'));
    expect(onOpen).toBeCalled();
  });

  function checkToggle(wrapper: ShallowWrapper, selector = 'Button') {
    expect(wrapper.state()).toEqual({ open: false });

    click(wrapper.find(selector));
    expect(wrapper.state()).toEqual({ open: true });

    click(wrapper.find(selector));
    expect(wrapper.state()).toEqual({ open: false });
  }
});

describe('DropdownOverlay', () => {
  it('should render overlay with screen fixer', () => {
    const wrapper = shallow(
      <DropdownOverlay>
        <div />
      </DropdownOverlay>,
      // disable ScreenPositionFixer positioning
      { disableLifecycleMethods: true }
    );

    expect(wrapper.is(ScreenPositionFixer)).toBe(true);
    expect(wrapper.dive().dive().dive().is(Popup)).toBe(true);
  });

  it('should render overlay without screen fixer', () => {
    const wrapper = shallow(
      <DropdownOverlay placement={PopupPlacement.BottomRight}>
        <div />
      </DropdownOverlay>
    );
    expect(wrapper.is('Popup')).toBe(true);
  });
});