diff --git a/src/DirectionProvider.jsx b/src/DirectionProvider.jsx index fa53036..e9dbb6f 100644 --- a/src/DirectionProvider.jsx +++ b/src/DirectionProvider.jsx @@ -38,9 +38,9 @@ export default class DirectionProvider extends React.Component { }; } - componentWillReceiveProps(nextProps) { - if (this.props.direction !== nextProps.direction) { - this.broadcast.setState(nextProps.direction); + componentDidUpdate(prevProps) { + if (prevProps.direction !== this.props.direction) { + this.broadcast.setState(this.props.direction); } } diff --git a/tests/DirectionProvider_test.jsx b/tests/DirectionProvider_test.jsx index ac9099b..633e4f2 100644 --- a/tests/DirectionProvider_test.jsx +++ b/tests/DirectionProvider_test.jsx @@ -1,6 +1,15 @@ +/** + * @jest-environment jsdom + */ + + // We must set the env to jsdom for this file to accommodate mounted components. + // The components must be mounted because there is currently no way to test + // code within componentDidUpdate lifecycle in shallow mounting. + // https://github.com/airbnb/enzyme/issues/1452 + import React from 'react'; import { expect } from 'chai'; -import { shallow } from 'enzyme'; +import { shallow, mount } from 'enzyme'; import sinon from 'sinon-sandbox'; import DirectionProvider from '../src/DirectionProvider'; @@ -40,7 +49,7 @@ describe('', () => { it('broadcasts the direction when the direction prop changes', () => { const direction = DIRECTIONS.LTR; const nextDirection = DIRECTIONS.RTL; - const wrapper = shallow( + const wrapper = mount( {children}, ); const broadcast = wrapper.instance().broadcast; @@ -52,7 +61,7 @@ describe('', () => { it('does not broadcast the direction when the direction prop stays the same', () => { const direction = DIRECTIONS.LTR; const nextDirection = DIRECTIONS.LTR; - const wrapper = shallow( + const wrapper = mount( {children}, ); const broadcast = wrapper.instance().broadcast; diff --git a/tests/_helpers.jsx b/tests/_helpers.jsx index cbd5517..0f72af2 100644 --- a/tests/_helpers.jsx +++ b/tests/_helpers.jsx @@ -5,6 +5,7 @@ import sinon from 'sinon-sandbox'; import chaiEnzyme from 'chai-enzyme'; import configure from 'enzyme-adapter-react-helper'; +import './shim'; configure({ disableLifecycleMethods: true }); diff --git a/tests/shim.js b/tests/shim.js new file mode 100644 index 0000000..b9399fa --- /dev/null +++ b/tests/shim.js @@ -0,0 +1,6 @@ +// This file is required to alleviate an RAF error that appears: +// https://github.com/facebook/jest/issues/4545 + +global.requestAnimationFrame = (callback) => { + setTimeout(callback, 0); +};