Skip to content

Commit

Permalink
test: Add loader e2e test showcasing usage
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Dec 25, 2024
1 parent d402ae8 commit b48fc8f
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/e2e/next/cypress/e2e/shared/loader.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { testLoader } from 'e2e-shared/specs/loader.cy'

testLoader({ path: '/app/loader', nextJsRouter: 'app' })

testLoader({ path: '/pages/loader', nextJsRouter: 'pages' })
11 changes: 11 additions & 0 deletions packages/e2e/next/src/app/app/loader/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LoaderRenderer, loadSearchParams } from 'e2e-shared/specs/loader'
import type { SearchParams } from 'nuqs/server'

type PageProps = {
searchParams: Promise<SearchParams>
}

export default async function Page({ searchParams }: PageProps) {
const serverValues = await loadSearchParams(searchParams)
return <LoaderRenderer serverValues={serverValues} />
}
22 changes: 22 additions & 0 deletions packages/e2e/next/src/pages/pages/loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
type SearchParams,
LoaderRenderer,
loadSearchParams
} from 'e2e-shared/specs/loader'
import { GetServerSidePropsContext } from 'next'

type PageProps = {
serverValues: SearchParams
}

export default function Page({ serverValues }: PageProps) {
return <LoaderRenderer serverValues={serverValues} />
}

export async function getServerSideProps({ query }: GetServerSidePropsContext) {
return {
props: {
serverValues: loadSearchParams(query)
}
}
}
3 changes: 3 additions & 0 deletions packages/e2e/react-router/v6/cypress/e2e/shared/loader.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { testLoader } from 'e2e-shared/specs/loader.cy'

testLoader({ path: '/loader' })
1 change: 1 addition & 0 deletions packages/e2e/react-router/v6/src/react-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const router = createBrowserRouter(
<Route path="routing/useQueryStates/other" lazy={load(import('./routes/routing.useQueryStates.other'))} />
<Route path='shallow/useQueryState' lazy={load(import('./routes/shallow.useQueryState'))} />
<Route path='shallow/useQueryStates' lazy={load(import('./routes/shallow.useQueryStates'))} />
<Route path='loader' lazy={load(import('./routes/loader'))} />
</Route>
))

Expand Down
11 changes: 11 additions & 0 deletions packages/e2e/react-router/v6/src/routes/loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LoaderRenderer, loadSearchParams } from 'e2e-shared/specs/loader'
import { useLoaderData, type LoaderFunctionArgs } from 'react-router-dom'

export function loader({ request }: LoaderFunctionArgs) {
return loadSearchParams(request)
}

export default function Page() {
const serverValues = useLoaderData() as Awaited<ReturnType<typeof loader>>
return <LoaderRenderer serverValues={serverValues} />
}
1 change: 1 addition & 0 deletions packages/e2e/react-router/v7/app/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export default [
route('/routing/useQueryStates/other', './routes/routing.useQueryStates.other.tsx'),
route('/shallow/useQueryState', './routes/shallow.useQueryState.tsx'),
route('/shallow/useQueryStates', './routes/shallow.useQueryStates.tsx'),
route('/loader', './routes/loader.tsx')
])
] satisfies RouteConfig
13 changes: 13 additions & 0 deletions packages/e2e/react-router/v7/app/routes/loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { LoaderRenderer, loadSearchParams } from 'e2e-shared/specs/loader'
import type { LoaderFunctionArgs } from 'react-router'
import type { Route } from './+types/loader'

export function loader({ request }: LoaderFunctionArgs) {
return loadSearchParams(request)
}

export default function Page({
loaderData: serverValues
}: Route.ComponentProps) {
return <LoaderRenderer serverValues={serverValues} />
}
3 changes: 3 additions & 0 deletions packages/e2e/react-router/v7/cypress/e2e/shared/loader.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { testLoader } from 'e2e-shared/specs/loader.cy'

testLoader({ path: '/loader' })
12 changes: 12 additions & 0 deletions packages/e2e/remix/app/routes/loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { LoaderFunctionArgs } from '@remix-run/node'
import { useLoaderData } from '@remix-run/react'
import { LoaderRenderer, loadSearchParams } from 'e2e-shared/specs/loader'

export function loader({ request }: LoaderFunctionArgs) {
return loadSearchParams(request)
}

export default function Page() {
const serverValues = useLoaderData<typeof loader>()
return <LoaderRenderer serverValues={serverValues} />
}
3 changes: 3 additions & 0 deletions packages/e2e/remix/cypress/e2e/shared/loader.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { testLoader } from 'e2e-shared/specs/loader.cy'

testLoader({ path: '/loader' })
10 changes: 10 additions & 0 deletions packages/e2e/shared/specs/loader.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createTest } from '../create-test'

export const testLoader = createTest('Loader', ({ path }) => {
it('loads state from the URL', () => {
cy.visit(path + '?test=pass&int=42')
cy.contains('#hydration-marker', 'hydrated').should('be.hidden')
cy.get('#test').should('have.text', 'pass')
cy.get('#int').should('have.text', '42')
})
})
27 changes: 27 additions & 0 deletions packages/e2e/shared/specs/loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
createLoader,
type inferParserType,
parseAsInteger,
parseAsString
} from 'nuqs/server'

const searchParams = {
test: parseAsString,
int: parseAsInteger
}

export type SearchParams = inferParserType<typeof searchParams>
export const loadSearchParams = createLoader(searchParams)

type LoaderRendererProps = {
serverValues: inferParserType<typeof searchParams>
}

export function LoaderRenderer({ serverValues }: LoaderRendererProps) {
return (
<>
<pre id="test">{serverValues.test}</pre>
<pre id="int">{serverValues.int}</pre>
</>
)
}

0 comments on commit b48fc8f

Please sign in to comment.