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

Commit 3aa7a15

Browse files
committed
test(drawer): refactor drawer tests
1 parent 1af9a07 commit 3aa7a15

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

packages/chakra-ui-core/src/CDrawer/tests/CDrawer.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, 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'
43
import { useId } from '@/packages/chakra-ui-core/src/utils'
54

65
// mocks
@@ -49,84 +48,87 @@ it('should render correctly', async () => {
4948
const inlineAttrs = 'isOpen'
5049
const { asFragment } = renderComponent({ inlineAttrs })
5150

52-
await Vue.nextTick()
51+
await waitMs() // render
5352

5453
expect(asFragment(document.body.innerHTML)).toMatchSnapshot()
5554
})
5655

5756
test('clicking the close button calls the onClose callback', async () => {
5857
const onClose = jest.fn()
5958
const inlineAttrs = 'isOpen :on-close="close"'
60-
const { getByTestId } = renderComponent({ inlineAttrs, methods: { close: onClose } })
59+
renderComponent({ inlineAttrs, methods: { close: onClose } })
6160

62-
await Vue.nextTick()
63-
userEvent.click(getByTestId('close-btn'))
61+
await wait(() => {
62+
userEvent.click(screen.getByTestId('close-btn'))
63+
})
6464

6565
expect(onClose).toHaveBeenCalled()
6666
})
6767

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

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

7878
expect(onClose).toHaveBeenCalled()
7979
})
8080

8181
test('clicking overlay calls the onClose callback', async () => {
8282
const onClose = jest.fn()
8383
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 } })
8885

89-
userEvent.click(overlay)
86+
await wait(async () => {
87+
const overlay = screen.getByTestId('overlay')
88+
await fireEvent.click(overlay)
89+
})
9090

9191
expect(onClose).toHaveBeenCalled()
9292
})
9393

9494
test('focuses the initial focus ref when opened - initialFocusRef', async () => {
9595
const inlineAttrs = 'isOpen :on-close="close" :initialFocusRef="()=>$refs.inputInsideDrawer"'
96-
const { getByTestId } = renderComponent({ inlineAttrs })
96+
renderComponent({ inlineAttrs })
9797

98-
await Vue.nextTick()
99-
const inputInside = getByTestId('inputInsideDrawer')
98+
await waitMs() // render
10099

101-
await waitMs()
102-
expect(inputInside).toHaveFocus()
100+
await wait(() => {
101+
expect(screen.getByTestId('inputInsideDrawer')).toHaveFocus()
102+
})
103103
})
104104

105105
test('returns focus when closed - finalFocusRef', async () => {
106106
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
108110

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

113-
await userEvent.click(closeButton)
114-
await waitMs()
114+
await fireEvent.click(closeButton)
115115

116-
expect(inputOutside).toHaveFocus()
116+
await wait(() => {
117+
expect(inputOutside).toHaveFocus()
118+
})
117119
})
118120

119121
it('should have proper aria', async () => {
120122
useId.mockReturnValueOnce('1')
121123
const inlineAttrs = 'isOpen'
122124
renderComponent({ inlineAttrs })
123125

124-
await Vue.nextTick()
125-
const dialog = document.querySelector('section')
126-
await waitMs()
126+
await wait(() => {
127+
const dialog = screen.getByRole('dialog')
127128

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

0 commit comments

Comments
 (0)