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

Fix globalRegistry being undefined with enzyme shallow #57

Merged
merged 1 commit into from
Jan 20, 2020
Merged
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
2 changes: 1 addition & 1 deletion src/components/__tests__/container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('Container', () => {
children,
value: {
getStore: expect.any(Function),
globalRegistry: undefined, // shallow() context support is buggy
globalRegistry: defaultRegistry,
},
});
});
Expand Down
18 changes: 15 additions & 3 deletions src/components/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';

import { Context } from '../context';
import { StoreRegistry, bindAction, bindActions } from '../store';
import {
StoreRegistry,
bindAction,
bindActions,
defaultRegistry,
} from '../store';
import shallowEqual from '../utils/shallow-equal';

const noop = () => () => {};
Expand Down Expand Up @@ -39,11 +44,18 @@ export default class Container extends Component {
super(props, context);
this.registry = new StoreRegistry('__local__');

const {
// These fallbacks are needed only to make enzyme shallow work
// as it does not fully support provider-less Context enzyme#1553
globalRegistry = defaultRegistry,
getStore = defaultRegistry.getStore,
} = this.context;

this.state = {
api: {
globalRegistry: this.context.globalRegistry,
globalRegistry,
getStore: (Store, scope) =>
this.getScopedStore(Store, scope) || this.context.getStore(Store),
this.getScopedStore(Store, scope) || getStore(Store),
},
// stored to make them available in getDerivedStateFromProps
// as js context there is null https://github.com/facebook/react/issues/12612
Expand Down