Skip to content

Commit 1951b1b

Browse files
committed
make typescript happy
1 parent ec8e3cb commit 1951b1b

22 files changed

+272
-258
lines changed

packages/@headlessui-react/src/hooks/use-tree-walker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function useTreeWalker({
3535
let walk = walkRef.current
3636

3737
let acceptNode = Object.assign((node: HTMLElement) => accept(node), { acceptNode: accept })
38+
// @ts-expect-error This `false` is a simple small fix for older browsers
3839
let walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, acceptNode, false)
3940

4041
while (walker.nextNode()) walk(walker.currentNode as HTMLElement)

packages/@headlessui-react/src/test-utils/accessibility-assertions.ts

+34-33
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function assertMenuButton(
9191
expect(button).toHaveAttribute(attributeName, options.attributes[attributeName])
9292
}
9393
} catch (err) {
94-
Error.captureStackTrace(err, assertMenuButton)
94+
if (err instanceof Error) Error.captureStackTrace(err, assertMenuButton)
9595
throw err
9696
}
9797
}
@@ -105,7 +105,7 @@ export function assertMenuButtonLinkedWithMenu(button = getMenuButton(), menu =
105105
expect(button).toHaveAttribute('aria-controls', menu.getAttribute('id'))
106106
expect(menu).toHaveAttribute('aria-labelledby', button.getAttribute('id'))
107107
} catch (err) {
108-
Error.captureStackTrace(err, assertMenuButtonLinkedWithMenu)
108+
if (err instanceof Error) Error.captureStackTrace(err, assertMenuButtonLinkedWithMenu)
109109
throw err
110110
}
111111
}
@@ -118,7 +118,7 @@ export function assertMenuLinkedWithMenuItem(item: HTMLElement | null, menu = ge
118118
// Ensure link between menu & menu item is correct
119119
expect(menu).toHaveAttribute('aria-activedescendant', item.getAttribute('id'))
120120
} catch (err) {
121-
Error.captureStackTrace(err, assertMenuLinkedWithMenuItem)
121+
if (err instanceof Error) Error.captureStackTrace(err, assertMenuLinkedWithMenuItem)
122122
throw err
123123
}
124124
}
@@ -130,7 +130,7 @@ export function assertNoActiveMenuItem(menu = getMenu()) {
130130
// Ensure we don't have an active menu
131131
expect(menu).not.toHaveAttribute('aria-activedescendant')
132132
} catch (err) {
133-
Error.captureStackTrace(err, assertNoActiveMenuItem)
133+
if (err instanceof Error) Error.captureStackTrace(err, assertNoActiveMenuItem)
134134
throw err
135135
}
136136
}
@@ -183,7 +183,7 @@ export function assertMenu(
183183
assertNever(options.state)
184184
}
185185
} catch (err) {
186-
Error.captureStackTrace(err, assertMenu)
186+
if (err instanceof Error) Error.captureStackTrace(err, assertMenu)
187187
throw err
188188
}
189189
}
@@ -214,7 +214,7 @@ export function assertMenuItem(
214214
}
215215
}
216216
} catch (err) {
217-
Error.captureStackTrace(err, assertMenuItem)
217+
if (err instanceof Error) Error.captureStackTrace(err, assertMenuItem)
218218
throw err
219219
}
220220
}
@@ -311,7 +311,7 @@ export function assertListbox(
311311
assertNever(options.state)
312312
}
313313
} catch (err) {
314-
Error.captureStackTrace(err, assertListbox)
314+
if (err instanceof Error) Error.captureStackTrace(err, assertListbox)
315315
throw err
316316
}
317317
}
@@ -368,7 +368,7 @@ export function assertListboxButton(
368368
expect(button).toHaveAttribute(attributeName, options.attributes[attributeName])
369369
}
370370
} catch (err) {
371-
Error.captureStackTrace(err, assertListboxButton)
371+
if (err instanceof Error) Error.captureStackTrace(err, assertListboxButton)
372372
throw err
373373
}
374374
}
@@ -400,7 +400,7 @@ export function assertListboxLabel(
400400
expect(label).toHaveAttribute(attributeName, options.attributes[attributeName])
401401
}
402402
} catch (err) {
403-
Error.captureStackTrace(err, assertListboxLabel)
403+
if (err instanceof Error) Error.captureStackTrace(err, assertListboxLabel)
404404
throw err
405405
}
406406
}
@@ -417,7 +417,7 @@ export function assertListboxButtonLinkedWithListbox(
417417
expect(button).toHaveAttribute('aria-controls', listbox.getAttribute('id'))
418418
expect(listbox).toHaveAttribute('aria-labelledby', button.getAttribute('id'))
419419
} catch (err) {
420-
Error.captureStackTrace(err, assertListboxButtonLinkedWithListbox)
420+
if (err instanceof Error) Error.captureStackTrace(err, assertListboxButtonLinkedWithListbox)
421421
throw err
422422
}
423423
}
@@ -432,7 +432,7 @@ export function assertListboxLabelLinkedWithListbox(
432432

433433
expect(listbox).toHaveAttribute('aria-labelledby', label.getAttribute('id'))
434434
} catch (err) {
435-
Error.captureStackTrace(err, assertListboxLabelLinkedWithListbox)
435+
if (err instanceof Error) Error.captureStackTrace(err, assertListboxLabelLinkedWithListbox)
436436
throw err
437437
}
438438
}
@@ -448,7 +448,8 @@ export function assertListboxButtonLinkedWithListboxLabel(
448448
// Ensure link between button & label is correct
449449
expect(button).toHaveAttribute('aria-labelledby', `${label.id} ${button.id}`)
450450
} catch (err) {
451-
Error.captureStackTrace(err, assertListboxButtonLinkedWithListboxLabel)
451+
if (err instanceof Error)
452+
Error.captureStackTrace(err, assertListboxButtonLinkedWithListboxLabel)
452453
throw err
453454
}
454455
}
@@ -461,7 +462,7 @@ export function assertActiveListboxOption(item: HTMLElement | null, listbox = ge
461462
// Ensure link between listbox & listbox item is correct
462463
expect(listbox).toHaveAttribute('aria-activedescendant', item.getAttribute('id'))
463464
} catch (err) {
464-
Error.captureStackTrace(err, assertActiveListboxOption)
465+
if (err instanceof Error) Error.captureStackTrace(err, assertActiveListboxOption)
465466
throw err
466467
}
467468
}
@@ -473,7 +474,7 @@ export function assertNoActiveListboxOption(listbox = getListbox()) {
473474
// Ensure we don't have an active listbox
474475
expect(listbox).not.toHaveAttribute('aria-activedescendant')
475476
} catch (err) {
476-
Error.captureStackTrace(err, assertNoActiveListboxOption)
477+
if (err instanceof Error) Error.captureStackTrace(err, assertNoActiveListboxOption)
477478
throw err
478479
}
479480
}
@@ -482,7 +483,7 @@ export function assertNoSelectedListboxOption(items = getListboxOptions()) {
482483
try {
483484
for (let item of items) expect(item).not.toHaveAttribute('aria-selected')
484485
} catch (err) {
485-
Error.captureStackTrace(err, assertNoSelectedListboxOption)
486+
if (err instanceof Error) Error.captureStackTrace(err, assertNoSelectedListboxOption)
486487
throw err
487488
}
488489
}
@@ -530,7 +531,7 @@ export function assertListboxOption(
530531
}
531532
}
532533
} catch (err) {
533-
Error.captureStackTrace(err, assertListboxOption)
534+
if (err instanceof Error) Error.captureStackTrace(err, assertListboxOption)
534535
throw err
535536
}
536537
}
@@ -597,7 +598,7 @@ export function assertSwitch(
597598
assertNever(options.state)
598599
}
599600
} catch (err) {
600-
Error.captureStackTrace(err, assertSwitch)
601+
if (err instanceof Error) Error.captureStackTrace(err, assertSwitch)
601602
throw err
602603
}
603604
}
@@ -678,7 +679,7 @@ export function assertDisclosureButton(
678679
expect(button).toHaveAttribute(attributeName, options.attributes[attributeName])
679680
}
680681
} catch (err) {
681-
Error.captureStackTrace(err, assertDisclosureButton)
682+
if (err instanceof Error) Error.captureStackTrace(err, assertDisclosureButton)
682683
throw err
683684
}
684685
}
@@ -725,7 +726,7 @@ export function assertDisclosurePanel(
725726
assertNever(options.state)
726727
}
727728
} catch (err) {
728-
Error.captureStackTrace(err, assertDisclosurePanel)
729+
if (err instanceof Error) Error.captureStackTrace(err, assertDisclosurePanel)
729730
throw err
730731
}
731732
}
@@ -810,7 +811,7 @@ export function assertPopoverButton(
810811
expect(button).toHaveAttribute(attributeName, options.attributes[attributeName])
811812
}
812813
} catch (err) {
813-
Error.captureStackTrace(err, assertPopoverButton)
814+
if (err instanceof Error) Error.captureStackTrace(err, assertPopoverButton)
814815
throw err
815816
}
816817
}
@@ -857,7 +858,7 @@ export function assertPopoverPanel(
857858
assertNever(options.state)
858859
}
859860
} catch (err) {
860-
Error.captureStackTrace(err, assertPopoverPanel)
861+
if (err instanceof Error) Error.captureStackTrace(err, assertPopoverPanel)
861862
throw err
862863
}
863864
}
@@ -984,7 +985,7 @@ export function assertDialog(
984985
assertNever(options.state)
985986
}
986987
} catch (err) {
987-
Error.captureStackTrace(err, assertDialog)
988+
if (err instanceof Error) Error.captureStackTrace(err, assertDialog)
988989
throw err
989990
}
990991
}
@@ -1040,7 +1041,7 @@ export function assertDialogTitle(
10401041
assertNever(options.state)
10411042
}
10421043
} catch (err) {
1043-
Error.captureStackTrace(err, assertDialogTitle)
1044+
if (err instanceof Error) Error.captureStackTrace(err, assertDialogTitle)
10441045
throw err
10451046
}
10461047
}
@@ -1096,7 +1097,7 @@ export function assertDialogDescription(
10961097
assertNever(options.state)
10971098
}
10981099
} catch (err) {
1099-
Error.captureStackTrace(err, assertDialogDescription)
1100+
if (err instanceof Error) Error.captureStackTrace(err, assertDialogDescription)
11001101
throw err
11011102
}
11021103
}
@@ -1143,7 +1144,7 @@ export function assertDialogOverlay(
11431144
assertNever(options.state)
11441145
}
11451146
} catch (err) {
1146-
Error.captureStackTrace(err, assertDialogOverlay)
1147+
if (err instanceof Error) Error.captureStackTrace(err, assertDialogOverlay)
11471148
throw err
11481149
}
11491150
}
@@ -1185,7 +1186,7 @@ export function assertRadioGroupLabel(
11851186
expect(label).toHaveAttribute(attributeName, options.attributes[attributeName])
11861187
}
11871188
} catch (err) {
1188-
Error.captureStackTrace(err, assertRadioGroupLabel)
1189+
if (err instanceof Error) Error.captureStackTrace(err, assertRadioGroupLabel)
11891190
throw err
11901191
}
11911192
}
@@ -1267,7 +1268,7 @@ export function assertTabs(
12671268
}
12681269
}
12691270
} catch (err) {
1270-
Error.captureStackTrace(err, assertTabs)
1271+
if (err instanceof Error) Error.captureStackTrace(err, assertTabs)
12711272
throw err
12721273
}
12731274
}
@@ -1287,7 +1288,7 @@ export function assertActiveElement(element: HTMLElement | null) {
12871288
expect(document.activeElement?.outerHTML).toBe(element.outerHTML)
12881289
}
12891290
} catch (err) {
1290-
Error.captureStackTrace(err, assertActiveElement)
1291+
if (err instanceof Error) Error.captureStackTrace(err, assertActiveElement)
12911292
throw err
12921293
}
12931294
}
@@ -1297,7 +1298,7 @@ export function assertContainsActiveElement(element: HTMLElement | null) {
12971298
if (element === null) return expect(element).not.toBe(null)
12981299
expect(element.contains(document.activeElement)).toBe(true)
12991300
} catch (err) {
1300-
Error.captureStackTrace(err, assertContainsActiveElement)
1301+
if (err instanceof Error) Error.captureStackTrace(err, assertContainsActiveElement)
13011302
throw err
13021303
}
13031304
}
@@ -1311,7 +1312,7 @@ export function assertHidden(element: HTMLElement | null) {
13111312
expect(element).toHaveAttribute('hidden')
13121313
expect(element).toHaveStyle({ display: 'none' })
13131314
} catch (err) {
1314-
Error.captureStackTrace(err, assertHidden)
1315+
if (err instanceof Error) Error.captureStackTrace(err, assertHidden)
13151316
throw err
13161317
}
13171318
}
@@ -1323,7 +1324,7 @@ export function assertVisible(element: HTMLElement | null) {
13231324
expect(element).not.toHaveAttribute('hidden')
13241325
expect(element).not.toHaveStyle({ display: 'none' })
13251326
} catch (err) {
1326-
Error.captureStackTrace(err, assertVisible)
1327+
if (err instanceof Error) Error.captureStackTrace(err, assertVisible)
13271328
throw err
13281329
}
13291330
}
@@ -1336,7 +1337,7 @@ export function assertFocusable(element: HTMLElement | null) {
13361337

13371338
expect(isFocusableElement(element, FocusableMode.Strict)).toBe(true)
13381339
} catch (err) {
1339-
Error.captureStackTrace(err, assertFocusable)
1340+
if (err instanceof Error) Error.captureStackTrace(err, assertFocusable)
13401341
throw err
13411342
}
13421343
}
@@ -1347,7 +1348,7 @@ export function assertNotFocusable(element: HTMLElement | null) {
13471348

13481349
expect(isFocusableElement(element, FocusableMode.Strict)).toBe(false)
13491350
} catch (err) {
1350-
Error.captureStackTrace(err, assertNotFocusable)
1351+
if (err instanceof Error) Error.captureStackTrace(err, assertNotFocusable)
13511352
throw err
13521353
}
13531354
}

