diff --git a/src/Home.test.tsx b/src/Home.test.tsx new file mode 100644 index 0000000..ee8f1d3 --- /dev/null +++ b/src/Home.test.tsx @@ -0,0 +1,57 @@ +import { render, screen } from '@testing-library/react' +import { useEffect, useRef, type ReactNode } from 'react' +import { createMemoryRouter, RouterProvider } from 'react-router' +import { expect, test } from 'vitest' +import { BaoClient } from './api/bao' +import { SessionProvider } from './api/session' +import { useSession } from './api/useSession' +import { Home } from './Home' +import { Providers } from './test/providers' + +/** Signs in first, and only renders its children once the session exists. */ +function SignedIn({ children }: { children: ReactNode }) { + const { client, connect } = useSession() + // connect() isn't a stable function, so guard instead of listing it as a dependency (that would sign in forever). + const done = useRef(false) + useEffect(() => { + if (done.current) return + done.current = true + connect(new BaoClient('http://b', 't'), { accessor: 'a', display_name: 'alice', policies: [], ttl: 0, renewable: false }) + }) + return client ? children : null +} + +function setup(landing: ReactNode) { + const router = createMemoryRouter( + [ + { path: '/', element: landing }, + { path: '/connect', element:

connect page

}, + { path: '/identities', element:

identities page

}, + ], + { initialEntries: ['/'] }, + ) + render( + + + + + , + ) + return router +} + +test('sends a signed-out visitor to the connect page', async () => { + const router = setup() + expect(await screen.findByText('connect page')).toBeInTheDocument() + expect(router.state.location.pathname).toBe('/connect') +}) + +test('sends a signed-in visitor to identities', async () => { + const router = setup( + + + , + ) + expect(await screen.findByText('identities page')).toBeInTheDocument() + expect(router.state.location.pathname).toBe('/identities') +}) diff --git a/src/Home.tsx b/src/Home.tsx new file mode 100644 index 0000000..6357c94 --- /dev/null +++ b/src/Home.tsx @@ -0,0 +1,8 @@ +import { Navigate } from 'react-router' +import { useSession } from './api/useSession' + +/** The landing route: straight to the work when signed in, otherwise to the only page that can be used. */ +export function Home() { + const { client } = useSession() + return +} diff --git a/src/Layout.test.tsx b/src/Layout.test.tsx index 4b913c0..233fcf8 100644 --- a/src/Layout.test.tsx +++ b/src/Layout.test.tsx @@ -40,6 +40,16 @@ test('offers Connect only while signed out', async () => { expect(screen.queryByRole('link', { name: 'Connection' })).not.toBeInTheDocument() }) +test('shows the section links only while signed in', async () => { + const user = setup() + const names = ['Identities', 'Secrets', 'Graph'] + for (const n of names) expect(screen.queryByRole('link', { name: n })).not.toBeInTheDocument() + expect(screen.getByRole('link', { name: 'Connect' })).toBeInTheDocument() + + await user.click(screen.getByRole('button', { name: 'sign in' })) + for (const n of names) expect(screen.getByRole('link', { name: n })).toBeInTheDocument() +}) + test('while signed in, the signed-in name links to the connection details', async () => { const user = setup() await user.click(screen.getByRole('button', { name: 'sign in' })) diff --git a/src/Layout.tsx b/src/Layout.tsx index f930ee7..7ec5d7e 100644 --- a/src/Layout.tsx +++ b/src/Layout.tsx @@ -21,11 +21,13 @@ export function Layout() {