aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ui-common/components/controls/__tests__/Tooltip-test.tsx
blob: d490fd5179c3897009081bec9736192c1f398bae (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
112
/*
 * 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 { shallow } from 'enzyme';
import * as React from 'react';
import Tooltip, { TooltipInner } from '../Tooltip';

jest.useFakeTimers();
jest.mock('react-dom', () => {
  const actual = require.requireActual('react-dom');
  return Object.assign({}, actual, {
    findDOMNode: () => undefined,
  });
});

it('should render', () => {
  expect(
    shallow(
      <TooltipInner overlay={<span id="overlay" />} visible={false}>
        <div id="tooltip" />
      </TooltipInner>
    )
  ).toMatchSnapshot();
  expect(
    shallow(
      <TooltipInner overlay={<span id="overlay" />} visible={true}>
        <div id="tooltip" />
      </TooltipInner>,
      { disableLifecycleMethods: true }
    )
  ).toMatchSnapshot();
});

it('should open & close', () => {
  const onShow = jest.fn();
  const onHide = jest.fn();
  const wrapper = shallow(
    <TooltipInner onHide={onHide} onShow={onShow} overlay={<span id="overlay" />}>
      <div id="tooltip" />
    </TooltipInner>
  );
  wrapper.find('#tooltip').simulate('mouseenter');
  jest.runOnlyPendingTimers();
  wrapper.update();
  expect(wrapper.find('TooltipPortal').exists()).toBe(true);
  expect(onShow).toBeCalled();

  wrapper.find('#tooltip').simulate('mouseleave');
  jest.runOnlyPendingTimers();
  wrapper.update();
  expect(wrapper.find('TooltipPortal').exists()).toBe(false);
  expect(onHide).toBeCalled();
});

it('should not open when mouse goes away quickly', () => {
  const onShow = jest.fn();
  const onHide = jest.fn();
  const wrapper = shallow(
    <TooltipInner onHide={onHide} onShow={onShow} overlay={<span id="overlay" />}>
      <div id="tooltip" />
    </TooltipInner>
  );

  wrapper.find('#tooltip').simulate('mouseenter');
  wrapper.find('#tooltip').simulate('mouseleave');
  jest.runOnlyPendingTimers();
  wrapper.update();

  expect(wrapper.find('TooltipPortal').exists()).toBe(false);
});

it('should not render tooltip without overlay', () => {
  const wrapper = shallow(
    <Tooltip overlay={undefined}>
      <div id="tooltip" />
    </Tooltip>
  );
  expect(wrapper.type()).toBe('div');
});

it('should not render empty tooltips', () => {
  expect(
    shallow(
      <Tooltip overlay={undefined} visible={true}>
        <div id="tooltip" />
      </Tooltip>
    )
  ).toMatchSnapshot();
  expect(
    shallow(
      <Tooltip overlay="" visible={true}>
        <div id="tooltip" />
      </Tooltip>
    )
  ).toMatchSnapshot();
});