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 support for passing context to React class based components via contextType #2507

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,13 @@ class ReactSixteenAdapter extends EnzymeAdapter {
renderedEl = transformSuspense(renderedEl, renderedEl, { suspenseFallback });
const { type: Component } = renderedEl;

const context = getMaskedContext(Component.contextTypes, unmaskedContext);
let context;
if (Component.contextType) {
const Provider = adapter.getProviderFromConsumer(Component.contextType);
context = providerValues.has(Provider) ? providerValues.get(Provider) : getProviderDefaultValue(Provider);
} else {
context = getMaskedContext(Component.contextTypes, unmaskedContext);
}

if (isMemo(el.type)) {
const { type: InnerComp, compare } = el.type;
Expand Down
80 changes: 78 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ describe('shallow', () => {
let Consumer;

beforeEach(() => {
({ Provider, Consumer } = React.createContext('howdy!'));
({ Provider, Consumer } = createContext('howdy!'));
});

class Consumes extends React.Component {
Expand Down Expand Up @@ -611,7 +611,7 @@ describe('shallow', () => {
let Consumer;

beforeEach(() => {
({ Provider, Consumer } = React.createContext('howdy!'));
({ Provider, Consumer } = createContext('howdy!'));
});

class Consumes extends React.Component {
Expand Down Expand Up @@ -666,6 +666,82 @@ describe('shallow', () => {
expect(consumer.text()).to.equal('foo');
});
});

describeIf(is('>= 16.6'), 'shallow() on Provider and Consumer through .contextType', () => {
let Provider;

beforeEach(() => {
({ Provider } = createContext('howdy!'));
});

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

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

class InnerComponent extends React.Component {
render() {
return this.context;
}
}

InnerComponent.contextType = Provider;

describe('rendering the Provider directly', () => {
it('renders initial context value', () => {
const wrapper = shallow(<OuterComponent value="foo" />);
const provides = wrapper.find(Provider).shallow();
const provider = provides.find(InnerComponent).dive();

expect(provider.text()).to.equal('foo');
});

it('renders updated context value', () => {
const wrapper = shallow(<OuterComponent value="foo" />);
wrapper.setProps({ value: 'bar' });
const provides = wrapper.find(Provider).shallow();
const provider = provides.find(InnerComponent).dive();

expect(provider.text()).to.equal('bar');
});
});

describe('rendering the Provider through wrappingComponent', () => {
it('renders initial context value', () => {
const wrapper = shallow(<InnerComponent />, {
wrappingComponent: WrappingComponent,
wrappingComponentProps: { value: 'foo' },
});

expect(wrapper.text()).to.equal('foo');
});

it('renders updated context value', () => {
const wrapper = shallow(<InnerComponent />, {
wrappingComponent: WrappingComponent,
wrappingComponentProps: { value: 'foo' },
});
const wrappingComponent = wrapper.getWrappingComponent();
wrappingComponent.setProps({ value: 'bar' });
wrappingComponent.rerender();

expect(wrapper.text()).to.equal('bar');
});
});
});
});

describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
Expand Down