|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | +import { createRoutesStub, useLoaderData, useNavigation } from 'react-router' |
| 9 | +import { expect, test } from 'vitest' |
| 10 | +import { render } from 'vitest-browser-react' |
| 11 | + |
| 12 | +import { Tabs } from '~/ui/lib/Tabs' |
| 13 | + |
| 14 | +import { QueryParamTabs } from './QueryParamTabs' |
| 15 | + |
| 16 | +test('waits for the loader before showing the new tab', async () => { |
| 17 | + let finishLoad = () => {} |
| 18 | + // tab each render of the second panel saw in loader data |
| 19 | + const panelTwoRenders: string[] = [] |
| 20 | + |
| 21 | + function PanelTwo() { |
| 22 | + const { tab } = useLoaderData<{ tab: string }>() |
| 23 | + panelTwoRenders.push(tab) |
| 24 | + return <p>Panel two</p> |
| 25 | + } |
| 26 | + |
| 27 | + function Page() { |
| 28 | + // like the loading bar in RootLayout, which rerenders while the nav is pending |
| 29 | + const navigation = useNavigation() |
| 30 | + return ( |
| 31 | + <QueryParamTabs defaultValue="one"> |
| 32 | + <Tabs.List> |
| 33 | + <Tabs.Trigger value="one">One</Tabs.Trigger> |
| 34 | + <Tabs.Trigger value="two">Two</Tabs.Trigger> |
| 35 | + </Tabs.List> |
| 36 | + <Tabs.Content value="one"> |
| 37 | + <p>Panel one</p> |
| 38 | + </Tabs.Content> |
| 39 | + <Tabs.Content value="two"> |
| 40 | + <PanelTwo /> |
| 41 | + </Tabs.Content> |
| 42 | + <p>Navigation {navigation.state}</p> |
| 43 | + </QueryParamTabs> |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + const Stub = createRoutesStub([ |
| 48 | + { |
| 49 | + path: '/', |
| 50 | + Component: Page, |
| 51 | + // like a page loader that only prefetches the current tab's data |
| 52 | + loader: async ({ request }) => { |
| 53 | + const tab = new URL(request.url).searchParams.get('tab') ?? 'one' |
| 54 | + if (tab === 'two') await new Promise<void>((resolve) => (finishLoad = resolve)) |
| 55 | + return { tab } |
| 56 | + }, |
| 57 | + }, |
| 58 | + ]) |
| 59 | + |
| 60 | + const screen = await render(<Stub />) |
| 61 | + await expect.element(screen.getByText('Panel one')).toBeVisible() |
| 62 | + |
| 63 | + await screen.getByRole('tab', { name: 'Two' }).click() |
| 64 | + await expect.element(screen.getByText('Navigation loading')).toBeInTheDocument() |
| 65 | + // still on the old tab while the new tab's loader runs |
| 66 | + await expect.element(screen.getByText('Panel one')).toBeVisible() |
| 67 | + expect(panelTwoRenders).toEqual([]) |
| 68 | + |
| 69 | + finishLoad() |
| 70 | + await expect.element(screen.getByText('Panel two')).toBeVisible() |
| 71 | + expect(panelTwoRenders).not.toContain('one') |
| 72 | +}) |
0 commit comments