Skip to content

Commit 7156de2

Browse files
authored
Update and fix tests (v10.1.7, #165)
1 parent d29344e commit 7156de2

File tree

6 files changed

+5360
-5390
lines changed

6 files changed

+5360
-5390
lines changed

docs/badges/umd.svg

+2-2
Loading

integration-tests/regression.test.js

+44-76
Original file line numberDiff line numberDiff line change
@@ -74,191 +74,159 @@ describe('Tram-One - regressions', () => {
7474
})
7575

7676
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()
8479

8580
// previously when interacting with an input, if the component would rerender
8681
// focus would be removed from the component and put on the body of the page
8782

8883
// focus on the input
89-
userEvent.click(getByLabelText(appContainer, 'New Task Label'))
84+
userEvent.click(getByLabelText(container, 'New Task Label'))
9085

9186
// verify that the element has focus (before we start changing text)
9287
await waitFor(() => {
93-
expect(getByLabelText(appContainer, 'New Task Label')).toHaveFocus()
88+
expect(getByLabelText(container, 'New Task Label')).toHaveFocus()
9489
})
9590

9691
// clear the input
97-
userEvent.type(getByLabelText(appContainer, 'New Task Label'), '{selectall}{backspace}')
92+
userEvent.type(getByLabelText(container, 'New Task Label'), '{selectall}{backspace}')
9893

9994
// wait for mutation observer to reapply focus
10095
await waitFor(() => {
101-
expect(getByLabelText(appContainer, 'New Task Label')).toHaveFocus()
96+
expect(getByLabelText(container, 'New Task Label')).toHaveFocus()
10297
})
10398

10499
// update the state by typing
105-
userEvent.type(getByLabelText(appContainer, 'New Task Label'), '0')
100+
userEvent.type(getByLabelText(container, 'New Task Label'), '0')
106101

107102
// 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')
109104

110105
// wait for mutation observer to re-attach focus
111106
// expect the input to keep focus after the change event
112107
await waitFor(() => {
113-
expect(getByLabelText(appContainer, 'New Task Label')).toHaveFocus()
108+
expect(getByLabelText(container, 'New Task Label')).toHaveFocus()
114109
})
115-
116-
// cleanup - remove app
117-
appContainer.remove()
118110
})
119111

120112
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()
128115

129116
// previously when interacting with an input, if the component would rerender
130117
// focus would be removed from the component and put on the body of the page
131118

132119
// focus on the first input
133-
userEvent.click(getByLabelText(appContainer, 'New Task Label'))
120+
userEvent.click(getByLabelText(container, 'New Task Label'))
134121

135122
// focus on the second input
136-
userEvent.click(getByLabelText(appContainer, 'New Task Type'))
123+
userEvent.click(getByLabelText(container, 'New Task Type'))
137124

138125
// verify that the element has focus (before we start changing text)
139126
await waitFor(() => {
140-
expect(getByLabelText(appContainer, 'New Task Type')).toHaveFocus()
127+
expect(getByLabelText(container, 'New Task Type')).toHaveFocus()
141128
})
142129

143130
// clear the input
144-
userEvent.type(getByLabelText(appContainer, 'New Task Type'), '{selectall}{backspace}')
131+
userEvent.type(getByLabelText(container, 'New Task Type'), '{selectall}{backspace}')
145132

146133
// wait for mutation observer to reapply focus
147134
await waitFor(() => {
148-
expect(getByLabelText(appContainer, 'New Task Type')).toHaveFocus()
135+
expect(getByLabelText(container, 'New Task Type')).toHaveFocus()
149136
})
150137

151138
// update the state by typing
152-
userEvent.type(getByLabelText(appContainer, 'New Task Type'), '0')
139+
userEvent.type(getByLabelText(container, 'New Task Type'), '0')
153140

154141
// 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')
156143

157144
// wait for mutation observer to re-attach focus
158145
// expect the input to keep focus after the change event
159146
await waitFor(() => {
160-
expect(getByLabelText(appContainer, 'New Task Type')).toHaveFocus()
147+
expect(getByLabelText(container, 'New Task Type')).toHaveFocus()
161148
})
162-
163-
// cleanup - remove app
164-
appContainer.remove()
165149
})
166150

