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

Add a feature flag to disable legacy context #16269

Merged
merged 7 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

let React;
let ReactDOM;
let ReactFeatureFlags;

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

React = require('react');
ReactDOM = require('react-dom');
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.disableLegacyContext = true;
});

it('throws for a legacy context provider', () => {
class Provider extends React.Component {
static childContextTypes = {
foo() {},
};
getChildContext() {
return {foo: 10};
}
render() {
return null;
}
}

const container = document.createElement('div');
expect(() => {
ReactDOM.render(<Provider />, container);
}).toThrow(
'The legacy childContextTypes API is no longer supported. ' +
'Use React.createContext() instead.',
);
});

it('throws for a legacy context consumer', () => {
class Consumer extends React.Component {
static contextTypes = {
foo() {},
};
render() {
return null;
}
}

const container = document.createElement('div');
expect(() => {
ReactDOM.render(<Consumer />, container);
}).toThrow(
'The legacy contextTypes API is no longer supported. ' +
'Use React.createContext() with contextType instead.',
);
});

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

class Provider extends React.Component {
render() {
return (
<Ctx.Provider value={this.props.value}>
{this.props.children}
</Ctx.Provider>
);
}
}

class RenderPropConsumer extends React.Component {
render() {
return <Ctx.Consumer>{value => value}</Ctx.Consumer>;
}
}

class ContextTypeConsumer extends React.Component {
static contextType = Ctx;
render() {
return this.context;
}
}

function FnConsumer() {
return React.useContext(Ctx);
}

const container = document.createElement('div');
ReactDOM.render(
<Provider value="a">
<span>
<RenderPropConsumer />
<ContextTypeConsumer />
<FnConsumer />
</span>
</Provider>,
container,
);
expect(container.textContent).toBe('aaa');
});
});
Loading