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

Commit 470ec73

Browse files
committed
test(editable): refactor use screen
1 parent 3aa7a15 commit 470ec73

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

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

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CEditable, CEditableInput, CEditablePreview } from '../..'
22
import { useId } from '../../utils'
3-
import { render, userEvent, fireEvent } from '@/tests/test-utils'
3+
import { render, userEvent, fireEvent, screen } from '@/tests/test-utils'
44

55
// mocks
66
jest.mock('@/packages/chakra-ui-core/src/utils/generators.js')
@@ -29,9 +29,9 @@ it('should render correctly', () => {
2929
it('should render correctly - input', async () => {
3030
useId.mockReturnValueOnce('1')
3131
const inlineAttrs = 'defaultValue="testing" '
32-
const { asFragment, getByTestId } = renderComponent({ inlineAttrs })
32+
const { asFragment } = renderComponent({ inlineAttrs })
3333

34-
const preview = getByTestId('preview')
34+
const preview = screen.getByTestId('preview')
3535
await userEvent.click(preview)
3636

3737
expect(asFragment(document.body.innerHTML)).toMatchSnapshot()
@@ -44,82 +44,87 @@ it('uncontrolled: handles callbacks correctly', async () => {
4444
const onEdit = jest.fn()
4545

4646
const inlineAttrs = '@edit="onEdit" @change="onChange" @cancel="onCancel" @submit="onSubmit" defaultValue="Hello "'
47-
const { getByTestId } = renderComponent({ inlineAttrs, methods: { onChange, onCancel, onSubmit, onEdit } })
47+
renderComponent({ inlineAttrs, methods: { onChange, onCancel, onSubmit, onEdit } })
4848

49-
const preview = getByTestId('preview')
49+
const preview = screen.getByTestId('preview')
5050
await userEvent.click(preview)
5151

52-
const input = getByTestId('input')
52+
const input = screen.getByTestId('input')
5353

5454
// calls `onEdit` when preview is focused
55-
fireEvent.focus(preview)
55+
await fireEvent.focus(preview)
5656
expect(onEdit).toHaveBeenCalled()
5757

5858
// calls `onChange` with input on change
59-
userEvent.type(input, 'World')
59+
await userEvent.type(input, 'World')
6060
expect(onChange).toHaveBeenCalledWith('Hello World')
6161

6262
// calls `onCancel` with previous value when "esc" pressed
63-
fireEvent.keyDown(input, { key: 'Escape' })
63+
await fireEvent.keyDown(input, { key: 'Escape' })
6464
expect(onCancel).toHaveBeenCalledWith('Hello ')
6565

6666
// calls `onSubmit` with previous value when "enter" pressed after cancelling
67-
fireEvent.keyDown(input, { key: 'Enter' })
67+
await fireEvent.keyDown(input, { key: 'Enter' })
6868
expect(onSubmit).toHaveBeenCalledWith('Hello ')
6969

7070
// TODO: ⚠️ we should make controlledInput for vue
71+
// we didn't change the v-model but input.value changes
7172
// remove this line, previous input value stays and next 2 expect fails
7273
input.value = 'Hello '
7374

7475
// returns new value when submitting without cancelling
75-
userEvent.type(input, 'World')
76-
fireEvent.keyDown(input, { key: 'Enter' })
76+
await userEvent.type(input, 'World')
77+
await fireEvent.keyDown(input, { key: 'Enter' })
7778
expect(onSubmit).toHaveBeenCalledWith('Hello World')
7879

7980
// cancelling now returns new value
80-
fireEvent.keyDown(input, { key: 'Escape' })
81+
await fireEvent.keyDown(input, { key: 'Escape' })
8182
expect(onCancel).toHaveBeenCalledWith('Hello ')
8283
})
8384

8485
it('controlled: handles callbacks correctly', async () => {
86+
useId.mockReturnValueOnce('1')
8587
const onChange = jest.fn()
8688
const onCancel = jest.fn()
8789
const onSubmit = jest.fn()
8890
const onEdit = jest.fn()
8991

9092
const inlineAttrs = '@edit="onEdit" @change="onChange" @cancel="onCancel" @submit="onSubmit" defaultValue="Hello "'
91-
const { getByTestId } = renderComponent({ inlineAttrs, methods: { onChange, onCancel, onSubmit, onEdit } })
93+
renderComponent({ inlineAttrs, methods: { onChange, onCancel, onSubmit, onEdit } })
9294

93-
const preview = getByTestId('preview')
95+
const preview = screen.getByTestId('preview')
9496

95-
await userEvent.click(preview)
96-
const input = getByTestId('input')
97+
await fireEvent.touch(preview)
98+
const input = screen.getByTestId('input')
9799

98100
// calls `onEdit` when preview is focused
99-
fireEvent.focus(preview)
101+
await fireEvent.focus(preview)
100102
expect(onEdit).toHaveBeenCalled()
101103

102104
// calls `onSubmit` with `value`
103-
fireEvent.keyDown(input, { key: 'Enter' })
105+
await fireEvent.keyDown(input, { key: 'Enter' })
104106
expect(onSubmit).toHaveBeenCalledWith('Hello ')
105107

106108
// calls `onCancel` with `value`
107-
fireEvent.keyDown(input, { key: 'Escape' })
109+
await fireEvent.keyDown(input, { key: 'Escape' })
108110
expect(onSubmit).toHaveBeenCalledWith('Hello ')
109111

110112
// calls `onChange` with new input on change
111-
userEvent.type(input, 'World')
113+
// await fireEvent.update(input, 'Hello World') // changes all value
114+
await userEvent.type(input, 'World') // continues typing with previous value
115+
116+
await fireEvent.keyDown(input, { key: 'Escape' })
112117
expect(onChange).toHaveBeenCalledWith('Hello World')
113118
})
114119

115120
test('has the proper aria attributes', async () => {
116121
const inlineAttrs = 'defaultValue=""'
117-
const { getByTestId } = renderComponent({ inlineAttrs })
122+
renderComponent({ inlineAttrs })
118123

119-
const preview = getByTestId('preview')
124+
const preview = screen.getByTestId('preview')
120125

121126
await userEvent.click(preview)
122-
const input = getByTestId('input')
127+
const input = screen.getByTestId('input')
123128

124129
// preview and input do not have aria-disabled when `CEditable` is not disabled
125130
expect(preview).not.toHaveAttribute('aria-disabled')
@@ -128,9 +133,9 @@ test('has the proper aria attributes', async () => {
128133

129134
test('has the proper aria attributes when disabled', () => {
130135
const inlineAttrs = 'isDisabled defaultValue=""'
131-
const { getByTestId } = renderComponent({ inlineAttrs })
136+
renderComponent({ inlineAttrs })
132137

133-
const preview = getByTestId('preview')
138+
const preview = screen.getByTestId('preview')
134139

135140
// preview does not have aria-disabled when `CEditable` is not disabled
136141
expect(preview).toHaveAttribute('aria-disabled', 'true')

0 commit comments

Comments
 (0)