167151
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()
175154

176155
// previously when interacting with an input, if both a parent and child element
177156
// would update, then focus would not reattach, and/or the value would not update correctly
178157

179158
// focus on the parent input
180-
userEvent.click(getByLabelText(appContainer, 'Mirror Input'))
159+
userEvent.click(getByLabelText(container, 'Mirror Input'))
181160

182161
// verify that the element has focus (before we start changing text)
183162
await waitFor(() => {
184-
expect(getByLabelText(appContainer, 'Mirror Input')).toHaveFocus()
163+
expect(getByLabelText(container, 'Mirror Input')).toHaveFocus()
185164
})
186165

187166
// update the state by typing
188-
userEvent.type(getByLabelText(appContainer, 'Mirror Input'), 'Test')
167+
userEvent.type(getByLabelText(container, 'Mirror Input'), 'Test')
189168

190169
// verify the element and it's child have the new value
191170
// the element should still have focus
192171
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')
196175
})
197176

198177
// repeat the test with the child element
199178
// focus on the child input
200-
userEvent.click(getByLabelText(appContainer, 'Sub Mirror Input'))
179+
userEvent.click(getByLabelText(container, 'Sub Mirror Input'))
201180

202181
// verify that the element has focus (before we start changing text)
203182
await waitFor(() => {
204-
expect(getByLabelText(appContainer, 'Sub Mirror Input')).toHaveFocus()
183+
expect(getByLabelText(container, 'Sub Mirror Input')).toHaveFocus()
205184
})
206185

207186
// update the state by typing
208-
userEvent.type(getByLabelText(appContainer, 'Sub Mirror Input'), ' Again')
187+
userEvent.type(getByLabelText(container, 'Sub Mirror Input'), ' Again')
209188

210189
// 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')
212191

213192
// verify the element and it's parent have the new value
214193
// the element should still have focus
215194
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')
219198
})
220-
221-
// cleanup - remove app
222-
appContainer.remove()
223199
})
224200

225201
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()
233204

234205
// previously when interacting with an input, if the number of elements decreased
235206
// an error was thrown because the element to focus on no longer existed
236207

237208
// focus on the child input
238-
userEvent.click(getByLabelText(appContainer, 'Sub Mirror Input'))
209+
userEvent.click(getByLabelText(container, 'Sub Mirror Input'))
239210

240211
// verify that the element has focus (before we start changing text)
241212
await waitFor(() => {
242-
expect(getByLabelText(appContainer, 'Sub Mirror Input')).toHaveFocus()
213+
expect(getByLabelText(container, 'Sub Mirror Input')).toHaveFocus()
243214
})
244215

245216
// update the state by typing
246-
userEvent.type(getByLabelText(appContainer, 'Sub Mirror Input'), 'Test')
217+
userEvent.type(getByLabelText(container, 'Sub Mirror Input'), 'Test')
247218

248219
// 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')
250221

251222
// verify the element and it's parent have the new value
252223
// also verify that the elements were added above it too (previously this would have failed)
253224
// the element should still have focus
254225
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')
259230
})
260-
261-
// cleanup - remove app
262-
appContainer.remove()
263231
})
264232
})

integration-tests/test-app/index.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,25 @@ const app = () => {
3131
}
3232

3333
const startApp = container => {
34-
if (container === undefined) {
35-
container = document.createElement('div')
36-
container.id = 'app'
34+
let appContainer = container
35+
if (!appContainer) {
36+
// before we setup the app, cleanup the document state if this was called before
37+
const previousApp = document.querySelector('#app')
38+
if (previousApp) previousApp.remove()
39+
40+
// setup the container for the app
41+
appContainer = document.createElement('div')
42+
appContainer.id = 'app'
43+
44+
// attach the container to the document
45+
// this is required, since focus and visibility checks depend on being in the document
46+
window.document.body.appendChild(appContainer)
3747
}
3848

39-
start(app, container)
49+
start(app, appContainer)
4050

4151
return {
42-
container
52+
container: appContainer
4353
}
4454
}
4555

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
22
setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],
3-
testEnvironment: 'jest-environment-jsdom-sixteen'
3+
testEnvironment: 'jsdom'
44
}

0 commit comments

Comments
 (0)