packages/@headlessui-react/src/test-utils/interactions.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export async function type(events: Partial<KeyboardEvent>[], element = document.
161161

162162
await new Promise(nextFrame)
163163
} catch (err) {
164-
Error.captureStackTrace(err, type)
164+
if (err instanceof Error) Error.captureStackTrace(err, type)
165165
throw err
166166
} finally {
167167
jest.useRealTimers()
@@ -224,7 +224,7 @@ export async function click(
224224

225225
await new Promise(nextFrame)
226226
} catch (err) {
227-
Error.captureStackTrace(err, click)
227+
if (err instanceof Error) Error.captureStackTrace(err, click)
228228
throw err
229229
}
230230
}
@@ -237,7 +237,7 @@ export async function focus(element: Document | Element | Window | Node | null)
237237

238238
await new Promise(nextFrame)
239239
} catch (err) {
240-
Error.captureStackTrace(err, focus)
240+
if (err instanceof Error) Error.captureStackTrace(err, focus)
241241
throw err
242242
}
243243
}
@@ -251,7 +251,7 @@ export async function mouseEnter(element: Document | Element | Window | null) {
251251

252252
await new Promise(nextFrame)
253253
} catch (err) {
254-
Error.captureStackTrace(err, mouseEnter)
254+
if (err instanceof Error) Error.captureStackTrace(err, mouseEnter)
255255
throw err
256256
}
257257
}
@@ -265,7 +265,7 @@ export async function mouseMove(element: Document | Element | Window | null) {
265265

266266
await new Promise(nextFrame)
267267
} catch (err) {
268-
Error.captureStackTrace(err, mouseMove)
268+
if (err instanceof Error) Error.captureStackTrace(err, mouseMove)
269269
throw err
270270
}
271271
}
@@ -281,7 +281,7 @@ export async function mouseLeave(element: Document | Element | Window | null) {
281281

282282
await new Promise(nextFrame)
283283
} catch (err) {
284-
Error.captureStackTrace(err, mouseLeave)
284+
if (err instanceof Error) Error.captureStackTrace(err, mouseLeave)
285285
throw err
286286
}
287287
}

0 commit comments

Comments
 (0)