|
1 |
| -import Vue from 'vue' |
2 | 1 | import { CInput, CButton, CDrawer, CDrawerBody, CDrawerFooter, CDrawerHeader, CDrawerOverlay, CDrawerContent, CDrawerCloseButton } from '../../'
|
3 |
| -import { render, userEvent, fireEvent, waitMs } from '@/tests/test-utils' |
| 2 | +import { render, userEvent, fireEvent, waitMs, wait, screen } from '@/tests/test-utils' |
4 | 3 | import { useId } from '@/packages/chakra-ui-core/src/utils'
|
5 | 4 |
|
6 | 5 | // mocks
|
@@ -49,84 +48,87 @@ it('should render correctly', async () => {
|
49 | 48 | const inlineAttrs = 'isOpen'
|
50 | 49 | const { asFragment } = renderComponent({ inlineAttrs })
|
51 | 50 |
|
52 |
| - await Vue.nextTick() |
| 51 | + await waitMs() // render |
53 | 52 |
|
54 | 53 | expect(asFragment(document.body.innerHTML)).toMatchSnapshot()
|
55 | 54 | })
|
56 | 55 |
|
57 | 56 | test('clicking the close button calls the onClose callback', async () => {
|
58 | 57 | const onClose = jest.fn()
|
59 | 58 | const inlineAttrs = 'isOpen :on-close="close"'
|
60 |
| - const { getByTestId } = renderComponent({ inlineAttrs, methods: { close: onClose } }) |
| 59 | + renderComponent({ inlineAttrs, methods: { close: onClose } }) |
61 | 60 |
|
62 |
| - await Vue.nextTick() |
63 |
| - userEvent.click(getByTestId('close-btn')) |
| 61 | + await wait(() => { |
| 62 | + userEvent.click(screen.getByTestId('close-btn')) |
| 63 | + }) |
64 | 64 |
|
65 | 65 | expect(onClose).toHaveBeenCalled()
|
66 | 66 | })
|
67 | 67 |
|
68 | 68 | test('pressing "esc" calls the onClose callback', async () => {
|
69 | 69 | const onClose = jest.fn()
|
70 | 70 | const inlineAttrs = ':isOpen="isOpen" :on-close="close"'
|
71 |
| - const { getByTestId } = renderComponent({ inlineAttrs, data: () => ({ isOpen: true }), methods: { close: onClose } }) |
| 71 | + renderComponent({ inlineAttrs, data: () => ({ isOpen: true }), methods: { close: onClose } }) |
72 | 72 |
|
73 |
| - await Vue.nextTick() |
74 |
| - const inputInside = getByTestId('inputInsideDrawer') |
75 |
| - |
76 |
| - fireEvent.keyDown(inputInside, { key: 'Escape' }) |
| 73 | + await wait(async () => { |
| 74 | + const inputInside = screen.getByTestId('inputInsideDrawer') |
| 75 | + await fireEvent.keyDown(inputInside, { key: 'Escape' }) |
| 76 | + }) |
77 | 77 |
|
78 | 78 | expect(onClose).toHaveBeenCalled()
|
79 | 79 | })
|
80 | 80 |
|
81 | 81 | test('clicking overlay calls the onClose callback', async () => {
|
82 | 82 | const onClose = jest.fn()
|
83 | 83 | const inlineAttrs = ':isOpen="isOpen" :on-close="close"'
|
84 |
| - const { getByTestId } = renderComponent({ inlineAttrs, data: () => ({ isOpen: true }), methods: { close: onClose } }) |
85 |
| - |
86 |
| - await Vue.nextTick() |
87 |
| - const overlay = getByTestId('overlay') |
| 84 | + renderComponent({ inlineAttrs, data: () => ({ isOpen: true }), methods: { close: onClose } }) |
88 | 85 |
|
89 |
| - userEvent.click(overlay) |
| 86 | + await wait(async () => { |
| 87 | + const overlay = screen.getByTestId('overlay') |
| 88 | + await fireEvent.click(overlay) |
| 89 | + }) |
90 | 90 |
|
91 | 91 | expect(onClose).toHaveBeenCalled()
|
92 | 92 | })
|
93 | 93 |
|
94 | 94 | test('focuses the initial focus ref when opened - initialFocusRef', async () => {
|
95 | 95 | const inlineAttrs = 'isOpen :on-close="close" :initialFocusRef="()=>$refs.inputInsideDrawer"'
|
96 |
| - const { getByTestId } = renderComponent({ inlineAttrs }) |
| 96 | + renderComponent({ inlineAttrs }) |
97 | 97 |
|
98 |
| - await Vue.nextTick() |
99 |
| - const inputInside = getByTestId('inputInsideDrawer') |
| 98 | + await waitMs() // render |
100 | 99 |
|
101 |
| - await waitMs() |
102 |
| - expect(inputInside).toHaveFocus() |
| 100 | + await wait(() => { |
| 101 | + expect(screen.getByTestId('inputInsideDrawer')).toHaveFocus() |
| 102 | + }) |
103 | 103 | })
|
104 | 104 |
|
105 | 105 | test('returns focus when closed - finalFocusRef', async () => {
|
106 | 106 | const inlineAttrs = ':isOpen="isOpen" :on-close="close" :finalFocusRef="$refs.inputOutsideDrawer"'
|
107 |
| - const { getByTestId } = renderComponent({ inlineAttrs, data: () => ({ isOpen: true }) }) |
| 107 | + renderComponent({ inlineAttrs, data: () => ({ isOpen: true }) }) |
| 108 | + |
| 109 | + await waitMs() // render |
108 | 110 |
|
109 |
| - await Vue.nextTick() |
110 |
| - const inputOutside = getByTestId('inputOutsideDrawer') |
111 |
| - const closeButton = getByTestId('close-btn') |
| 111 | + const inputOutside = screen.getByTestId('inputOutsideDrawer') |
| 112 | + const closeButton = screen.getByTestId('close-btn') |
112 | 113 |
|
113 |
| - await userEvent.click(closeButton) |
114 |
| - await waitMs() |
| 114 | + await fireEvent.click(closeButton) |
115 | 115 |
|
116 |
| - expect(inputOutside).toHaveFocus() |
| 116 | + await wait(() => { |
| 117 | + expect(inputOutside).toHaveFocus() |
| 118 | + }) |
117 | 119 | })
|
118 | 120 |
|
119 | 121 | it('should have proper aria', async () => {
|
120 | 122 | useId.mockReturnValueOnce('1')
|
121 | 123 | const inlineAttrs = 'isOpen'
|
122 | 124 | renderComponent({ inlineAttrs })
|
123 | 125 |
|
124 |
| - await Vue.nextTick() |
125 |
| - const dialog = document.querySelector('section') |
126 |
| - await waitMs() |
| 126 | + await wait(() => { |
| 127 | + const dialog = screen.getByRole('dialog') |
127 | 128 |
|
128 |
| - expect(dialog).toHaveAttribute('role', 'dialog') |
129 |
| - expect(dialog).toHaveAttribute('aria-modal', 'true') |
130 |
| - expect(dialog).toHaveAttribute('aria-labelledby', 'drawer-1-header') |
131 |
| - expect(dialog).toHaveAttribute('aria-describedby', 'drawer-1-body') |
| 129 | + expect(dialog).toHaveAttribute('role', 'dialog') |
| 130 | + expect(dialog).toHaveAttribute('aria-modal', 'true') |
| 131 | + expect(dialog).toHaveAttribute('aria-labelledby', 'drawer-1-header') |
| 132 | + expect(dialog).toHaveAttribute('aria-describedby', 'drawer-1-body') |
| 133 | + }) |
132 | 134 | })
|
0 commit comments