Skip to content

Commit

Permalink
fix: cypress/react18 rerender (#23360)
Browse files Browse the repository at this point in the history
Co-authored-by: astone123 <adams@cypress.io>
  • Loading branch information
hiebj and astone123 committed Sep 2, 2022
1 parent 8e4ebef commit 8b8f20e
Show file tree
Hide file tree
Showing 10 changed files with 523 additions and 94 deletions.
8 changes: 6 additions & 2 deletions npm/react18/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import type {
UnmountArgs,
} from '@cypress/react'

let root: any
let root: ReactDOM.Root | null

const cleanup = () => {
if (root) {
root.unmount()

root = null

return true
}

Expand All @@ -27,7 +29,9 @@ export function mount (jsx: React.ReactNode, options: MountOptions = {}, rerende
const internalOptions: InternalMountOptions = {
reactDom: ReactDOM,
render: (reactComponent: ReturnType<typeof React.createElement>, el: HTMLElement) => {
root = ReactDOM.createRoot(el)
if (!root) {
root = ReactDOM.createRoot(el)
}

return root.render(reactComponent)
},
Expand Down
196 changes: 170 additions & 26 deletions system-tests/__snapshots__/component_testing_spec.ts.js

Large diffs are not rendered by default.

159 changes: 132 additions & 27 deletions system-tests/__snapshots__/vite_dev_server_fresh_spec.ts.js

Large diffs are not rendered by default.

210 changes: 175 additions & 35 deletions system-tests/__snapshots__/webpack_dev_server_fresh_spec.ts.js

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions system-tests/project-fixtures/react/src/Rerendering.cy.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';

function StatefulComponent ({ foo }) {
const [bar, setBar] = React.useState(0);
return (
<button
onClick={() => {
setBar(bar + 1);
}}
>
{foo} {bar}
</button>
);
};

describe('re-render', () => {
it('maintains component state across re-renders', () => {
cy.mount(<StatefulComponent foo="baz" />).then(({ rerender }) => {
cy.get('button').should('have.text', 'baz 0');
cy.get('button').click().should('have.text', 'baz 1');
rerender(<StatefulComponent foo="baz" />)

// The button should still show 1 after re-render
cy.get('button').should('have.text', 'baz 1');
});
});
});
6 changes: 6 additions & 0 deletions system-tests/project-fixtures/react/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const {defineConfig} = require('vite')

module.exports = defineConfig({
resolve: {
alias: {
'react': require.resolve('react'),
'react-dom': require.resolve('react-dom'),
},
},
logLevel: 'silent'
})
2 changes: 1 addition & 1 deletion system-tests/projects/react17/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"react": "17.0.2",
"react-dom": " 17.0.2"
"react-dom": "17.0.2"
},
"devDependencies": {
"@vitejs/plugin-react": "^1.3.2",
Expand Down
2 changes: 1 addition & 1 deletion system-tests/projects/react17/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ randomfill@^1.0.3:
randombytes "^2.0.5"
safe-buffer "^5.1.0"

"react-dom@ 17.0.2":
react-dom@17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-17.0.2.tgz#ecffb6845e3ad8dbfcdc498f0d0a939736502c23"
integrity sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA==
Expand Down
4 changes: 2 additions & 2 deletions system-tests/test/component_testing_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe(`React major versions with Vite`, function () {
return systemTests.exec(this, {
project: `react${majorVersion}`,
configFile: 'cypress-vite.config.ts',
spec: 'src/App.cy.jsx,src/Unmount.cy.jsx,src/UsingLegacyMount.cy.jsx',
spec: 'src/App.cy.jsx,src/Unmount.cy.jsx,src/UsingLegacyMount.cy.jsx,src/Rerendering.cy.jsx',
testingType: 'component',
browser: 'chrome',
snapshot: true,
Expand All @@ -97,7 +97,7 @@ describe(`React major versions with Webpack`, function () {
return systemTests.exec(this, {
project: `react${majorVersion}`,
configFile: 'cypress-webpack.config.ts',
spec: 'src/App.cy.jsx,src/Unmount.cy.jsx,src/UsingLegacyMount.cy.jsx',
spec: 'src/App.cy.jsx,src/Unmount.cy.jsx,src/UsingLegacyMount.cy.jsx,src/Rerendering.cy.jsx',
testingType: 'component',
browser: 'chrome',
snapshot: true,
Expand Down
3 changes: 3 additions & 0 deletions system-tests/test/vite_dev_server_fresh_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('@cypress/vite-dev-server', function () {
browser: 'chrome',
snapshot: true,
expectedExitCode: 3,
onStdout: (stdout) => {
return stdout.replace(/http:\/\/localhost:\d+/g, 'http://localhost:xxxx')
},
})
})
}
Expand Down

5 comments on commit 8b8f20e

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8b8f20e Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.7.1/linux-x64/develop-8b8f20eec77d4c0a704aee7f7077dc92dbafb93f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8b8f20e Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.7.1/linux-arm64/develop-8b8f20eec77d4c0a704aee7f7077dc92dbafb93f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8b8f20e Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.7.1/darwin-arm64/develop-8b8f20eec77d4c0a704aee7f7077dc92dbafb93f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8b8f20e Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.7.1/darwin-x64/develop-8b8f20eec77d4c0a704aee7f7077dc92dbafb93f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8b8f20e Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.7.1/win32-x64/develop-8b8f20eec77d4c0a704aee7f7077dc92dbafb93f/cypress.tgz

Please sign in to comment.