Skip to content

Commit

Permalink
Convert ReactLegacyContextDisabled-test.internal to createRoot (#28093)
Browse files Browse the repository at this point in the history
Convert ReactLegacyContextDisabled-test.internal to createRoot
  • Loading branch information
kassens authored Jan 25, 2024
1 parent 991b72a commit c7e735f
Showing 1 changed file with 66 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
'use strict';

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactFeatureFlags;
let act;

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

React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.disableLegacyContext = true;
act = require('internal-test-utils').act;
});

function formatValue(val) {
Expand All @@ -38,7 +40,7 @@ describe('ReactLegacyContextDisabled', () => {
return JSON.stringify(val);
}

it('warns for legacy context', () => {
it('warns for legacy context', async () => {
class LegacyProvider extends React.Component {
static childContextTypes = {
foo() {},
Expand Down Expand Up @@ -81,17 +83,19 @@ describe('ReactLegacyContextDisabled', () => {
}

const container = document.createElement('div');
expect(() => {
ReactDOM.render(
<LegacyProvider>
<span>
<LegacyClsConsumer />
<LegacyFnConsumer />
<RegularFn />
</span>
</LegacyProvider>,
container,
);
const root = ReactDOMClient.createRoot(container);
await expect(async () => {
await act(() => {
root.render(
<LegacyProvider>
<span>
<LegacyClsConsumer />
<LegacyFnConsumer />
<RegularFn />
</span>
</LegacyProvider>,
);
});
}).toErrorDev([
'LegacyProvider uses the legacy childContextTypes API which is no longer supported. ' +
'Use React.createContext() instead.',
Expand All @@ -104,19 +108,20 @@ describe('ReactLegacyContextDisabled', () => {
expect(lifecycleContextLog).toEqual([]);

// Test update path.
ReactDOM.render(
<LegacyProvider>
<span>
<LegacyClsConsumer />
<LegacyFnConsumer />
<RegularFn />
</span>
</LegacyProvider>,
container,
);
await act(() => {
root.render(
<LegacyProvider>
<span>
<LegacyClsConsumer />
<LegacyFnConsumer />
<RegularFn />
</span>
</LegacyProvider>,
);
});
expect(container.textContent).toBe('{}undefinedundefined');
expect(lifecycleContextLog).toEqual([{}, {}, {}]);
ReactDOM.unmountComponentAtNode(container);
root.unmount();

// test server path.
let text;
Expand All @@ -143,7 +148,7 @@ describe('ReactLegacyContextDisabled', () => {
expect(lifecycleContextLog).toEqual([{}, {}, {}]);
});

it('renders a tree with modern context', () => {
it('renders a tree with modern context', async () => {
const Ctx = React.createContext();

class Provider extends React.Component {
Expand Down Expand Up @@ -185,44 +190,48 @@ describe('ReactLegacyContextDisabled', () => {
}

const container = document.createElement('div');
ReactDOM.render(
<Provider value="a">
<span>
<RenderPropConsumer />
<ContextTypeConsumer />
<FnConsumer />
</span>
</Provider>,
container,
);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<Provider value="a">
<span>
<RenderPropConsumer />
<ContextTypeConsumer />
<FnConsumer />
</span>
</Provider>,
);
});
expect(container.textContent).toBe('aaa');
expect(lifecycleContextLog).toEqual([]);

// Test update path
ReactDOM.render(
<Provider value="a">
<span>
<RenderPropConsumer />
<ContextTypeConsumer />
<FnConsumer />
</span>
</Provider>,
container,
);
await act(() => {
root.render(
<Provider value="a">
<span>
<RenderPropConsumer />
<ContextTypeConsumer />
<FnConsumer />
</span>
</Provider>,
);
});
expect(container.textContent).toBe('aaa');
expect(lifecycleContextLog).toEqual(['a', 'a', 'a']);
lifecycleContextLog.length = 0;

ReactDOM.render(
<Provider value="b">
<span>
<RenderPropConsumer />
<ContextTypeConsumer />
<FnConsumer />
</span>
</Provider>,
container,
);
await act(() => {
root.render(
<Provider value="b">
<span>
<RenderPropConsumer />
<ContextTypeConsumer />
<FnConsumer />
</span>
</Provider>,
);
});
expect(container.textContent).toBe('bbb');
if (gate(flags => flags.enableLazyContextPropagation)) {
// In the lazy propagation implementation, we don't check if context
Expand All @@ -233,6 +242,6 @@ describe('ReactLegacyContextDisabled', () => {
// changed, so we skipped sCU.
expect(lifecycleContextLog).toEqual(['b', 'b']);
}
ReactDOM.unmountComponentAtNode(container);
root.unmount();
});
});

0 comments on commit c7e735f

Please sign in to comment.