|
1 |
| -import * as React from 'react'; |
2 |
| -import App from '@app/index'; |
3 |
| -import { mount, shallow } from 'enzyme'; |
4 |
| -import { Button } from '@patternfly/react-core'; |
| 1 | +import * as React from 'react' |
| 2 | +import {render, screen, queryByAttribute, act, within} from '@testing-library/react' |
| 3 | +import '@testing-library/jest-dom' |
| 4 | +import App from '@app/index' |
| 5 | +import userEvent from '@testing-library/user-event' |
| 6 | +import { Button } from '@patternfly/react-core' |
5 | 7 |
|
6 | 8 | describe('App tests', () => {
|
7 | 9 | test('should render default App component', () => {
|
8 |
| - const view = shallow(<App />); |
9 |
| - expect(view).toMatchSnapshot(); |
10 |
| - }); |
11 |
| - |
12 |
| - it('should render a nav-toggle button', () => { |
13 |
| - const wrapper = mount(<App />); |
14 |
| - const button = wrapper.find(Button); |
15 |
| - expect(button.exists()).toBe(true); |
16 |
| - }); |
| 10 | + render(<App />) |
| 11 | + expect(screen).toMatchSnapshot() |
| 12 | + }) |
17 | 13 |
|
18 | 14 | it('should hide the sidebar on smaller viewports', () => {
|
19 |
| - Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 600 }); |
20 |
| - const wrapper = mount(<App />); |
21 |
| - window.dispatchEvent(new Event('resize')); |
22 |
| - expect(wrapper.find('#page-sidebar').hasClass('pf-m-collapsed')).toBeTruthy(); |
23 |
| - }); |
| 15 | + Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 600 }) |
| 16 | + const getById = queryByAttribute.bind(null, 'id') |
| 17 | + const dom = render(<App />) |
| 18 | + act(() => window.dispatchEvent(new Event('resize'))) |
| 19 | + expect(getById(dom.container, 'page-sidebar')).toHaveClass('pf-m-collapsed') |
| 20 | + }) |
24 | 21 |
|
25 |
| - it('should expand the sidebar on larger viewports', () => { |
26 |
| - Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1200 }); |
27 |
| - const wrapper = mount(<App />); |
28 |
| - window.dispatchEvent(new Event('resize')); |
29 |
| - expect(wrapper.find('#page-sidebar').hasClass('pf-m-expanded')).toBeTruthy(); |
30 |
| - }); |
| 22 | + it('should show the sidebar when clicking the nav-toggle button', async () => { |
| 23 | + Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 600 }) |
| 24 | + const getById = queryByAttribute.bind(null, 'id') |
| 25 | + const dom = render(<App />) |
| 26 | + const navButton = screen.getByRole('button') |
31 | 27 |
|
32 |
| - it('should hide the sidebar when clicking the nav-toggle button', () => { |
33 |
| - Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1200 }); |
34 |
| - const wrapper = mount(<App />); |
35 |
| - window.dispatchEvent(new Event('resize')); |
36 |
| - const button = wrapper.find('#nav-toggle').hostNodes(); |
37 |
| - expect(wrapper.find('#page-sidebar').hasClass('pf-m-expanded')).toBeTruthy(); |
38 |
| - button.simulate('click'); |
39 |
| - expect(wrapper.find('#page-sidebar').hasClass('pf-m-collapsed')).toBeTruthy(); |
40 |
| - expect(wrapper.find('#page-sidebar').hasClass('pf-m-expanded')).toBeFalsy(); |
41 |
| - }); |
42 |
| -}); |
| 28 | + act(() => window.dispatchEvent(new Event('resize'))) |
| 29 | + expect(getById(dom.container,'page-sidebar')).toHaveClass('pf-m-collapsed') |
| 30 | + await userEvent.click(navButton) |
| 31 | + expect(getById(dom.container,'page-sidebar')).toHaveClass('pf-m-expanded') |
| 32 | + }) |
| 33 | +}) |
0 commit comments