@@ -74,191 +74,159 @@ describe('Tram-One - regressions', () => {
74
74
} )
75
75
76
76
it ( 'should keep focus on inputs when components would rerender' , async ( ) => {
77
- // for focus to work correctly, the element needs to be attached to the document
78
- const appContainer = document . createElement ( 'div' )
79
- appContainer . id = 'app'
80
- window . document . body . appendChild ( appContainer )
81
-
82
- // start the app using a css selector
83
- startApp ( '#app' )
77
+ // start the app
78
+ const { container } = startApp ( )
84
79
85
80
// previously when interacting with an input, if the component would rerender
86
81
// focus would be removed from the component and put on the body of the page
87
82
88
83
// focus on the input
89
- userEvent . click ( getByLabelText ( appContainer , 'New Task Label' ) )
84
+ userEvent . click ( getByLabelText ( container , 'New Task Label' ) )
90
85
91
86
// verify that the element has focus (before we start changing text)
92
87
await waitFor ( ( ) => {
93
- expect ( getByLabelText ( appContainer , 'New Task Label' ) ) . toHaveFocus ( )
88
+ expect ( getByLabelText ( container , 'New Task Label' ) ) . toHaveFocus ( )
94
89
} )
95
90
96
91
// clear the input
97
- userEvent . type ( getByLabelText ( appContainer , 'New Task Label' ) , '{selectall}{backspace}' )
92
+ userEvent . type ( getByLabelText ( container , 'New Task Label' ) , '{selectall}{backspace}' )
98
93
99
94
// wait for mutation observer to reapply focus
100
95
await waitFor ( ( ) => {
101
- expect ( getByLabelText ( appContainer , 'New Task Label' ) ) . toHaveFocus ( )
96
+ expect ( getByLabelText ( container , 'New Task Label' ) ) . toHaveFocus ( )
102
97
} )
103
98
104
99
// update the state by typing
105
- userEvent . type ( getByLabelText ( appContainer , 'New Task Label' ) , '0' )
100
+ userEvent . type ( getByLabelText ( container , 'New Task Label' ) , '0' )
106
101
107
102
// verify the element has the new value
108
- expect ( getByLabelText ( appContainer , 'New Task Label' ) ) . toHaveValue ( '0' )
103
+ expect ( getByLabelText ( container , 'New Task Label' ) ) . toHaveValue ( '0' )
109
104
110
105
// wait for mutation observer to re-attach focus
111
106
// expect the input to keep focus after the change event
112
107
await waitFor ( ( ) => {
113
- expect ( getByLabelText ( appContainer , 'New Task Label' ) ) . toHaveFocus ( )
108
+ expect ( getByLabelText ( container , 'New Task Label' ) ) . toHaveFocus ( )
114
109
} )
115
-
116
- // cleanup - remove app
117
- appContainer . remove ( )
118
110
} )
119
111
120
112
it ( 'should keep focus on the most recent input when components rerender' , async ( ) => {
121
- // for focus to work correctly, the element needs to be attached to the document
122
- const appContainer = document . createElement ( 'div' )
123
- appContainer . id = 'app'
124
- window . document . body . appendChild ( appContainer )
125
-
126
- // start the app using a css selector
127
- startApp ( '#app' )
113
+ // start the app
114
+ const { container } = startApp ( )
128
115
129
116
// previously when interacting with an input, if the component would rerender
130
117
// focus would be removed from the component and put on the body of the page
131
118
132
119
// focus on the first input
133
- userEvent . click ( getByLabelText ( appContainer , 'New Task Label' ) )
120
+ userEvent . click ( getByLabelText ( container , 'New Task Label' ) )
134
121
135
122
// focus on the second input
136
- userEvent . click ( getByLabelText ( appContainer , 'New Task Type' ) )
123
+ userEvent . click ( getByLabelText ( container , 'New Task Type' ) )
137
124
138
125
// verify that the element has focus (before we start changing text)
139
126
await waitFor ( ( ) => {
140
- expect ( getByLabelText ( appContainer , 'New Task Type' ) ) . toHaveFocus ( )
127
+ expect ( getByLabelText ( container , 'New Task Type' ) ) . toHaveFocus ( )
141
128
} )
142
129
143
130
// clear the input
144
- userEvent . type ( getByLabelText ( appContainer , 'New Task Type' ) , '{selectall}{backspace}' )
131
+ userEvent . type ( getByLabelText ( container , 'New Task Type' ) , '{selectall}{backspace}' )
145
132
146
133
// wait for mutation observer to reapply focus
147
134
await waitFor ( ( ) => {
148
- expect ( getByLabelText ( appContainer , 'New Task Type' ) ) . toHaveFocus ( )
135
+ expect ( getByLabelText ( container , 'New Task Type' ) ) . toHaveFocus ( )
149
136
} )
150
137
151
138
// update the state by typing
152
- userEvent . type ( getByLabelText ( appContainer , 'New Task Type' ) , '0' )
139
+ userEvent . type ( getByLabelText ( container , 'New Task Type' ) , '0' )
153
140
154
141
// verify the element has the new value
155
- expect ( getByLabelText ( appContainer , 'New Task Type' ) ) . toHaveValue ( '0' )
142
+ expect ( getByLabelText ( container , 'New Task Type' ) ) . toHaveValue ( '0' )
156
143
157
144
// wait for mutation observer to re-attach focus
158
145
// expect the input to keep focus after the change event
159
146
await waitFor ( ( ) => {
160
- expect ( getByLabelText ( appContainer , 'New Task Type' ) ) . toHaveFocus ( )
147
+ expect ( getByLabelText ( container , 'New Task Type' ) ) . toHaveFocus ( )
161
148
} )
162
-
163
- // cleanup - remove app
164
- appContainer . remove ( )
165
149
} )
166
150
167
151
it ( 'should keep focus when both the parent and child element would update' , async ( ) => {
168
- // for focus to work correctly, the element needs to be attached to the document
169
- const appContainer = document . createElement ( 'div' )
170
- appContainer . id = 'app'
171
- window . document . body . appendChild ( appContainer )
172
-
173
- // start the app using a css selector
174
- startApp ( '#app' )
152
+ // start the app
153
+ const { container } = startApp ( )
175
154
176
155
// previously when interacting with an input, if both a parent and child element
177
156
// would update, then focus would not reattach, and/or the value would not update correctly
178
157
179
158
// focus on the parent input
180
- userEvent . click ( getByLabelText ( appContainer , 'Mirror Input' ) )
159
+ userEvent . click ( getByLabelText ( container , 'Mirror Input' ) )
181
160
182
161
// verify that the element has focus (before we start changing text)
183
162
await waitFor ( ( ) => {
184
- expect ( getByLabelText ( appContainer , 'Mirror Input' ) ) . toHaveFocus ( )
163
+ expect ( getByLabelText ( container , 'Mirror Input' ) ) . toHaveFocus ( )
185
164
} )
186
165
187
166
// update the state by typing
188
- userEvent . type ( getByLabelText ( appContainer , 'Mirror Input' ) , 'Test' )
167
+ userEvent . type ( getByLabelText ( container , 'Mirror Input' ) , 'Test' )
189
168
190
169
// verify the element and it's child have the new value
191
170
// the element should still have focus
192
171
await waitFor ( ( ) => {
193
- expect ( getByLabelText ( appContainer , 'Mirror Input' ) ) . toHaveFocus ( )
194
- expect ( getByLabelText ( appContainer , 'Mirror Input' ) ) . toHaveValue ( 'Test' )
195
- expect ( getByLabelText ( appContainer , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test' )
172
+ expect ( getByLabelText ( container , 'Mirror Input' ) ) . toHaveFocus ( )
173
+ expect ( getByLabelText ( container , 'Mirror Input' ) ) . toHaveValue ( 'Test' )
174
+ expect ( getByLabelText ( container , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test' )
196
175
} )
197
176
198
177
// repeat the test with the child element
199
178
// focus on the child input
200
- userEvent . click ( getByLabelText ( appContainer , 'Sub Mirror Input' ) )
179
+ userEvent . click ( getByLabelText ( container , 'Sub Mirror Input' ) )
201
180
202
181
// verify that the element has focus (before we start changing text)
203
182
await waitFor ( ( ) => {
204
- expect ( getByLabelText ( appContainer , 'Sub Mirror Input' ) ) . toHaveFocus ( )
183
+ expect ( getByLabelText ( container , 'Sub Mirror Input' ) ) . toHaveFocus ( )
205
184
} )
206
185
207
186
// update the state by typing
208
- userEvent . type ( getByLabelText ( appContainer , 'Sub Mirror Input' ) , ' Again' )
187
+ userEvent . type ( getByLabelText ( container , 'Sub Mirror Input' ) , ' Again' )
209
188
210
189
// verify the element has the new value
211
- expect ( getByLabelText ( appContainer , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test Again' )
190
+ expect ( getByLabelText ( container , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test Again' )
212
191
213
192
// verify the element and it's parent have the new value
214
193
// the element should still have focus
215
194
await waitFor ( ( ) => {
216
- expect ( getByLabelText ( appContainer , 'Sub Mirror Input' ) ) . toHaveFocus ( )
217
- expect ( getByLabelText ( appContainer , 'Mirror Input' ) ) . toHaveValue ( 'Test Again' )
218
- expect ( getByLabelText ( appContainer , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test Again' )
195
+ expect ( getByLabelText ( container , 'Sub Mirror Input' ) ) . toHaveFocus ( )
196
+ expect ( getByLabelText ( container , 'Mirror Input' ) ) . toHaveValue ( 'Test Again' )
197
+ expect ( getByLabelText ( container , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test Again' )
219
198
} )
220
-
221
- // cleanup - remove app
222
- appContainer . remove ( )
223
199
} )
224
200
225
201
it ( 'should not error when reseting focus if the number of elements changed' , async ( ) => {
226
- // for focus to work correctly, the element needs to be attached to the document
227
- const appContainer = document . createElement ( 'div' )
228
- appContainer . id = 'app'
229
- window . document . body . appendChild ( appContainer )
230
-
231
- // start the app using a css selector
232
- startApp ( '#app' )
202
+ // start the app
203
+ const { container } = startApp ( )
233
204
234
205
// previously when interacting with an input, if the number of elements decreased
235
206
// an error was thrown because the element to focus on no longer existed
236
207
237
208
// focus on the child input
238
- userEvent . click ( getByLabelText ( appContainer , 'Sub Mirror Input' ) )
209
+ userEvent . click ( getByLabelText ( container , 'Sub Mirror Input' ) )
239
210
240
211
// verify that the element has focus (before we start changing text)
241
212
await waitFor ( ( ) => {
242
- expect ( getByLabelText ( appContainer , 'Sub Mirror Input' ) ) . toHaveFocus ( )
213
+ expect ( getByLabelText ( container , 'Sub Mirror Input' ) ) . toHaveFocus ( )
243
214
} )
244
215
245
216
// update the state by typing
246
- userEvent . type ( getByLabelText ( appContainer , 'Sub Mirror Input' ) , 'Test' )
217
+ userEvent . type ( getByLabelText ( container , 'Sub Mirror Input' ) , 'Test' )
247
218
248
219
// verify the element has the new value
249
- expect ( getByLabelText ( appContainer , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test' )
220
+ expect ( getByLabelText ( container , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test' )
250
221
251
222
// verify the element and it's parent have the new value
252
223
// also verify that the elements were added above it too (previously this would have failed)
253
224
// the element should still have focus
254
225
await waitFor ( ( ) => {
255
- expect ( getByLabelText ( appContainer , 'Sub Mirror Input' ) ) . toHaveFocus ( )
256
- expect ( queryAllByText ( appContainer , '-' ) ) . toHaveLength ( 4 )
257
- expect ( getByLabelText ( appContainer , 'Mirror Input' ) ) . toHaveValue ( 'Test' )
258
- expect ( getByLabelText ( appContainer , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test' )
226
+ expect ( getByLabelText ( container , 'Sub Mirror Input' ) ) . toHaveFocus ( )
227
+ expect ( queryAllByText ( container , '-' ) ) . toHaveLength ( 4 )
228
+ expect ( getByLabelText ( container , 'Mirror Input' ) ) . toHaveValue ( 'Test' )
229
+ expect ( getByLabelText ( container , 'Sub Mirror Input' ) ) . toHaveValue ( 'Test' )
259
230
} )
260
-
261
- // cleanup - remove app
262
- appContainer . remove ( )
263
231
} )
264
232
} )
0 commit comments