generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add loader e2e test showcasing usage
- Loading branch information
Showing
13 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</> | ||
) | ||
} |