1 | import React from 'react';
|
2 | import { expect } from 'chai';
|
3 | import { shallow } from 'enzyme';
|
4 | import sinon from 'sinon-sandbox';
|
5 |
|
6 | import DirectionProvider from '../src/DirectionProvider';
|
7 | import { DIRECTIONS } from '../src/constants';
|
8 |
|
9 | describe('<DirectionProvider>', () => {
|
10 | let children;
|
11 | beforeEach(() => {
|
12 | children = <div>Foo</div>;
|
13 | });
|
14 |
|
15 | it('renders its children', () => {
|
16 | const wrapper = shallow(
|
17 | <DirectionProvider direction={DIRECTIONS.RTL}>{children}</DirectionProvider>,
|
18 | );
|
19 | expect(wrapper.contains(children)).to.eq(true);
|
20 | });
|
21 |
|
22 | it('renders a wrapping div with a dir attribute', () => {
|
23 | const direction = DIRECTIONS.RTL;
|
24 | const wrapper = shallow(
|
25 | <DirectionProvider direction={direction}>{children}</DirectionProvider>,
|
26 | );
|
27 | expect(wrapper).to.have.prop('dir', direction);
|
28 | });
|
29 |
|
30 | it('broadcasts the direction when the direction prop changes', () => {
|
31 | const direction = DIRECTIONS.LTR;
|
32 | const nextDirection = DIRECTIONS.RTL;
|
33 | const wrapper = shallow(
|
34 | <DirectionProvider direction={direction}>{children}</DirectionProvider>,
|
35 | );
|
36 | const broadcast = wrapper.instance().broadcast;
|
37 | const broadcastSpy = sinon.spy(broadcast, 'setState');
|
38 | wrapper.setProps({ direction: nextDirection });
|
39 | expect(broadcastSpy).to.have.callCount(1);
|
40 | });
|
41 |
|
42 | it('does not broadcast the direction when the direction prop stays the same', () => {
|
43 | const direction = DIRECTIONS.LTR;
|
44 | const nextDirection = DIRECTIONS.LTR;
|
45 | const wrapper = shallow(
|
46 | <DirectionProvider direction={direction}>{children}</DirectionProvider>,
|
47 | );
|
48 | const broadcast = wrapper.instance().broadcast;
|
49 | const broadcastSpy = sinon.spy(broadcast, 'setState');
|
50 | wrapper.setProps({ direction: nextDirection });
|
51 | expect(broadcastSpy).to.have.callCount(0);
|
52 | });
|
53 | });
|