Skip to content

Commit ddc5151

Browse files
committed
test: Add routing e2e test
1 parent eb626c7 commit ddc5151

37 files changed

+352
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { testRouting } from 'e2e-shared/specs/routing.cy'
2+
3+
testRouting({
4+
path: '/app/routing/useQueryState',
5+
hook: 'useQueryState',
6+
nextJsRouter: 'app'
7+
})
8+
9+
testRouting({
10+
path: '/app/routing/useQueryStates',
11+
hook: 'useQueryStates',
12+
nextJsRouter: 'app'
13+
})
14+
15+
testRouting({
16+
path: '/pages/routing/useQueryState',
17+
hook: 'useQueryState',
18+
nextJsRouter: 'pages'
19+
})
20+
21+
testRouting({
22+
path: '/pages/routing/useQueryStates',
23+
hook: 'useQueryStates',
24+
nextJsRouter: 'pages'
25+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
import { Suspense } from 'react'
3+
4+
export default function Page() {
5+
return (
6+
<Suspense>
7+
<RoutingUseQueryState path="/app/routing/useQueryState" />
8+
</Suspense>
9+
)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
import { Suspense } from 'react'
3+
4+
export default function Page() {
5+
return (
6+
<Suspense>
7+
<RoutingUseQueryState path="/app/routing/useQueryState" />
8+
</Suspense>
9+
)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
import { Suspense } from 'react'
3+
4+
export default function Page() {
5+
return (
6+
<Suspense>
7+
<RoutingUseQueryStates path="/app/routing/useQueryStates" />
8+
</Suspense>
9+
)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
import { Suspense } from 'react'
3+
4+
export default function Page() {
5+
return (
6+
<Suspense>
7+
<RoutingUseQueryStates path="/app/routing/useQueryStates" />
8+
</Suspense>
9+
)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/pages/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/pages/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/pages/routing/useQueryStates" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/pages/routing/useQueryStates" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { testRouting } from 'e2e-shared/specs/routing.cy'
2+
3+
testRouting({
4+
path: '/routing/useQueryState',
5+
hook: 'useQueryState',
6+
shallowOptions: [false] // todo: Enable shallow routing
7+
})
8+
9+
testRouting({
10+
path: '/routing/useQueryStates',
11+
hook: 'useQueryStates',
12+
shallowOptions: [false] // todo: Enable shallow routing
13+
})

packages/e2e/react-router/v6/src/react-router.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const router = createBrowserRouter(
2525
<Route path='linking/useQueryStates/other' lazy={load(import('./routes/linking.useQueryStates.other'))} />
2626
<Route path='push/useQueryState' lazy={load(import('./routes/push.useQueryState'))} />
2727
<Route path='push/useQueryStates' lazy={load(import('./routes/push.useQueryStates'))} />
28+
<Route path="routing/useQueryState" lazy={load(import('./routes/routing.useQueryState'))} />
29+
<Route path="routing/useQueryState/other" lazy={load(import('./routes/routing.useQueryState.other'))} />
30+
<Route path="routing/useQueryStates" lazy={load(import('./routes/routing.useQueryStates'))} />
31+
<Route path="routing/useQueryStates/other" lazy={load(import('./routes/routing.useQueryStates.other'))} />
2832
</Route>
2933
))
3034

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/routing/useQueryStates" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/routing/useQueryStates" />
5+
}

packages/e2e/react-router/v7/app/routes.ts

+4
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ export default [
1212
route('/linking/useQueryStates/other', './routes/linking.useQueryStates.other.tsx'),
1313
route('/push/useQueryState', './routes/push.useQueryState.tsx'),
1414
route('/push/useQueryStates', './routes/push.useQueryStates.tsx'),
15+
route('/routing/useQueryState', './routes/routing.useQueryState.tsx'),
16+
route('/routing/useQueryState/other', './routes/routing.useQueryState.other.tsx'),
17+
route('/routing/useQueryStates', './routes/routing.useQueryStates.tsx'),
18+
route('/routing/useQueryStates/other', './routes/routing.useQueryStates.other.tsx')
1519
])
1620
] satisfies RouteConfig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/routing/useQueryStates" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/routing/useQueryStates" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { testRouting } from 'e2e-shared/specs/routing.cy'
2+
3+
testRouting({
4+
path: '/routing/useQueryState',
5+
hook: 'useQueryState',
6+
shallowOptions: [false] // todo: Enable shallow routing
7+
})
8+
9+
testRouting({
10+
path: '/routing/useQueryStates',
11+
hook: 'useQueryStates',
12+
shallowOptions: [false] // todo: Enable shallow routing
13+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { testRouting } from 'e2e-shared/specs/routing.cy'
2+
3+
testRouting({
4+
path: '/routing/useQueryState',
5+
hook: 'useQueryState',
6+
shallowOptions: [false] // todo: Enable shallow routing
7+
})
8+
9+
testRouting({
10+
path: '/routing/useQueryStates',
11+
hook: 'useQueryStates',
12+
shallowOptions: [false] // todo: Enable shallow routing
13+
})

packages/e2e/react/src/routes.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const routes: Record<string, React.LazyExoticComponent<() => JSX.Element>> = {
1111
'/linking/useQueryState/other': lazy(() => import('./routes/linking.useQueryState.other')),
1212
'/linking/useQueryStates': lazy(() => import('./routes/linking.useQueryStates')),
1313
'/linking/useQueryStates/other': lazy(() => import('./routes/linking.useQueryStates.other')),
14+
'/routing/useQueryState': lazy(() => import('./routes/routing.useQueryState')),
15+
'/routing/useQueryState/other': lazy(() => import('./routes/routing.useQueryState.other')),
16+
'/routing/useQueryStates': lazy(() => import('./routes/routing.useQueryStates')),
17+
'/routing/useQueryStates/other': lazy(() => import('./routes/routing.useQueryStates.other'))
1418
}
1519

1620
export function Router() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/routing/useQueryStates" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/routing/useQueryStates" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryState } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryState path="/routing/useQueryState" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/routing/useQueryStates" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { RoutingUseQueryStates } from 'e2e-shared/specs/routing'
2+
3+
export default function Page() {
4+
return <RoutingUseQueryStates path="/routing/useQueryStates" />
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { testRouting } from 'e2e-shared/specs/routing.cy'
2+
3+
testRouting({
4+
path: '/routing/useQueryState',
5+
hook: 'useQueryState',
6+
shallowOptions: [false] // todo: Enable shallow routing
7+
})
8+
9+
testRouting({
10+
path: '/routing/useQueryStates',
11+
hook: 'useQueryStates',
12+
shallowOptions: [false] // todo: Enable shallow routing
13+
})

packages/e2e/shared/create-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function createTest(
1111
) {
1212
return (config: TestConfig) => {
1313
const router = config.nextJsRouter ? `${config.nextJsRouter} router` : null
14-
const describeLabel = [config.hook, label, router, config.description]
14+
const describeLabel = [label, config.hook, router, config.description]
1515
.filter(Boolean)
1616
.join(' - ')
1717
describe(describeLabel, implementation.bind(null, config))

packages/e2e/shared/specs/linking.cy.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const testLinking = createTest('Linking', ({ path }) => {
99
cy.get('#state').should('have.text', 'pass')
1010
})
1111

12-
it('picks up state from Links pointing to the same page', () => {
12+
it('picks up state from Links from another page', () => {
1313
cy.visit(path + '/other')
1414
cy.contains('#hydration-marker', 'hydrated').should('be.hidden')
1515
cy.get('#state').should('be.empty')
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createTest, type TestConfig } from '../create-test'
2+
import { getRoutingUrl } from './routing.defs'
3+
4+
type TestRoutingOptions = TestConfig & {
5+
shallowOptions?: boolean[]
6+
methodOptions?: ('replace' | 'push')[]
7+
}
8+
9+
export function testRouting({
10+
shallowOptions = [true, false],
11+
methodOptions = ['replace', 'push'],
12+
...options
13+
}: TestRoutingOptions) {
14+
const factory = createTest('Routing', ({ path }) => {
15+
for (const shallow of shallowOptions) {
16+
for (const method of methodOptions) {
17+
it(`picks up state from a router call pointing to the same page - router.${method}({ shallow: ${shallow} })`, () => {
18+
cy.visit(getRoutingUrl(path, { shallow, method }))
19+
cy.contains('#hydration-marker', 'hydrated').should('be.hidden')
20+
cy.get('#state').should('be.empty')
21+
cy.get('button').click()
22+
cy.get('#state').should('have.text', 'pass')
23+
if (method === 'push') {
24+
cy.go('back')
25+
cy.get('#state').should('be.empty')
26+
}
27+
})
28+
29+
it('picks up state from a router issued from another page', () => {
30+
cy.visit(getRoutingUrl(path + '/other', { shallow, method }))
31+
cy.contains('#hydration-marker', 'hydrated').should('be.hidden')
32+
cy.get('#state').should('be.empty')
33+
cy.get('button').click()
34+
cy.get('#state').should('have.text', 'pass')
35+
if (method === 'push') {
36+
cy.go('back')
37+
cy.get('#state').should('be.empty')
38+
}
39+
})
40+
}
41+
}
42+
})
43+
return factory(options)
44+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
createSerializer,
3+
parseAsBoolean,
4+
parseAsString,
5+
parseAsStringLiteral
6+
} from 'nuqs'
7+
8+
export const routingSearchParams = {
9+
state: parseAsString,
10+
shallow: parseAsBoolean.withDefault(true),
11+
method: parseAsStringLiteral(['push', 'replace']).withDefault('replace')
12+
}
13+
export const routingUrlKeys = {
14+
state: 'test',
15+
method: 'router'
16+
}
17+
18+
export const getRoutingUrl = createSerializer(routingSearchParams, {
19+
urlKeys: routingUrlKeys
20+
})

0 commit comments

Comments
 (0)