|
1 | 1 | import * as React from 'react';
|
2 |
| -import { shallow } from 'enzyme/build'; |
| 2 | +import { render, screen, userEvent } from '../../../test-utils/testing-library'; |
3 | 3 | import { SidebarToggleComponent as SidebarToggle } from '../SidebarToggle';
|
4 | 4 |
|
5 | 5 | describe('elements/content-sidebar/SidebarToggle', () => {
|
6 | 6 | const historyMock = { replace: jest.fn() };
|
7 |
| - const getWrapper = (props = {}) => shallow(<SidebarToggle history={historyMock} {...props} />); |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + jest.clearAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + const renderSidebarToggle = (props = {}) => { |
| 13 | + return render(<SidebarToggle history={historyMock} {...props} />); |
| 14 | + }; |
8 | 15 |
|
9 | 16 | test.each`
|
10 |
| - isOpen | location |
| 17 | + isOpen | expectedState |
11 | 18 | ${true} | ${{ state: { open: false } }}
|
12 | 19 | ${false} | ${{ state: { open: true } }}
|
13 |
| - `('should render and handle clicks correctly when isOpen is $isOpen', ({ isOpen, location }) => { |
14 |
| - const event = { preventDefault: jest.fn() }; |
15 |
| - const wrapper = getWrapper({ isOpen }); |
| 20 | + `('should render and handle clicks correctly when isOpen is $isOpen', async ({ isOpen, expectedState }) => { |
| 21 | + const user = userEvent(); |
| 22 | + renderSidebarToggle({ isOpen }); |
| 23 | + |
| 24 | + const toggleButton = screen.getByTestId('sidebartoggle'); |
| 25 | + expect(toggleButton).toBeInTheDocument(); |
| 26 | + |
| 27 | + await user.click(toggleButton); |
| 28 | + |
| 29 | + expect(historyMock.replace).toHaveBeenCalledWith(expectedState); |
| 30 | + }); |
| 31 | +}); |
16 | 32 |
|
17 |
| - wrapper.simulate('click', event); |
| 33 | +describe('elements/content-sidebar/SidebarToggle - Router Disabled', () => { |
| 34 | + const mockInternalSidebarNavigationHandler = jest.fn(); |
| 35 | + const defaultProps = { |
| 36 | + routerDisabled: true, |
| 37 | + internalSidebarNavigation: { sidebar: 'activity' }, |
| 38 | + internalSidebarNavigationHandler: mockInternalSidebarNavigationHandler, |
| 39 | + }; |
18 | 40 |
|
19 |
| - expect(event.preventDefault).toHaveBeenCalled(); |
20 |
| - expect(historyMock.replace).toHaveBeenCalledWith(location); |
21 |
| - expect(wrapper).toMatchSnapshot(); |
| 41 | + beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
22 | 43 | });
|
| 44 | + |
| 45 | + const renderSidebarToggle = (props = {}) => { |
| 46 | + return render(<SidebarToggle {...defaultProps} {...props} />); |
| 47 | + }; |
| 48 | + |
| 49 | + test.each` |
| 50 | + isOpen | expectedNavigation |
| 51 | + ${true} | ${{ sidebar: 'activity', open: false }} |
| 52 | + ${false} | ${{ sidebar: 'activity', open: true }} |
| 53 | + `('should handle toggle clicks correctly when isOpen is $isOpen', async ({ isOpen, expectedNavigation }) => { |
| 54 | + const user = userEvent(); |
| 55 | + renderSidebarToggle({ isOpen }); |
| 56 | + |
| 57 | + const toggleButton = screen.getByTestId('sidebartoggle'); |
| 58 | + expect(toggleButton).toBeInTheDocument(); |
| 59 | + await user.click(toggleButton); |
| 60 | + |
| 61 | + expect(mockInternalSidebarNavigationHandler).toHaveBeenCalledWith(expectedNavigation, true); |
| 62 | + }); |
| 63 | + |
| 64 | + test('should handle complex navigation state correctly', async () => { |
| 65 | + const user = userEvent(); |
| 66 | + const complexNavigation = { |
| 67 | + sidebar: 'activity', |
| 68 | + versionId: '123', |
| 69 | + activeFeedEntryType: 'comments', |
| 70 | + activeFeedEntryId: '456', |
| 71 | + }; |
| 72 | + |
| 73 | + renderSidebarToggle({ |
| 74 | + isOpen: true, |
| 75 | + internalSidebarNavigation: complexNavigation, |
| 76 | + }); |
| 77 | + |
| 78 | + const toggleButton = screen.getByTestId('sidebartoggle'); |
| 79 | + await user.click(toggleButton); |
| 80 | + |
| 81 | + expect(mockInternalSidebarNavigationHandler).toHaveBeenCalledWith({ |
| 82 | + ...complexNavigation, |
| 83 | + open: false, |
| 84 | + }, true); |
| 85 | + }); |
| 86 | + |
23 | 87 | });
|
0 commit comments