Skip to content

Commit fbbcd2c

Browse files
authored
test(Switch): detach the touch controllers each test attaches (#13017)
* test(Switch): detach the touch controllers each test attaches A test drives a tap's lift on a controller that it never detaches. The lift starts the 300 ms wait for the browser's click. When the file ends first, the wait outlives the test environment. Its timer then calls removeEventListener on a window that the environment has torn down, and Vitest reports an unhandled error for the file. The file's afterEach now detaches every controller that its tests attach. Detach ends the wait and clears its timer. * Merge branch 'main' into switch-touch-test-detaches * Merge branch 'main' into switch-touch-test-detaches
1 parent 3920a07 commit fbbcd2c

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎packages/core/src/Switch/useSwitchTouch.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ const {SwitchTouchController} = await import('./useSwitchTouch');
8989
const {switchTouchGeometry} = await import('./switchTouch');
9090
const {trackStateOf} = await import('./switchFill');
9191

92+
/** Every controller a test attaches. The top-level afterEach detaches them. */
93+
const controllers: InstanceType<typeof SwitchTouchController>[] = [];
94+
9295
function mount() {
9396
// The theme's two fills, as the track's classes paint them.
9497
const style = document.createElement('style');
@@ -108,6 +111,7 @@ function mount() {
108111
false,
109112
switchTouchGeometry(40, 24),
110113
);
114+
controllers.push(controller);
111115
controller.setOtherTrack(trackStateOf({className: 'on'}));
112116
controller.attach({surface, input, thumb, track}, 'touch');
113117
const fade = animators.find(
@@ -146,6 +150,12 @@ const flush = async () => {
146150
};
147151

148152
afterEach(() => {
153+
// A lift starts a 300 ms wait for the browser's click. Detach ends the
154+
// wait, so its timer cannot fire after the test environment removes the
155+
// window.
156+
for (const controller of controllers.splice(0)) {
157+
controller.detach();
158+
}
149159
animators.length = 0;
150160
document.body.innerHTML = '';
151161
document.head.innerHTML = '';
@@ -450,8 +460,9 @@ describe('the first value change before any measurement', () => {
450460
// The theme's size for the state the switch attaches in, as its
451461
// compiled CSS sets it on the thumb.
452462
const style = document.createElement('style');
453-
if (attached != null)
463+
if (attached != null) {
454464
style.textContent = `div div { --_switch-thumb-width: ${attached}; }`;
465+
}
455466
document.head.append(style);
456467
return mount();
457468
}
@@ -531,6 +542,7 @@ describe('the first value change before any measurement', () => {
531542
false,
532543
switchTouchGeometry(40, 24),
533544
);
545+
controllers.push(controller);
534546
controller.attach({surface, input, thumb: thumbElement, track}, 'pointer');
535547

536548
layOut(track, 20);

0 commit comments

Comments
 (0)