From 30c96271335c313c8d0a8f75537d2188bdaba63e Mon Sep 17 00:00:00 2001 From: Valentin Hervieu Date: Thu, 1 Dec 2022 05:01:29 +0100 Subject: [PATCH] Update next/link default legacyBehavior (#42623) ## Bug Fixes #42621 - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` Co-authored-by: JJ Kasper --- packages/next/client/link.tsx | 3 +- .../test/index.test.js | 30 +++++++++++++++++++ test/unit/link-rendering.test.ts | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/integration/link-with-multiple-child/test/index.test.js diff --git a/packages/next/client/link.tsx b/packages/next/client/link.tsx index 6da55598e4640..80d34bc9b0dda 100644 --- a/packages/next/client/link.tsx +++ b/packages/next/client/link.tsx @@ -375,7 +375,8 @@ const Link = React.forwardRef( onClick, onMouseEnter: onMouseEnterProp, onTouchStart: onTouchStartProp, - legacyBehavior = Boolean(process.env.__NEXT_NEW_LINK_BEHAVIOR) !== true, + // @ts-expect-error this is inlined as a literal boolean not a string + legacyBehavior = process.env.__NEXT_NEW_LINK_BEHAVIOR === false, ...restProps } = props diff --git a/test/integration/link-with-multiple-child/test/index.test.js b/test/integration/link-with-multiple-child/test/index.test.js new file mode 100644 index 0000000000000..a1434ee113af6 --- /dev/null +++ b/test/integration/link-with-multiple-child/test/index.test.js @@ -0,0 +1,30 @@ +/** + * @jest-environment jsdom + */ +import { render, screen } from '@testing-library/react' +import Link from 'next/link' + +test('single child', () => { + render(Hello world) + expect(screen.getByRole('link')).not.toBeNull() +}) + +test('multiple child with default legacyBehavior', () => { + render( + + Hello world + ! + + ) + expect(screen.getByRole('link')).not.toBeNull() +}) + +test('multiple child with forced legacyBehavior=false', () => { + render( + + Hello world + ! + + ) + expect(screen.getByRole('link')).not.toBeNull() +}) diff --git a/test/unit/link-rendering.test.ts b/test/unit/link-rendering.test.ts index 7b79b5ebc90e3..0af74027480ac 100644 --- a/test/unit/link-rendering.test.ts +++ b/test/unit/link-rendering.test.ts @@ -10,7 +10,7 @@ describe('Link rendering', () => { { href: '/my-path', }, - React.createElement('a', {}, 'to another page') + 'to another page' ) const html = ReactDOM.renderToString(element) expect(html).toMatchInlineSnapshot(