generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add multitenancy test for Next.js (#917)
- Loading branch information
Showing
63 changed files
with
1,262 additions
and
71 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,68 @@ | ||
import { createTest, type TestConfig } from 'e2e-shared/create-test' | ||
import { getOptionsUrl } from 'e2e-shared/lib/options' | ||
|
||
function testMultiTenant( | ||
options: TestConfig & { | ||
expectedPathname: string | ||
} | ||
) { | ||
const factory = createTest('Multitenant', ({ path }) => { | ||
for (const shallow of [true, false]) { | ||
for (const history of ['replace', 'push'] as const) { | ||
it(`Updates with ({ shallow: ${shallow}, history: ${history} })`, () => { | ||
cy.visit(getOptionsUrl(path, { shallow, history })) | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#client-state').should('be.empty') | ||
cy.get('#server-state').should('be.empty') | ||
cy.get('#client-tenant').should('have.text', 'david') | ||
cy.get('#server-tenant').should('have.text', 'david') | ||
cy.get('#router-pathname').should( | ||
'have.text', | ||
options.expectedPathname | ||
) | ||
cy.get('button').click() | ||
cy.get('#client-state').should('have.text', 'pass') | ||
cy.get('#client-tenant').should('have.text', 'david') | ||
cy.get('#server-tenant').should('have.text', 'david') | ||
cy.get('#router-pathname').should( | ||
'have.text', | ||
options.expectedPathname | ||
) | ||
if (shallow === false) { | ||
cy.get('#server-state').should('have.text', 'pass') | ||
} else { | ||
cy.get('#server-state').should('be.empty') | ||
} | ||
if (history !== 'push') { | ||
return | ||
} | ||
cy.go('back') | ||
cy.get('#client-tenant').should('have.text', 'david') | ||
cy.get('#server-tenant').should('have.text', 'david') | ||
cy.get('#client-state').should('be.empty') | ||
cy.get('#server-state').should('be.empty') | ||
cy.get('#router-pathname').should( | ||
'have.text', | ||
options.expectedPathname | ||
) | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
return factory(options) | ||
} | ||
|
||
testMultiTenant({ | ||
path: '/app/multitenant', | ||
nextJsRouter: 'app', | ||
description: 'Dynamic route', | ||
expectedPathname: '/app/multitenant' | ||
}) | ||
|
||
testMultiTenant({ | ||
path: '/pages/multitenant', | ||
nextJsRouter: 'pages', | ||
description: 'Dynamic route', | ||
expectedPathname: '/pages/multitenant/[tenant]' | ||
}) |
77 changes: 77 additions & 0 deletions
77
packages/e2e/next/cypress/e2e/shared/dynamic-segments.cy.ts
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,77 @@ | ||
import { testDynamicSegments } from 'e2e-shared/specs/dynamic-segments.cy' | ||
|
||
testDynamicSegments({ | ||
path: '/app/dynamic-segments/dynamic/segment', | ||
expectedSegments: ['segment'], | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testDynamicSegments({ | ||
path: '/pages/dynamic-segments/dynamic/segment', | ||
expectedSegments: ['segment'], | ||
nextJsRouter: 'pages' | ||
}) | ||
|
||
// Catch-all -- | ||
|
||
testDynamicSegments({ | ||
path: '/app/dynamic-segments/catch-all/foo', | ||
expectedSegments: ['foo'], | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testDynamicSegments({ | ||
path: '/pages/dynamic-segments/catch-all/foo', | ||
expectedSegments: ['foo'], | ||
nextJsRouter: 'pages' | ||
}) | ||
|
||
testDynamicSegments({ | ||
path: '/app/dynamic-segments/catch-all/a/b/c', | ||
expectedSegments: ['a', 'b', 'c'], | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testDynamicSegments({ | ||
path: '/pages/dynamic-segments/catch-all/a/b/c', | ||
expectedSegments: ['a', 'b', 'c'], | ||
nextJsRouter: 'pages' | ||
}) | ||
|
||
// Optional catch-all -- | ||
|
||
testDynamicSegments({ | ||
path: '/app/dynamic-segments/optional-catch-all', // no segments | ||
expectedSegments: [], | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testDynamicSegments({ | ||
path: '/pages/dynamic-segments/optional-catch-all', // no segments | ||
expectedSegments: [], | ||
nextJsRouter: 'pages' | ||
}) | ||
|
||
testDynamicSegments({ | ||
path: '/app/dynamic-segments/optional-catch-all/foo', | ||
expectedSegments: ['foo'], | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testDynamicSegments({ | ||
path: '/pages/dynamic-segments/optional-catch-all/foo', | ||
expectedSegments: ['foo'], | ||
nextJsRouter: 'pages' | ||
}) | ||
|
||
testDynamicSegments({ | ||
path: '/app/dynamic-segments/optional-catch-all/a/b/c', | ||
expectedSegments: ['a', 'b', 'c'], | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testDynamicSegments({ | ||
path: '/pages/dynamic-segments/optional-catch-all/a/b/c', | ||
expectedSegments: ['a', 'b', 'c'], | ||
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 { testPrettyUrls } from 'e2e-shared/specs/pretty-urls.cy' | ||
|
||
testPrettyUrls({ | ||
path: '/app/pretty-urls', | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testPrettyUrls({ | ||
path: '/pages/pretty-urls', | ||
nextJsRouter: 'pages' | ||
}) |
16 changes: 16 additions & 0 deletions
16
packages/e2e/next/src/app/app/(shared)/dynamic-segments/catch-all/[...segments]/client.tsx
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,16 @@ | ||
'use client' | ||
|
||
import { DisplaySegments } from 'e2e-shared/specs/dynamic-segments' | ||
import { useParams } from 'next/navigation' | ||
import { ReactNode } from 'react' | ||
|
||
export function ClientSegment({ children }: { children?: ReactNode }) { | ||
const params = useParams() | ||
const segments = params?.segments as string[] | ||
return ( | ||
<> | ||
{children} | ||
<DisplaySegments environment="client" segments={segments} /> | ||
</> | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/e2e/next/src/app/app/(shared)/dynamic-segments/catch-all/[...segments]/page.tsx
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,34 @@ | ||
import { Display } from 'e2e-shared/components/display' | ||
import { DisplaySegments, UrlControls } from 'e2e-shared/specs/dynamic-segments' | ||
import { createLoader, parseAsString, type SearchParams } from 'nuqs/server' | ||
import { Suspense } from 'react' | ||
import { ClientSegment } from './client' | ||
|
||
type PageProps = { | ||
params: Promise<{ segments: string[] }> | ||
searchParams: Promise<SearchParams> | ||
} | ||
|
||
const loadTest = createLoader({ | ||
test: parseAsString | ||
}) | ||
|
||
export default async function DynamicPage(props: PageProps) { | ||
const searchParams = await props.searchParams | ||
const { segments } = await props.params | ||
const { test: serverState } = loadTest(searchParams) | ||
return ( | ||
<> | ||
<Suspense> | ||
<UrlControls> | ||
<Display environment="server" state={serverState} /> | ||
</UrlControls> | ||
</Suspense> | ||
<Suspense> | ||
<ClientSegment> | ||
<DisplaySegments environment="server" segments={segments} /> | ||
</ClientSegment> | ||
</Suspense> | ||
</> | ||
) | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/e2e/next/src/app/app/(shared)/dynamic-segments/dynamic/[segment]/client.tsx
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,16 @@ | ||
'use client' | ||
|
||
import { DisplaySegments } from 'e2e-shared/specs/dynamic-segments' | ||
import { useParams } from 'next/navigation' | ||
import { ReactNode } from 'react' | ||
|
||
export function ClientSegment({ children }: { children?: ReactNode }) { | ||
const params = useParams() | ||
const segment = params?.segment as string | ||
return ( | ||
<> | ||
{children} | ||
<DisplaySegments environment="client" segments={[segment]} /> | ||
</> | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/e2e/next/src/app/app/(shared)/dynamic-segments/dynamic/[segment]/page.tsx
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,34 @@ | ||
import { Display } from 'e2e-shared/components/display' | ||
import { DisplaySegments, UrlControls } from 'e2e-shared/specs/dynamic-segments' | ||
import { createLoader, parseAsString, type SearchParams } from 'nuqs/server' | ||
import { Suspense } from 'react' | ||
import { ClientSegment } from './client' | ||
|
||
type PageProps = { | ||
params: Promise<{ segment: string }> | ||
searchParams: Promise<SearchParams> | ||
} | ||
|
||
const loadTest = createLoader({ | ||
test: parseAsString | ||
}) | ||
|
||
export default async function DynamicPage(props: PageProps) { | ||
const searchParams = await props.searchParams | ||
const { segment } = await props.params | ||
const { test: serverState } = loadTest(searchParams) | ||
return ( | ||
<> | ||
<Suspense> | ||
<UrlControls> | ||
<Display environment="server" state={serverState} /> | ||
</UrlControls> | ||
</Suspense> | ||
<Suspense> | ||
<ClientSegment> | ||
<DisplaySegments environment="server" segments={[segment]} /> | ||
</ClientSegment> | ||
</Suspense> | ||
</> | ||
) | ||
} |
16 changes: 16 additions & 0 deletions
16
.../next/src/app/app/(shared)/dynamic-segments/optional-catch-all/[[...segments]]/client.tsx
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,16 @@ | ||
'use client' | ||
|
||
import { DisplaySegments } from 'e2e-shared/specs/dynamic-segments' | ||
import { useParams } from 'next/navigation' | ||
import { ReactNode } from 'react' | ||
|
||
export function ClientSegment({ children }: { children?: ReactNode }) { | ||
const params = useParams() | ||
const segments = params?.segments as string[] | ||
return ( | ||
<> | ||
{children} | ||
<DisplaySegments environment="client" segments={segments} /> | ||
</> | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
...2e/next/src/app/app/(shared)/dynamic-segments/optional-catch-all/[[...segments]]/page.tsx
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,34 @@ | ||
import { Display } from 'e2e-shared/components/display' | ||
import { DisplaySegments, UrlControls } from 'e2e-shared/specs/dynamic-segments' | ||
import { createLoader, parseAsString, type SearchParams } from 'nuqs/server' | ||
import { Suspense } from 'react' | ||
import { ClientSegment } from './client' | ||
|
||
type PageProps = { | ||
params: Promise<{ segments: string[] }> | ||
searchParams: Promise<SearchParams> | ||
} | ||
|
||
const loadTest = createLoader({ | ||
test: parseAsString | ||
}) | ||
|
||
export default async function DynamicPage(props: PageProps) { | ||
const searchParams = await props.searchParams | ||
const { segments } = await props.params | ||
const { test: serverState } = loadTest(searchParams) | ||
return ( | ||
<> | ||
<Suspense> | ||
<UrlControls> | ||
<Display environment="server" state={serverState} /> | ||
</UrlControls> | ||
</Suspense> | ||
<Suspense> | ||
<ClientSegment> | ||
<DisplaySegments environment="server" segments={segments} /> | ||
</ClientSegment> | ||
</Suspense> | ||
</> | ||
) | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/e2e/next/src/app/app/(shared)/pretty-urls/page.tsx
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 { PrettyUrls } from 'e2e-shared/specs/pretty-urls' | ||
import { Suspense } from 'react' | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<PrettyUrls /> | ||
</Suspense> | ||
) | ||
} |
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
14 changes: 14 additions & 0 deletions
14
packages/e2e/next/src/app/app/multitenant/[tenant]/client-tenant.tsx
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,14 @@ | ||
'use client' | ||
|
||
import { useParams, usePathname } from 'next/navigation' | ||
|
||
export function TenantClient() { | ||
const params = useParams() | ||
const pathname = usePathname() | ||
return ( | ||
<> | ||
<p id="client-tenant">{params?.tenant}</p> | ||
<p id="router-pathname">{pathname}</p> | ||
</> | ||
) | ||
} |
Oops, something went wrong.