-
-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: persisting the 'disabled' prop for custom link elements using `c…
…reateLink` (#1544) * fix(react-router): do not remove the disabled prop for `_asChild` links * refactor(react-router): useLinkProps always returns a `disabled` boolean * test(react-router): make sure this works as exptected * test(react-router): tests to include other props * test: use queryByText
- Loading branch information
1 parent
7507443
commit fbf1d02
Showing
2 changed files
with
110 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
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,103 @@ | ||
import React from 'react' | ||
import { describe, it, expect, afterEach } from 'vitest' | ||
import { render, cleanup } from '@testing-library/react' | ||
|
||
import { | ||
createRouter, | ||
createRootRoute, | ||
createRoute, | ||
createMemoryHistory, | ||
RouterProvider, | ||
createLink, | ||
Link, | ||
} from '../src' | ||
|
||
afterEach(() => { | ||
cleanup() | ||
}) | ||
|
||
describe('Link', () => { | ||
it('should NOT pass the "disabled" prop to the rendered Link component', async () => { | ||
const rootRoute = createRootRoute() | ||
const indexRoute = createRoute({ | ||
getParentRoute: () => rootRoute, | ||
path: '/', | ||
component: () => ( | ||
<Link to="/" disabled> | ||
Index | ||
</Link> | ||
), | ||
}) | ||
const router = createRouter({ | ||
routeTree: rootRoute.addChildren([indexRoute]), | ||
history: createMemoryHistory({ initialEntries: ['/'] }), | ||
}) | ||
|
||
await router.load() | ||
|
||
const rendered = render(<RouterProvider router={router} />) | ||
const customElement = rendered.queryByText('Index') | ||
|
||
expect(customElement!.hasAttribute('disabled')).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('createLink', () => { | ||
it('should pass the "disabled" prop to the rendered target element', async () => { | ||
const CustomLink = createLink('button') | ||
|
||
const rootRoute = createRootRoute() | ||
const indexRoute = createRoute({ | ||
getParentRoute: () => rootRoute, | ||
path: '/', | ||
component: () => ( | ||
<CustomLink to="/" disabled> | ||
Index | ||
</CustomLink> | ||
), | ||
}) | ||
const router = createRouter({ | ||
routeTree: rootRoute.addChildren([indexRoute]), | ||
history: createMemoryHistory({ initialEntries: ['/'] }), | ||
}) | ||
|
||
await router.load() | ||
|
||
const rendered = render(<RouterProvider router={router} />) | ||
const customElement = rendered.queryByText('Index') | ||
|
||
expect(customElement!.hasAttribute('disabled')).toBe(true) | ||
expect(customElement!.getAttribute('disabled')).toBe('') | ||
}) | ||
|
||
it('should pass the "foo" prop to the rendered target element', async () => { | ||
const CustomLink = createLink('button') | ||
|
||
const rootRoute = createRootRoute() | ||
const indexRoute = createRoute({ | ||
getParentRoute: () => rootRoute, | ||
path: '/', | ||
component: () => ( | ||
<CustomLink | ||
to="/" | ||
// @ts-expect-error | ||
foo="bar" | ||
> | ||
Index | ||
</CustomLink> | ||
), | ||
}) | ||
const router = createRouter({ | ||
routeTree: rootRoute.addChildren([indexRoute]), | ||
history: createMemoryHistory({ initialEntries: ['/'] }), | ||
}) | ||
|
||
await router.load() | ||
|
||
const rendered = render(<RouterProvider router={router} />) | ||
const customElement = rendered.queryByText('Index') | ||
|
||
expect(customElement!.hasAttribute('foo')).toBe(true) | ||
expect(customElement!.getAttribute('foo')).toBe('bar') | ||
}) | ||
}) |