UNPKG

3.97 kBJSXView Raw
1import PropTypes from 'prop-types';
2import React from 'react';
3import { expect } from 'chai';
4import { shallow, render } from 'enzyme';
5import sinon from 'sinon-sandbox';
6
7import DirectionProvider from '../src/DirectionProvider';
8import withDirection, { withDirectionPropTypes } from '../src/withDirection';
9import { DIRECTIONS, CHANNEL } from '../src/constants';
10import mockBrcast from './mocks/brcast_mock';
11
12function getWrappedComponent(expectedDirection) {
13 function MyComponent({ animal, direction }) {
14 expect(direction).to.equal(expectedDirection);
15 return (
16 <div>{`My direction is ${direction} and my animal is ${animal}.`}</div>
17 );
18 }
19 MyComponent.propTypes = {
20 ...withDirectionPropTypes,
21 animal: PropTypes.string,
22 };
23 MyComponent.defaultProps = {
24 animal: 'dog',
25 };
26
27 return withDirection(MyComponent);
28}
29
30describe('withDirection()', () => {
31 it('has a wrapped displayName', () => {
32 const Wrapped = getWrappedComponent(DIRECTIONS.LTR);
33 expect(Wrapped.displayName).to.equal('withDirection(MyComponent)');
34 });
35
36 it('defaults direction to LTR', () => {
37 const Wrapped = getWrappedComponent(DIRECTIONS.LTR);
38 render(<Wrapped />);
39 });
40
41 it('passes direction from context to the wrapped component', () => {
42 const Wrapped = getWrappedComponent(DIRECTIONS.RTL);
43 render(
44 <DirectionProvider direction={DIRECTIONS.RTL}>
45 <div>
46 <Wrapped />
47 </div>
48 </DirectionProvider>,
49 );
50 });
51
52 describe('lifecycle methods', () => {
53 let brcast;
54 beforeEach(() => {
55 const unsubscribe = sinon.stub();
56 brcast = mockBrcast({
57 data: DIRECTIONS.LTR,
58 subscribe: sinon.stub().yields(DIRECTIONS.RTL).returns(unsubscribe),
59 unsubscribe,
60 });
61 });
62
63 describe('with a brcast context', () => {
64 let context;
65 beforeEach(() => {
66 context = {
67 [CHANNEL]: brcast,
68 };
69 });
70
71 it('sets state with a new direction when the context changes', () => {
72 const Wrapped = getWrappedComponent(DIRECTIONS.LTR);
73 const wrapper = shallow(<Wrapped />, { context });
74 expect(wrapper).to.have.prop('direction', DIRECTIONS.LTR);
75
76 wrapper.instance().componentDidMount();
77 expect(wrapper).to.have.prop('direction', DIRECTIONS.RTL);
78 });
79
80 it('calls brcast subscribe when the component mounts', () => {
81 const Wrapped = getWrappedComponent(DIRECTIONS.LTR);
82 const wrapper = shallow(<Wrapped />, { context });
83
84 expect(brcast.subscribe).to.have.callCount(0);
85 wrapper.instance().componentDidMount();
86 expect(brcast.subscribe).to.have.callCount(1);
87 });
88
89 it('calls brcast unsubscribe when the component unmounts', () => {
90 const Wrapped = getWrappedComponent(DIRECTIONS.LTR);
91 const wrapper = shallow(<Wrapped />, { context });
92 wrapper.instance().componentDidMount();
93
94 expect(brcast.unsubscribe).to.have.callCount(0);
95 wrapper.instance().componentWillUnmount();
96 expect(brcast.unsubscribe).to.have.callCount(1);
97 });
98 });
99
100 describe('without a brcast context', () => {
101 let context;
102 beforeEach(() => {
103 context = {
104 [CHANNEL]: null,
105 };
106 });
107
108 it('does not call brcast subscribe when the component mounts', () => {
109 const Wrapped = getWrappedComponent(DIRECTIONS.LTR);
110 const wrapper = shallow(<Wrapped />, { context });
111
112 wrapper.instance().componentDidMount();
113 expect(brcast.subscribe).to.have.callCount(0);
114 });
115
116 it('does not call brcast unsubscribe when the component unmounts', () => {
117 const Wrapped = getWrappedComponent(DIRECTIONS.LTR);
118 const wrapper = shallow(<Wrapped />, { context });
119 wrapper.instance().componentDidMount();
120
121 wrapper.instance().componentWillUnmount();
122 expect(brcast.unsubscribe).to.have.callCount(0);
123 });
124 });
125 });
126});
127
\No newline at end of file