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.

Dropdown-test.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 { mount, shallow, ShallowWrapper } from 'enzyme';
  21. import * as React from 'react';
  22. import { click } from '../../../helpers/testUtils';
  23. import { Popup, PopupPlacement } from '../../ui/popups';
  24. import { Button } from '../buttons';
  25. import Dropdown, { DropdownOverlay } from '../Dropdown';
  26. import ScreenPositionFixer from '../ScreenPositionFixer';
  27. describe('Dropdown', () => {
  28. it('renders', () => {
  29. expect(
  30. shallow(<Dropdown overlay={<div id="overlay" />}>{() => <div />}</Dropdown>)
  31. .find('div')
  32. .exists()
  33. ).toBe(true);
  34. });
  35. it('toggles with element child', () => {
  36. checkToggle(
  37. shallow(
  38. <Dropdown overlay={<div id="overlay" />}>
  39. <Button />
  40. </Dropdown>
  41. )
  42. );
  43. checkToggle(
  44. shallow(
  45. <Dropdown overlay={<div id="overlay" />}>
  46. <a href="#">click me!</a>
  47. </Dropdown>
  48. ),
  49. 'a'
  50. );
  51. });
  52. it('toggles with render prop', () => {
  53. checkToggle(
  54. shallow(
  55. <Dropdown overlay={<div id="overlay" />}>
  56. {({ onToggleClick }) => <Button onClick={onToggleClick} />}
  57. </Dropdown>
  58. )
  59. );
  60. });
  61. it('should call onOpen', () => {
  62. const onOpen = jest.fn();
  63. const wrapper = mount(
  64. <Dropdown onOpen={onOpen} overlay={<div id="overlay" />}>
  65. <Button />
  66. </Dropdown>
  67. );
  68. expect(onOpen).not.toBeCalled();
  69. click(wrapper.find('Button'));
  70. expect(onOpen).toBeCalled();
  71. });
  72. function checkToggle(wrapper: ShallowWrapper, selector = 'Button') {
  73. expect(wrapper.state()).toEqual({ open: false });
  74. click(wrapper.find(selector));
  75. expect(wrapper.state()).toEqual({ open: true });
  76. click(wrapper.find(selector));
  77. expect(wrapper.state()).toEqual({ open: false });
  78. }
  79. });
  80. describe('DropdownOverlay', () => {
  81. it('should render overlay with screen fixer', () => {
  82. const wrapper = shallow(
  83. <DropdownOverlay>
  84. <div />
  85. </DropdownOverlay>,
  86. // disable ScreenPositionFixer positioning
  87. { disableLifecycleMethods: true }
  88. );
  89. expect(wrapper.is(ScreenPositionFixer)).toBe(true);
  90. expect(wrapper.dive().dive().dive().is(Popup)).toBe(true);
  91. });
  92. it('should render overlay without screen fixer', () => {
  93. const wrapper = shallow(
  94. <DropdownOverlay placement={PopupPlacement.BottomRight}>
  95. <div />
  96. </DropdownOverlay>
  97. );
  98. expect(wrapper.is('Popup')).toBe(true);
  99. });
  100. });