1
1
import { CEditable , CEditableInput , CEditablePreview } from '../..'
2
2
import { useId } from '../../utils'
3
- import { render , userEvent , fireEvent } from '@/tests/test-utils'
3
+ import { render , userEvent , fireEvent , screen } from '@/tests/test-utils'
4
4
5
5
// mocks
6
6
jest . mock ( '@/packages/chakra-ui-core/src/utils/generators.js' )
@@ -29,9 +29,9 @@ it('should render correctly', () => {
29
29
it ( 'should render correctly - input' , async ( ) => {
30
30
useId . mockReturnValueOnce ( '1' )
31
31
const inlineAttrs = 'defaultValue="testing" '
32
- const { asFragment, getByTestId } = renderComponent ( { inlineAttrs } )
32
+ const { asFragment } = renderComponent ( { inlineAttrs } )
33
33
34
- const preview = getByTestId ( 'preview' )
34
+ const preview = screen . getByTestId ( 'preview' )
35
35
await userEvent . click ( preview )
36
36
37
37
expect ( asFragment ( document . body . innerHTML ) ) . toMatchSnapshot ( )
@@ -44,82 +44,87 @@ it('uncontrolled: handles callbacks correctly', async () => {
44
44
const onEdit = jest . fn ( )
45
45
46
46
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 } } )
48
48
49
- const preview = getByTestId ( 'preview' )
49
+ const preview = screen . getByTestId ( 'preview' )
50
50
await userEvent . click ( preview )
51
51
52
- const input = getByTestId ( 'input' )
52
+ const input = screen . getByTestId ( 'input' )
53
53
54
54
// calls `onEdit` when preview is focused
55
- fireEvent . focus ( preview )
55
+ await fireEvent . focus ( preview )
56
56
expect ( onEdit ) . toHaveBeenCalled ( )
57
57
58
58
// calls `onChange` with input on change
59
- userEvent . type ( input , 'World' )
59
+ await userEvent . type ( input , 'World' )
60
60
expect ( onChange ) . toHaveBeenCalledWith ( 'Hello World' )
61
61
62
62
// calls `onCancel` with previous value when "esc" pressed
63
- fireEvent . keyDown ( input , { key : 'Escape' } )
63
+ await fireEvent . keyDown ( input , { key : 'Escape' } )
64
64
expect ( onCancel ) . toHaveBeenCalledWith ( 'Hello ' )
65
65
66
66
// calls `onSubmit` with previous value when "enter" pressed after cancelling
67
- fireEvent . keyDown ( input , { key : 'Enter' } )
67
+ await fireEvent . keyDown ( input , { key : 'Enter' } )
68
68
expect ( onSubmit ) . toHaveBeenCalledWith ( 'Hello ' )
69
69
70
70
// TODO: ⚠️ we should make controlledInput for vue
71
+ // we didn't change the v-model but input.value changes
71
72
// remove this line, previous input value stays and next 2 expect fails
72
73
input . value = 'Hello '
73
74
74
75
// 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' } )
77
78
expect ( onSubmit ) . toHaveBeenCalledWith ( 'Hello World' )
78
79
79
80
// cancelling now returns new value
80
- fireEvent . keyDown ( input , { key : 'Escape' } )
81
+ await fireEvent . keyDown ( input , { key : 'Escape' } )
81
82
expect ( onCancel ) . toHaveBeenCalledWith ( 'Hello ' )
82
83
} )
83
84
84
85
it ( 'controlled: handles callbacks correctly' , async ( ) => {
86
+ useId . mockReturnValueOnce ( '1' )
85
87
const onChange = jest . fn ( )
86
88
const onCancel = jest . fn ( )
87
89
const onSubmit = jest . fn ( )
88
90
const onEdit = jest . fn ( )
89
91
90
92
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 } } )
92
94
93
- const preview = getByTestId ( 'preview' )
95
+ const preview = screen . getByTestId ( 'preview' )
94
96
95
- await userEvent . click ( preview )
96
- const input = getByTestId ( 'input' )
97
+ await fireEvent . touch ( preview )
98
+ const input = screen . getByTestId ( 'input' )
97
99
98
100
// calls `onEdit` when preview is focused
99
- fireEvent . focus ( preview )
101
+ await fireEvent . focus ( preview )
100
102
expect ( onEdit ) . toHaveBeenCalled ( )
101
103
102
104
// calls `onSubmit` with `value`
103
- fireEvent . keyDown ( input , { key : 'Enter' } )
105
+ await fireEvent . keyDown ( input , { key : 'Enter' } )
104
106
expect ( onSubmit ) . toHaveBeenCalledWith ( 'Hello ' )
105
107
106
108
// calls `onCancel` with `value`
107
- fireEvent . keyDown ( input , { key : 'Escape' } )
109
+ await fireEvent . keyDown ( input , { key : 'Escape' } )
108
110
expect ( onSubmit ) . toHaveBeenCalledWith ( 'Hello ' )
109
111
110
112
// 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' } )
112
117
expect ( onChange ) . toHaveBeenCalledWith ( 'Hello World' )
113
118
} )
114
119
115
120
test ( 'has the proper aria attributes' , async ( ) => {
116
121
const inlineAttrs = 'defaultValue=""'
117
- const { getByTestId } = renderComponent ( { inlineAttrs } )
122
+ renderComponent ( { inlineAttrs } )
118
123
119
- const preview = getByTestId ( 'preview' )
124
+ const preview = screen . getByTestId ( 'preview' )
120
125
121
126
await userEvent . click ( preview )
122
- const input = getByTestId ( 'input' )
127
+ const input = screen . getByTestId ( 'input' )
123
128
124
129
// preview and input do not have aria-disabled when `CEditable` is not disabled
125
130
expect ( preview ) . not . toHaveAttribute ( 'aria-disabled' )
@@ -128,9 +133,9 @@ test('has the proper aria attributes', async () => {
128
133
129
134
test ( 'has the proper aria attributes when disabled' , ( ) => {
130
135
const inlineAttrs = 'isDisabled defaultValue=""'
131
- const { getByTestId } = renderComponent ( { inlineAttrs } )
136
+ renderComponent ( { inlineAttrs } )
132
137
133
- const preview = getByTestId ( 'preview' )
138
+ const preview = screen . getByTestId ( 'preview' )
134
139
135
140
// preview does not have aria-disabled when `CEditable` is not disabled
136
141
expect ( preview ) . toHaveAttribute ( 'aria-disabled' , 'true' )
0 commit comments