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

Convert ReactContextValidator to createRoot #28085

Merged
Merged
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
81 changes: 50 additions & 31 deletions packages/react/src/__tests__/ReactContextValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@

let PropTypes;
let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;
let act;

describe('ReactContextValidator', () => {
beforeEach(() => {
jest.resetModules();

PropTypes = require('prop-types');
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');
act = require('internal-test-utils').act;
});

// TODO: This behavior creates a runtime dependency on propTypes. We should
Expand Down Expand Up @@ -72,7 +74,7 @@ describe('ReactContextValidator', () => {
});

// @gate !disableLegacyContext
it('should pass next context to lifecycles', () => {
it('should pass next context to lifecycles', async () => {
let componentDidMountContext;
let componentDidUpdateContext;
let componentWillReceivePropsContext;
Expand Down Expand Up @@ -135,11 +137,18 @@ describe('ReactContextValidator', () => {
};

const container = document.createElement('div');
ReactDOM.render(<Parent foo="abc" />, container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Parent foo="abc" />);
});

expect(constructorContext).toEqual({foo: 'abc'});
expect(renderContext).toEqual({foo: 'abc'});
expect(componentDidMountContext).toEqual({foo: 'abc'});
ReactDOM.render(<Parent foo="def" />, container);
await act(() => {
root.render(<Parent foo="def" />);
});

expect(componentWillReceivePropsContext).toEqual({foo: 'abc'});
expect(componentWillReceivePropsNextContext).toEqual({foo: 'def'});
expect(shouldComponentUpdateContext).toEqual({foo: 'abc'});
Expand Down Expand Up @@ -369,7 +378,7 @@ describe('ReactContextValidator', () => {
expect(childContext.foo).toBe('FOO');
});

it('should pass next context to lifecycles', () => {
it('should pass next context to lifecycles', async () => {
let componentDidMountContext;
let componentDidUpdateContext;
let componentWillReceivePropsContext;
Expand Down Expand Up @@ -417,21 +426,26 @@ describe('ReactContextValidator', () => {
const secondContext = {bar: 456};

const container = document.createElement('div');
ReactDOM.render(
<Context.Provider value={firstContext}>
<Component />
</Context.Provider>,
container,
);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<Context.Provider value={firstContext}>
<Component />
</Context.Provider>,
);
});

expect(constructorContext).toBe(firstContext);
expect(renderContext).toBe(firstContext);
expect(componentDidMountContext).toBe(firstContext);
ReactDOM.render(
<Context.Provider value={secondContext}>
<Component />
</Context.Provider>,
container,
);
await act(() => {
root.render(
<Context.Provider value={secondContext}>
<Component />
</Context.Provider>,
);
});

expect(componentWillReceivePropsContext).toBe(firstContext);
expect(componentWillReceivePropsNextContext).toBe(secondContext);
expect(componentWillUpdateContext).toBe(firstContext);
Expand All @@ -447,7 +461,7 @@ describe('ReactContextValidator', () => {
}
});

it('should re-render PureComponents when context Provider updates', () => {
it('should re-render PureComponents when context Provider updates', async () => {
let renderedContext;

const Context = React.createContext();
Expand All @@ -464,19 +478,24 @@ describe('ReactContextValidator', () => {
const secondContext = {bar: 456};

const container = document.createElement('div');
ReactDOM.render(
<Context.Provider value={firstContext}>
<Component />
</Context.Provider>,
container,
);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<Context.Provider value={firstContext}>
<Component />
</Context.Provider>,
);
});

expect(renderedContext).toBe(firstContext);
ReactDOM.render(
<Context.Provider value={secondContext}>
<Component />
</Context.Provider>,
container,
);
await act(() => {
root.render(
<Context.Provider value={secondContext}>
<Component />
</Context.Provider>,
);
});

expect(renderedContext).toBe(secondContext);
});

Expand Down