Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 1ca10fb

Browse files
committed
test(modal): refactor use screen
1 parent 92d5db7 commit 1ca10fb

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

packages/chakra-ui-core/src/CModal/tests/CModal.test.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import Vue from 'vue'
21
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'
43
import { useId, wrapEvent } from '@/packages/chakra-ui-core/src/utils'
54

65
// mocks
@@ -53,84 +52,87 @@ it('should render correctly', async () => {
5352
const inlineAttrs = 'isOpen'
5453
const { asFragment } = renderComponent({ inlineAttrs })
5554

56-
await Vue.nextTick()
55+
await waitMs() // render
5756

5857
expect(asFragment(document.body.innerHTML)).toMatchSnapshot()
5958
})
6059

6160
test('clicking the close button calls the onClose callback', async () => {
6261
const onClose = jest.fn()
6362
const inlineAttrs = 'isOpen :on-close="close"'
64-
const { getByTestId } = renderComponent({ inlineAttrs, methods: { close: onClose } })
63+
renderComponent({ inlineAttrs, methods: { close: onClose } })
6564

66-
await Vue.nextTick()
67-
userEvent.click(getByTestId('close-btn'))
65+
await wait(() => {
66+
userEvent.click(screen.getByTestId('close-btn'))
67+
})
6868

6969
expect(onClose).toHaveBeenCalled()
7070
})
7171

7272
test('pressing "esc" calls the onClose callback', async () => {
7373
const onClose = jest.fn()
7474
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 } })
7676

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+
})
8181

8282
expect(onClose).toHaveBeenCalled()
8383
})
8484

8585
test('clicking overlay calls the onClose callback', async () => {
8686
const onClose = jest.fn()
8787
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 } })
9289

93-
userEvent.click(overlay)
90+
await wait(async () => {
91+
const overlay = screen.getByTestId('overlay')
92+
await fireEvent.click(overlay.parentElement)
93+
})
9494

9595
expect(onClose).toHaveBeenCalled()
9696
})
9797

9898
test('focuses the initial focus ref when opened - initialFocusRef', async () => {
9999
const inlineAttrs = 'isOpen :on-close="close" :initialFocusRef="()=>$refs.inputInsideModal"'
100-
const { getByTestId } = renderComponent({ inlineAttrs })
100+
renderComponent({ inlineAttrs })
101101

102-
await Vue.nextTick()
103-
const inputInside = getByTestId('inputInsideModal')
102+
await waitMs() // render
104103

105-
await waitMs()
106-
expect(inputInside).toHaveFocus()
104+
await wait(() => {
105+
expect(screen.getByTestId('inputInsideModal')).toHaveFocus()
106+
})
107107
})
108108

109109
test('returns focus when closed - finalFocusRef', async () => {
110110
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
112114

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')
116117

117-
await userEvent.click(closeButton)
118-
await waitMs()
118+
await fireEvent.click(closeButton)
119119

120-
expect(inputOutside).toHaveFocus()
120+
await wait(() => {
121+
expect(inputOutside).toHaveFocus()
122+
})
121123
})
122124

123125
it('should have proper aria', async () => {
124126
useId.mockReturnValueOnce('1')
125127
const inlineAttrs = 'isOpen'
126128
renderComponent({ inlineAttrs })
127129

128-
await Vue.nextTick()
129-
const dialog = document.querySelector('section')
130-
await waitMs()
130+
await wait(() => {
131+
const dialog = screen.getByRole('dialog')
131132

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+
})
136138
})

0 commit comments

Comments
 (0)