|
1 | 1 | import * as React from 'react'; |
| 2 | +import { screen, fireEvent } from '@testing-library/react'; |
2 | 3 |
|
| 4 | +import { render } from '../../../test-utils/testing-library'; |
3 | 5 | import SidebarToggleButton from '..'; |
4 | 6 |
|
5 | 7 | describe('components/sidebar-toggle-button/SidebarToggleButton', () => { |
6 | | - test('should render correctly as open', () => { |
7 | | - const wrapper = mount(<SidebarToggleButton isOpen />); |
| 8 | + test.each` |
| 9 | + isOpen | direction |
| 10 | + ${true} | ${'left'} |
| 11 | + ${false} | ${'left'} |
| 12 | + ${true} | ${'right'} |
| 13 | + ${false} | ${'right'} |
| 14 | + `( |
| 15 | + 'should render correct button correctly when open is $isOpen and direction is $direction and isPreviewModernizationEnabled is false', |
| 16 | + ({ isOpen, direction }) => { |
| 17 | + render(<SidebarToggleButton isOpen={isOpen} direction={direction} />, { |
| 18 | + wrapperProps: { features: { previewModernization: { enabled: false } } }, |
| 19 | + }); |
8 | 20 |
|
9 | | - expect(wrapper).toMatchSnapshot(); |
10 | | - }); |
| 21 | + const button = screen.getByRole('button'); |
| 22 | + let iconClassName = ''; |
| 23 | + let iconNotDisplayedClassName = ''; |
| 24 | + if (direction === 'left') { |
| 25 | + iconClassName = isOpen ? 'icon-show' : 'icon-hide'; |
| 26 | + iconNotDisplayedClassName = isOpen ? 'icon-hide' : 'icon-show'; |
| 27 | + } else { |
| 28 | + iconClassName = isOpen ? 'icon-hide' : 'icon-show'; |
| 29 | + iconNotDisplayedClassName = isOpen ? 'icon-show' : 'icon-hide'; |
| 30 | + } |
| 31 | + const icon = button.querySelector(`.${iconClassName}`); |
| 32 | + const iconNotDisplayed = button.querySelector(`.${iconNotDisplayedClassName}`); |
| 33 | + expect(icon).toBeInTheDocument(); |
| 34 | + expect(iconNotDisplayed).not.toBeInTheDocument(); |
| 35 | + const isCollapsed = button.classList.contains('bdl-is-collapsed'); |
| 36 | + expect(isCollapsed).toBe(!isOpen); |
| 37 | + }, |
| 38 | + ); |
11 | 39 |
|
12 | | - test('should render correctly as closed', () => { |
13 | | - const wrapper = mount(<SidebarToggleButton isOpen={false} />); |
| 40 | + test.each([true, false])( |
| 41 | + 'should stop the mousedown event from being propagated up to box-annotations if isPreviewModernizationEnabled is %s', |
| 42 | + isPreviewModernizationEnabled => { |
| 43 | + const onMouseDown = jest.fn(); |
| 44 | + const onClick = jest.fn(); |
| 45 | + render( |
| 46 | + // eslint-disable-next-line jsx-a11y/no-static-element-interactions |
| 47 | + <div onMouseDown={onMouseDown}> |
| 48 | + <SidebarToggleButton isOpen onClick={onClick} /> |
| 49 | + </div>, |
| 50 | + { |
| 51 | + wrapperProps: { features: { previewModernization: { enabled: isPreviewModernizationEnabled } } }, |
| 52 | + }, |
| 53 | + ); |
| 54 | + const button = screen.getByRole('button'); |
| 55 | + fireEvent.mouseDown(button); |
| 56 | + expect(onMouseDown).not.toHaveBeenCalled(); |
| 57 | + }, |
| 58 | + ); |
14 | 59 |
|
15 | | - expect(wrapper).toMatchSnapshot(); |
16 | | - }); |
17 | | - |
18 | | - test('should have the proper class when it is collapsed', () => { |
19 | | - const wrapper = mount(<SidebarToggleButton isOpen={false} />); |
20 | | - |
21 | | - expect(wrapper.find('PlainButton').hasClass('bdl-is-collapsed')).toBeTruthy(); |
22 | | - }); |
23 | | - |
24 | | - test('should render correctly as left oriented toggle when open', () => { |
25 | | - const wrapper = mount(<SidebarToggleButton direction="left" isOpen />); |
26 | | - |
27 | | - expect(wrapper).toMatchSnapshot(); |
28 | | - }); |
29 | | - |
30 | | - test('should render correctly as left oriented toggle when closed', () => { |
31 | | - const wrapper = mount(<SidebarToggleButton direction="left" isOpen={false} />); |
32 | | - |
33 | | - expect(wrapper).toMatchSnapshot(); |
34 | | - }); |
| 60 | + test.each([true, false])( |
| 61 | + 'should show proper button isPreviewModernizationEnabled is %s and click handler should work', |
| 62 | + isPreviewModernizationEnabled => { |
| 63 | + const onClick = jest.fn(); |
| 64 | + render(<SidebarToggleButton isOpen onClick={onClick} />, { |
| 65 | + wrapperProps: { features: { previewModernization: { enabled: isPreviewModernizationEnabled } } }, |
| 66 | + }); |
| 67 | + const button = screen.getByRole('button'); |
| 68 | + expect(button).toHaveClass('bdl-SidebarToggleButton'); |
| 69 | + const isModernized = button.classList.contains('bdl-SidebarToggleButton--modernized'); |
| 70 | + expect(isModernized).toBe(isPreviewModernizationEnabled); |
| 71 | + fireEvent.click(button); |
| 72 | + expect(onClick).toHaveBeenCalled(); |
| 73 | + }, |
| 74 | + ); |
35 | 75 | }); |
0 commit comments