You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. import { shallow } from 'enzyme';
  21. import * as React from 'react';
  22. import Tooltip, { TooltipInner, TooltipProps } from '../Tooltip';
  23. beforeAll(() => {
  24. jest.useFakeTimers();
  25. });
  26. afterAll(() => {
  27. jest.runOnlyPendingTimers();
  28. jest.useRealTimers();
  29. });
  30. jest.mock('react-dom', () => {
  31. const actual = jest.requireActual('react-dom');
  32. return Object.assign({}, actual, {
  33. findDOMNode: jest.fn().mockReturnValue(undefined)
  34. });
  35. });
  36. jest.mock('lodash', () => {
  37. const actual = jest.requireActual('lodash');
  38. return Object.assign({}, actual, {
  39. uniqueId: jest.fn(prefix => `${prefix}1`)
  40. });
  41. });
  42. beforeEach(jest.clearAllMocks);
  43. it('should render', () => {
  44. expect(shallowRenderTooltipInner()).toMatchSnapshot();
  45. expect(
  46. shallow(
  47. <TooltipInner overlay={<span id="overlay" />} visible={true}>
  48. <div id="tooltip" />
  49. </TooltipInner>,
  50. { disableLifecycleMethods: true }
  51. )
  52. ).toMatchSnapshot();
  53. });
  54. it('should open & close', () => {
  55. const onShow = jest.fn();
  56. const onHide = jest.fn();
  57. const wrapper = shallowRenderTooltipInner({ onHide, onShow });
  58. wrapper.find('#tooltip').simulate('pointerenter');
  59. jest.runOnlyPendingTimers();
  60. wrapper.update();
  61. expect(wrapper.find('TooltipPortal').exists()).toBe(true);
  62. expect(onShow).toBeCalled();
  63. wrapper.find('#tooltip').simulate('pointerleave');
  64. jest.runOnlyPendingTimers();
  65. wrapper.update();
  66. expect(wrapper.find('TooltipPortal').exists()).toBe(false);
  67. expect(onHide).toBeCalled();
  68. onShow.mockReset();
  69. onHide.mockReset();
  70. wrapper.find('#tooltip').simulate('focus');
  71. expect(wrapper.find('TooltipPortal').exists()).toBe(true);
  72. expect(onShow).toBeCalled();
  73. wrapper.find('#tooltip').simulate('blur');
  74. expect(wrapper.find('TooltipPortal').exists()).toBe(false);
  75. expect(onHide).toBeCalled();
  76. });
  77. it('should not open when pointer goes away quickly', () => {
  78. const onShow = jest.fn();
  79. const onHide = jest.fn();
  80. const wrapper = shallowRenderTooltipInner({ onHide, onShow });
  81. wrapper.find('#tooltip').simulate('pointerenter');
  82. wrapper.find('#tooltip').simulate('pointerleave');
  83. jest.runOnlyPendingTimers();
  84. wrapper.update();
  85. expect(wrapper.find('TooltipPortal').exists()).toBe(false);
  86. });
  87. it('should not render tooltip without overlay', () => {
  88. const wrapper = shallowRenderTooltip();
  89. expect(wrapper.type()).toBe('div');
  90. });
  91. it('should not render empty tooltips', () => {
  92. expect(shallowRenderTooltip()).toMatchSnapshot();
  93. expect(shallowRenderTooltip()).toMatchSnapshot();
  94. });
  95. it('should adjust arrow position', () => {
  96. const wrapper = shallowRenderTooltipInner();
  97. expect(wrapper.instance().adjustArrowPosition('left', { leftFix: 10, topFix: 20 })).toEqual({
  98. marginTop: -20
  99. });
  100. expect(wrapper.instance().adjustArrowPosition('right', { leftFix: 10, topFix: 20 })).toEqual({
  101. marginTop: -20
  102. });
  103. expect(wrapper.instance().adjustArrowPosition('top', { leftFix: 10, topFix: 20 })).toEqual({
  104. marginLeft: -10
  105. });
  106. expect(wrapper.instance().adjustArrowPosition('bottom', { leftFix: 10, topFix: 20 })).toEqual({
  107. marginLeft: -10
  108. });
  109. });
  110. function shallowRenderTooltip() {
  111. return shallow<TooltipProps>(
  112. <Tooltip overlay={undefined}>
  113. <div id="tooltip" />
  114. </Tooltip>
  115. );
  116. }
  117. function shallowRenderTooltipInner(props?: Partial<TooltipProps>) {
  118. return shallow<TooltipInner>(
  119. <TooltipInner overlay={<span id="overlay" />} {...props}>
  120. <div id="tooltip" />
  121. </TooltipInner>
  122. );
  123. }