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