Skip to content

Commit

Permalink
Convert ReactContextValidator to createRoot (#28085)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Jan 25, 2024
1 parent a41ebfc commit 43a3653
Showing 1 changed file with 50 additions and 31 deletions.
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

0 comments on commit 43a3653

Please sign in to comment.