From d0368471dc9398479d07c1412d5a40c136f29fa3 Mon Sep 17 00:00:00 2001 From: Alberto Gasparin Date: Mon, 20 Jan 2020 12:30:44 +1100 Subject: [PATCH] Fix globalRegistry being undefined with enzyme shallow --- src/components/__tests__/container.test.js | 2 +- src/components/container.js | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/components/__tests__/container.test.js b/src/components/__tests__/container.test.js index 30e4ad2..35a5cb6 100644 --- a/src/components/__tests__/container.test.js +++ b/src/components/__tests__/container.test.js @@ -82,7 +82,7 @@ describe('Container', () => { children, value: { getStore: expect.any(Function), - globalRegistry: undefined, // shallow() context support is buggy + globalRegistry: defaultRegistry, }, }); }); diff --git a/src/components/container.js b/src/components/container.js index 5f55202..0842048 100644 --- a/src/components/container.js +++ b/src/components/container.js @@ -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 = () => () => {}; @@ -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