|
| 1 | +// Solid's observe tier: the match publish inside `startTransition` is |
| 2 | +// declared to the attribution engine as the navigation, so the holds and |
| 3 | +// re-runs it causes are named after the route and the record spans from |
| 4 | +// the history change that started the load. `OBSERVE` is defined on the |
| 5 | +// dev build the tests resolve; in production it is undefined and the |
| 6 | +// declaration folds out. |
| 7 | +import { cleanup, render, screen, waitFor } from '@solidjs/testing-library' |
| 8 | +import { afterEach, beforeEach, expect, test } from 'vitest' |
| 9 | +import { attribution } from 'solid-js/attribution' |
| 10 | +import { |
| 11 | + Outlet, |
| 12 | + RouterProvider, |
| 13 | + createMemoryHistory, |
| 14 | + createRootRoute, |
| 15 | + createRoute, |
| 16 | + createRouter, |
| 17 | +} from '../src' |
| 18 | + |
| 19 | +beforeEach(() => attribution.enable({ log: false })) |
| 20 | +afterEach(() => { |
| 21 | + attribution.disable() |
| 22 | + cleanup() |
| 23 | +}) |
| 24 | + |
| 25 | +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)) |
| 26 | + |
| 27 | +function makeRouter(loaderMs: number) { |
| 28 | + const rootRoute = createRootRoute({ component: () => <Outlet /> }) |
| 29 | + const indexRoute = createRoute({ |
| 30 | + getParentRoute: () => rootRoute, |
| 31 | + path: '/', |
| 32 | + component: () => <div data-testid="home">Home</div>, |
| 33 | + }) |
| 34 | + const userRoute = createRoute({ |
| 35 | + getParentRoute: () => rootRoute, |
| 36 | + path: '/users/$id', |
| 37 | + loader: async ({ params }) => { |
| 38 | + await sleep(loaderMs) |
| 39 | + return { name: `user ${params.id}` } |
| 40 | + }, |
| 41 | + component: () => { |
| 42 | + const data = userRoute.useLoaderData() |
| 43 | + return <div data-testid="user">{data().name}</div> |
| 44 | + }, |
| 45 | + }) |
| 46 | + const routeTree = rootRoute.addChildren([indexRoute, userRoute]) |
| 47 | + return createRouter({ |
| 48 | + routeTree, |
| 49 | + history: createMemoryHistory({ initialEntries: ['/'] }), |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +test('the match publish is declared as the navigation, dated from the history change', async () => { |
| 54 | + const router = makeRouter(30) |
| 55 | + render(() => <RouterProvider router={router} />) |
| 56 | + await waitFor(() => expect(screen.getByTestId('home')).toBeTruthy()) |
| 57 | + // The initial load publishes matches for the location already shown: not |
| 58 | + // a navigation. |
| 59 | + expect(attribution.navigations()).toHaveLength(0) |
| 60 | + |
| 61 | + const requested = performance.now() |
| 62 | + await router.navigate({ to: '/users/$id', params: { id: '42' } }) |
| 63 | + await waitFor(() => expect(screen.getByTestId('user')).toBeTruthy()) |
| 64 | + await sleep(0) |
| 65 | + |
| 66 | + // One record for the navigation — the pending offer (a match with |
| 67 | + // `status: 'pending'`) is published undeclared. |
| 68 | + const navs = attribution.navigations() |
| 69 | + expect(navs).toHaveLength(1) |
| 70 | + const nav = navs[0]! |
| 71 | + expect(nav.name).toBe('/users/$id') |
| 72 | + expect(nav.to).toBe('/users/42') |
| 73 | + expect(nav.from).toBe('/') |
| 74 | + expect(nav.params).toEqual({ id: '42' }) |
| 75 | + expect(nav.outcome).toBe('committed') |
| 76 | + // `at` is the history change inside navigate(), before the loader ran: |
| 77 | + // the record spans the loader wait even though the publish came after it. |
| 78 | + expect(nav.at).toBeGreaterThanOrEqual(requested) |
| 79 | + expect(nav.at).toBeLessThan(requested + 30) |
| 80 | + expect(nav.settledMs!).toBeGreaterThanOrEqual(30) |
| 81 | +}) |
| 82 | + |
| 83 | +test('a navigation superseded before it published leaves one record for the destination that showed', async () => { |
| 84 | + const router = makeRouter(30) |
| 85 | + render(() => <RouterProvider router={router} />) |
| 86 | + await waitFor(() => expect(screen.getByTestId('home')).toBeTruthy()) |
| 87 | + |
| 88 | + const requested = performance.now() |
| 89 | + void router.navigate({ to: '/users/$id', params: { id: '1' } }) |
| 90 | + await sleep(5) |
| 91 | + await router.navigate({ to: '/users/$id', params: { id: '2' } }) |
| 92 | + await waitFor(() => |
| 93 | + expect(screen.getByTestId('user').textContent).toBe('user 2'), |
| 94 | + ) |
| 95 | + await sleep(0) |
| 96 | + |
| 97 | + const navs = attribution.navigations() |
| 98 | + expect(navs).toHaveLength(1) |
| 99 | + expect(navs[0]!.to).toBe('/users/2') |
| 100 | + // Dated from the first request: that is when the user started waiting. |
| 101 | + expect(navs[0]!.at).toBeLessThan(requested + 5) |
| 102 | +}) |
0 commit comments