Open History to see the last commit
1import type { NavigateOptions } from '@tanstack/router-core'
2import type * as App from './app'
3import { getRequiredLink, waitForRequiredLink } from '../setup-helpers'
4β
5const appModulePath = './dist/app.js'
6const { mountTestApp } = (await import(
7 /* @vite-ignore */ appModulePath
8)) as typeof App
9β
10export function setup() {
11 if (process.env.NODE_ENV !== 'production') {
12 console.warn(
13 'client-nav benchmark is running without NODE_ENV=production; React dev overhead will dominate results.',
14 )
15 }
16β
17 let container: HTMLDivElement | undefined = undefined
18 let unmount: (() => void) | undefined = undefined
19 let unsub = () => {}
20 let stepIndex = 0
21 let next: () => Promise<void> = () => Promise.reject('Test not initialized')
22β
23 async function before() {
24 stepIndex = 0
25 container = document.createElement('div')
26 document.body.append(container)
27β
28 const { router, unmount: dispose } = mountTestApp(container)
29 unmount = dispose
30β
31 let resolveRendered: () => void = () => {}
32 unsub = router.subscribe('onRendered', () => {
33 resolveRendered()
34 })
35β
36 const navigate = (opts: NavigateOptions) =>
37 new Promise<void>((resolveNext) => {
38 resolveRendered = resolveNext
39 router.navigate(opts)
40 })
41β
42 const click = (testId: string, cache?: Map<string, HTMLAnchorElement>) =>
43 new Promise<void>((resolveNext) => {
44 resolveRendered = resolveNext
45β
46 getRequiredLink(container!, testId, cache).dispatchEvent(
47 new MouseEvent('click', {
48 bubbles: true,
49 cancelable: true,
50 button: 0,
51 }),
52 )
53 })
54β
55 await router.load()
56β
57 const cachedLinks = new Map<string, HTMLAnchorElement>()
58 for (const testId of ['go-items-1', 'go-items-2', 'go-search', 'go-ctx']) {
59 await waitForRequiredLink(container, testId, cachedLinks)
60 }
61β
62 const steps = [
63 () => click('go-items-1', cachedLinks),
64 () => click('items-details'),
65 () =>
66 navigate({
67 to: '/items/$id/details',
68 params: { id: 2 },
69 replace: true,
70 }),
71 () => click('items-parent'),
72 () => click('go-search', cachedLinks),
73 () => click('search-next-page'),
74 () =>
75 navigate({
76 to: '/search',
77 search: { page: 1, filter: 'all' },
78 replace: true,
79 }),
80 () => click('go-ctx', cachedLinks),
81 () =>
82 navigate({
83 to: '/ctx/$id',
84 params: { id: 2 },
85 replace: true,
86 }),
87 () => click('go-items-2', cachedLinks),
88 ] as const
89β
90 next = () => {
91 const step = steps[stepIndex % steps.length]!
92 stepIndex += 1
93 return step()
94 }
95 }
96β
97 function after() {
98 unmount?.()
99 container?.remove()
100 unsub()
101 }
102β
103 function tick() {
104 return next()
105 }
106β
107 return {
108 before,
109 tick,
110 after,
111 }
112}