-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(router): reject superseded Solid and Vue render acknowledgements #8065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+309
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
231 changes: 231 additions & 0 deletions
231
packages/vue-router/tests/transitioner-render-ack.test.tsx
This file contains hidden or 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,231 @@ | ||
| import * as Vue from 'vue' | ||
| import { cleanup, render, screen, waitFor } from '@testing-library/vue' | ||
| import { afterEach, expect, test } from 'vitest' | ||
| import { | ||
| Outlet, | ||
| RouterProvider, | ||
| createControlledPromise, | ||
| createMemoryHistory, | ||
| createRootRoute, | ||
| createRoute, | ||
| createRouter, | ||
| useRouterState, | ||
| } from '../src' | ||
| import type { AnyRouter } from '../src' | ||
|
|
||
| afterEach(() => { | ||
| cleanup() | ||
| }) | ||
|
|
||
| test('a generation replaced before the Vue render tick does not emit onRendered', async () => { | ||
| const secondGate = createControlledPromise<void>() | ||
| const lifecycle: Array<string> = [] | ||
| let replacementEnabled = false | ||
| let secondNavigation: Promise<void> | undefined | ||
|
|
||
| const First = Vue.defineComponent({ | ||
| setup() { | ||
| Vue.onMounted(() => lifecycle.push('mounted:/first')) | ||
| return () => <div>First</div> | ||
| }, | ||
| }) | ||
| const SecondPending = Vue.defineComponent({ | ||
| setup() { | ||
| Vue.onMounted(() => lifecycle.push('mounted:pending:/second')) | ||
| return () => <div>Second pending</div> | ||
| }, | ||
| }) | ||
| const Second = Vue.defineComponent({ | ||
| setup() { | ||
| Vue.onMounted(() => lifecycle.push('mounted:/second')) | ||
| return () => <div>Second</div> | ||
| }, | ||
| }) | ||
| const rootRoute = createRootRoute({ component: () => <Outlet /> }) | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| component: () => <div>Home</div>, | ||
| }) | ||
| const firstRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/first', | ||
| component: First, | ||
| }) | ||
| const secondRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/second', | ||
| pendingMs: 0, | ||
| pendingMinMs: 0, | ||
| pendingComponent: SecondPending, | ||
| beforeLoad: () => secondGate, | ||
| component: Second, | ||
| }) | ||
| const Wrap = Vue.defineComponent({ | ||
| setup(_, { slots }) { | ||
| const leafRouteId = useRouterState<AnyRouter, string | undefined>({ | ||
| select: (state) => state.matches.at(-1)?.routeId, | ||
| }) | ||
| Vue.watch( | ||
| leafRouteId, | ||
| (routeId) => { | ||
| if ( | ||
| replacementEnabled && | ||
| routeId === firstRoute.id && | ||
| !secondNavigation | ||
| ) { | ||
| lifecycle.push('offered:/first') | ||
| secondNavigation = router.navigate({ to: '/second' }) | ||
| lifecycle.push('navigate:/second') | ||
| } | ||
| }, | ||
| { flush: 'sync' }, | ||
| ) | ||
| return () => slots.default?.() | ||
| }, | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute, firstRoute, secondRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/'] }), | ||
| Wrap: Wrap as any, | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
| expect(await screen.findByText('Home')).toBeInTheDocument() | ||
| await waitFor(() => expect(router.state.status).toBe('idle')) | ||
|
|
||
| const unsubscribers = [ | ||
| router.subscribe('onResolved', (event) => { | ||
| lifecycle.push(`onResolved:${event.toLocation.pathname}`) | ||
| }), | ||
| router.subscribe('onRendered', (event) => { | ||
| lifecycle.push(`onRendered:${event.toLocation.pathname}`) | ||
| }), | ||
| ] | ||
| replacementEnabled = true | ||
| let firstNavigation: Promise<void> | undefined | ||
| try { | ||
| firstNavigation = router.navigate({ to: '/first' }) | ||
|
|
||
| expect(await screen.findByText('Second pending')).toBeInTheDocument() | ||
| expect(secondNavigation).toBeDefined() | ||
| expect(screen.queryByText('First')).not.toBeInTheDocument() | ||
| expect(lifecycle).not.toContain('mounted:/first') | ||
| expect(lifecycle).not.toContain('onResolved:/first') | ||
| expect(lifecycle).not.toContain('onRendered:/first') | ||
|
|
||
| secondGate.resolve() | ||
| await Promise.all([firstNavigation, secondNavigation!]) | ||
| expect(await screen.findByText('Second')).toBeInTheDocument() | ||
| await waitFor(() => expect(lifecycle).toContain('onRendered:/second')) | ||
|
|
||
| expect(lifecycle).toContain('mounted:pending:/second') | ||
| expect(lifecycle).toContain('mounted:/second') | ||
| expect(lifecycle).toContain('onResolved:/second') | ||
| expect(lifecycle).not.toContain('mounted:/first') | ||
| expect(lifecycle).not.toContain('onResolved:/first') | ||
| expect(lifecycle).not.toContain('onRendered:/first') | ||
| } finally { | ||
| replacementEnabled = false | ||
| secondGate.resolve() | ||
| for (const unsubscribe of unsubscribers) { | ||
| unsubscribe() | ||
| } | ||
| await Promise.allSettled( | ||
| [firstNavigation, secondNavigation].filter( | ||
| (navigation): navigation is Promise<void> => !!navigation, | ||
| ), | ||
| ) | ||
| } | ||
| }) | ||
|
|
||
| test('a rendered generation superseded before core continuation does not emit onRendered', async () => { | ||
| const secondGate = createControlledPromise<void>() | ||
| const lifecycle: Array<string> = [] | ||
| let secondNavigation: Promise<void> | undefined | ||
|
|
||
| const First = Vue.defineComponent({ | ||
| setup() { | ||
| Vue.onMounted(() => { | ||
| lifecycle.push('mounted:/first') | ||
| secondNavigation = router.navigate({ to: '/second' }) | ||
| lifecycle.push('navigate:/second') | ||
| }) | ||
| return () => <div>First</div> | ||
| }, | ||
| }) | ||
| const Second = Vue.defineComponent({ | ||
| setup() { | ||
| Vue.onMounted(() => lifecycle.push('mounted:/second')) | ||
| return () => <div>Second</div> | ||
| }, | ||
| }) | ||
| const rootRoute = createRootRoute({ component: () => <Outlet /> }) | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| component: () => <div>Home</div>, | ||
| }) | ||
| const firstRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/first', | ||
| component: First, | ||
| }) | ||
| const secondRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/second', | ||
| loader: () => secondGate, | ||
| component: Second, | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute, firstRoute, secondRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/'] }), | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
| expect(await screen.findByText('Home')).toBeInTheDocument() | ||
| await waitFor(() => expect(router.state.status).toBe('idle')) | ||
|
|
||
| const unsubscribers = [ | ||
| router.subscribe('onResolved', (event) => { | ||
| lifecycle.push(`onResolved:${event.toLocation.pathname}`) | ||
| }), | ||
| router.subscribe('onRendered', (event) => { | ||
| lifecycle.push(`onRendered:${event.toLocation.pathname}`) | ||
| }), | ||
| ] | ||
| let firstNavigation: Promise<void> | undefined | ||
| try { | ||
| firstNavigation = router.navigate({ to: '/first' }) | ||
|
|
||
| expect(await screen.findByText('First')).toBeInTheDocument() | ||
| await waitFor(() => | ||
| expect(lifecycle).toEqual(['mounted:/first', 'navigate:/second']), | ||
| ) | ||
| expect(secondNavigation).toBeDefined() | ||
| expect(lifecycle).not.toContain('onResolved:/first') | ||
|
|
||
| secondGate.resolve() | ||
| await Promise.all([firstNavigation, secondNavigation!]) | ||
| expect(await screen.findByText('Second')).toBeInTheDocument() | ||
| await waitFor(() => | ||
| expect(lifecycle).toEqual([ | ||
| 'mounted:/first', | ||
| 'navigate:/second', | ||
| 'mounted:/second', | ||
| 'onResolved:/second', | ||
| 'onRendered:/second', | ||
| ]), | ||
| ) | ||
| } finally { | ||
| secondGate.resolve() | ||
| for (const unsubscribe of unsubscribers) { | ||
| unsubscribe() | ||
| } | ||
| await Promise.allSettled( | ||
| [firstNavigation, secondNavigation].filter( | ||
| (navigation): navigation is Promise<void> => !!navigation, | ||
| ), | ||
| ) | ||
| } | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Replace the
as anycast on theWrapoption.The repository guidelines require extensive type safety.
Wrap: Wrap as anyremoves all checking on this option. Type the component so it satisfies theWrapoption type, or narrow the cast to the declared option type.If the
Wrapoption type genuinely cannot accept a Vue component, that is a typing gap in the router options and deserves a separate fix.As per coding guidelines: "Use TypeScript strict mode with extensive type safety".
🤖 Prompt for AI Agents
Source: Coding guidelines