Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

setup-helpers.ts

43 lines944 Bytes

1export function getRequiredLink(
2 container: ParentNode,
3 testId: string,
4 cache?: Map<string, HTMLAnchorElement>,
5) {
6 const cachedLink = cache?.get(testId)
7 if (cachedLink) {
8 return cachedLink
9 }
10​
11 const link = container.querySelector<HTMLAnchorElement>(
12 `[data-testid="${testId}"]`,
13 )
14 if (!link) {
15 throw new Error(`Unable to find benchmark link: ${testId}`)
16 }
17​
18 cache?.set(testId, link)
19 return link
21​
22export async function waitForRequiredLink(
23 container: ParentNode,
24 testId: string,
25 cache?: Map<string, HTMLAnchorElement>,
26) {
27 for (let attempt = 0; attempt < 10; attempt++) {
28 const link = container.querySelector<HTMLAnchorElement>(
29 `[data-testid="${testId}"]`,
30 )
31​
32 if (link) {
33 cache?.set(testId, link)
34 return link
35 }
36​
37 await new Promise<void>((resolve) => {
38 requestAnimationFrame(() => resolve())
39 })
40 }
41​
42 return getRequiredLink(container, testId, cache)