Skip to content
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

Regression test for props object equality in React 19 #28980

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
112 changes: 112 additions & 0 deletions packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ let act;
let React;
let ReactDOM;
let ReactDOMClient;
let Scheduler;

let assertLog;

const clone = function (o) {
return JSON.parse(JSON.stringify(o));
Expand Down Expand Up @@ -95,6 +98,9 @@ describe('ReactComponentLifeCycle', () => {
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');

({assertLog} = require('internal-test-utils'));
});

it('should not reuse an instance when it has been unmounted', async () => {
Expand Down Expand Up @@ -1387,6 +1393,112 @@ describe('ReactComponentLifeCycle', () => {
expect(divRef.current.textContent).toBe('value: updated');
});

it('should pass previous props to shouldComponentUpdate', async () => {
const root = ReactDOMClient.createRoot(document.createElement('div'));
let childInstance;

class App extends React.Component {
render() {
return <Child />;
}
}

class Child extends React.Component {
render() {
childInstance = this;
return null;
}
shouldComponentUpdate(nextProps: Object): boolean {
Scheduler.log(this.props === nextProps);
return this.props === nextProps;
}
}

await act(async () => {
root.render(<App />);
});

await act(async () => {
childInstance.setState({updated: true});
});

assertLog([true]);
});

it('should pass previous props to shouldComponentUpdate with defaultProps', async () => {
const root = ReactDOMClient.createRoot(document.createElement('div'));
let childInstance;

class App extends React.Component {
render() {
return <Child />;
}
}

class Child extends React.Component {
static defaultProps = {name: 'Alice'};
render() {
childInstance = this;
return null;
}
shouldComponentUpdate(nextProps: Object): boolean {
Scheduler.log(this.props === nextProps);
return this.props === nextProps;
}
}

await act(async () => {
root.render(<App />);
});

await act(async () => {
childInstance.setState({updated: true});
});

assertLog([
// Regression with enableRefAsProp
gate(flags => !flags.enableRefAsProp),
]);
});

// @gate !disableStringRefs
it('should pass previous props to shouldComponentUpdate with string refs', async () => {
const root = ReactDOMClient.createRoot(document.createElement('div'));
let childInstance;

class App extends React.Component {
render() {
return <Child ref="someRef" />;
}
}

class Child extends React.Component {
render() {
childInstance = this;
return 'Child';
}
shouldComponentUpdate(nextProps: Object): boolean {
Scheduler.log(this.props === nextProps);
return this.props === nextProps;
}
}

await expect(async () => {
await act(async () => {
root.render(<App />);
});
}).toErrorDev('Component "App" contains the string ref "someRef".');

await act(async () => {
childInstance.setState({updated: true});
});

assertLog([
// Regression with enableRefAsProp
gate(flags => !flags.enableRefAsProp),
]);
});

it('should call getSnapshotBeforeUpdate before mutations are committed', async () => {
const log = [];

Expand Down