|
1 | 1 | /* eslint-disable no-undef */ |
2 | 2 | import React from 'react'; |
3 | | -import { shallow, mount } from 'enzyme'; |
| 3 | +import { mount } from 'enzyme'; |
4 | 4 | import EchartsReact from '../src'; |
5 | 5 | import option from './option'; |
6 | 6 |
|
7 | | -test('test echarts-for-react\'s index.js.', () => { |
8 | | - let component = mount(<EchartsReact |
9 | | - option={option} |
10 | | - className="echarts-for-react" |
11 | | - />); |
12 | | - expect(component.exists()).toBe(true); |
13 | | - |
14 | | - expect(component.getDOMNode().className).toBe('echarts-for-react-div echarts-for-react'); |
15 | | - |
16 | | - expect(component.find('div').length).toBe(1); |
17 | | - |
18 | | - expect(component.name()).toEqual('EchartsReact'); |
19 | | - // default props |
20 | | - expect(component.instance().props.option).toEqual(option); |
21 | | - expect(component.props().style).toEqual({}); |
22 | | - |
23 | | - expect(component.getDOMNode().style.height).toEqual('300px'); |
24 | | - expect(component.instance().props.className).toEqual('echarts-for-react'); |
25 | | - expect(component.instance().props.notMerge).toEqual(false); |
26 | | - expect(component.instance().props.lazyUpdate).toEqual(false); |
27 | | - expect(component.instance().props.theme).toEqual(null); |
28 | | - expect(typeof component.instance().props.onChartReady).toEqual('function'); |
29 | | - expect(component.instance().props.showLoading).toEqual(false); |
30 | | - expect(component.instance().props.onEvents).toEqual({}); |
31 | | - |
32 | | - |
33 | | - const testFunc = () => {}; |
34 | | - const chartReady = jest.fn(); |
35 | | - // not default props |
36 | | - component = mount(<EchartsReact |
37 | | - option={option} |
38 | | - style={{ width: 100 }} |
39 | | - notMerge |
40 | | - lazyUpdate |
41 | | - theme="test_theme" |
42 | | - onChartReady={chartReady} |
43 | | - showLoading |
44 | | - onEvents={{ onClick: testFunc }} |
45 | | - className="echarts-for-react" |
46 | | - />); |
47 | | - |
48 | | - expect(chartReady).toBeCalled(); |
49 | | - |
50 | | - expect(component.instance().props.option).toEqual(option); |
51 | | - expect(component.instance().props.style).toEqual({ width: 100 }); |
52 | | - expect(component.instance().props.className).toEqual('echarts-for-react'); |
53 | | - expect(component.instance().props.notMerge).toEqual(true); |
54 | | - expect(component.instance().props.lazyUpdate).toEqual(true); |
55 | | - expect(component.instance().props.theme).toEqual('test_theme'); |
56 | | - expect(component.instance().props.onChartReady).toEqual(chartReady); |
57 | | - expect(component.instance().props.showLoading).toEqual(true); |
58 | | - expect(component.instance().props.onEvents).toEqual({ onClick: testFunc }); |
59 | | -}); |
60 | 7 |
|
| 8 | +describe('index.js', () => { |
| 9 | + test('default props', () => { |
| 10 | + const component = mount(<EchartsReact |
| 11 | + option={option} |
| 12 | + className="echarts-for-react-root" |
| 13 | + />); |
| 14 | + |
| 15 | + expect(component.exists()).toBe(true); |
| 16 | + |
| 17 | + expect(component.find('div').length).toBe(1); |
| 18 | + |
| 19 | + // root tag |
| 20 | + expect(component.getDOMNode().nodeName.toLowerCase()).toBe('div'); |
| 21 | + // class name |
| 22 | + expect(component.getDOMNode().className).toBe('echarts-for-react echarts-for-react-root'); |
| 23 | + // style |
| 24 | + expect(component.getDOMNode().style.height).toBe('300px'); |
| 25 | + // default props |
| 26 | + expect(component.props().option).toEqual(option); |
| 27 | + expect(component.props().style).toEqual({}); |
| 28 | + expect(component.props().className).toBe('echarts-for-react-root'); |
| 29 | + expect(component.props().notMerge).toBe(false); |
| 30 | + expect(component.props().lazyUpdate).toBe(false); |
| 31 | + expect(component.props().theme).toBe(null); |
| 32 | + expect(typeof component.props().onChartReady).toBe('function'); |
| 33 | + expect(component.props().showLoading).toBe(false); |
| 34 | + expect(component.props().onEvents).toEqual({}); |
| 35 | + }); |
61 | 36 |
|
62 | | -test('test echarts-for-react update props.', () => { |
63 | | - const component = shallow(<EchartsReact |
64 | | - option={option} |
65 | | - className="echarts-for-react" |
66 | | - />); |
| 37 | + test('override props', () => { |
| 38 | + const testOnChartReadyFunc = jest.fn(); |
| 39 | + const testFunc = () => {}; |
| 40 | + // not default props |
| 41 | + const component = mount(<EchartsReact |
| 42 | + option={option} |
| 43 | + style={{ width: 100 }} |
| 44 | + notMerge |
| 45 | + lazyUpdate |
| 46 | + theme="test_theme" |
| 47 | + onChartReady={testOnChartReadyFunc} |
| 48 | + showLoading |
| 49 | + onEvents={{ onClick: testFunc }} |
| 50 | + />); |
| 51 | + |
| 52 | + // default props |
| 53 | + expect(component.props().option).toEqual(option); |
| 54 | + expect(component.props().style).toEqual({ width: 100 }); |
| 55 | + expect(component.props().className).toBe(''); |
| 56 | + expect(component.props().notMerge).toBe(true); |
| 57 | + expect(component.props().lazyUpdate).toBe(true); |
| 58 | + expect(component.props().theme).toBe('test_theme'); |
| 59 | + expect(typeof component.props().onChartReady).toBe('function'); |
| 60 | + expect(component.props().showLoading).toBe(true); |
| 61 | + expect(component.props().onEvents).toEqual({ onClick: testFunc }); |
| 62 | + |
| 63 | + expect(testOnChartReadyFunc).toBeCalled(); |
| 64 | + }); |
67 | 65 |
|
68 | | - expect(component.props().style).toEqual({ height: 300 }); |
69 | | - expect(component.hasClass('echarts-for-react')).toBe(true); |
70 | | - expect(component.hasClass('echarts-for-react-div')).toBe(true); |
| 66 | + test('update props', () => { |
| 67 | + const component = mount(<EchartsReact |
| 68 | + option={option} |
| 69 | + className="test-classname" |
| 70 | + />); |
71 | 71 |
|
72 | | - component.setProps({ |
73 | | - className: 'test-classname', |
74 | | - style: { height: 500 }, |
75 | | - }); |
| 72 | + expect(component.props().style).toEqual({}); |
| 73 | + expect(component.getDOMNode().style.height).toBe('300px'); |
76 | 74 |
|
77 | | - component.update(); |
| 75 | + const preId = component.instance().getEchartsInstance().id; |
| 76 | + // udpate props |
| 77 | + component.setProps({ |
| 78 | + className: 'test-classname', |
| 79 | + style: { height: 500 }, |
| 80 | + }); |
78 | 81 |
|
79 | | - expect(component.props().style).toEqual({ height: 500 }); |
80 | | - expect(component.hasClass('test-classname')).toBe(true); |
81 | | - expect(component.hasClass('echarts-for-react-div')).toBe(true); |
82 | | -}); |
| 82 | + component.update(); // force update |
83 | 83 |
|
| 84 | + expect(component.props().style).toEqual({ height: 500 }); |
| 85 | + expect(component.getDOMNode().style.height).toBe('500px'); |
84 | 86 |
|
85 | | -test('test echarts-for-react getEchartsInstance.', () => { |
86 | | - const component = mount(<EchartsReact |
87 | | - option={option} |
88 | | - className="echarts-for-react" |
89 | | - />); |
| 87 | + expect(component.props().className).toBe('test-classname'); |
90 | 88 |
|
91 | | - expect(component.instance().getEchartsInstance().displayName).toBe('EchartsInstance'); |
92 | | -}); |
| 89 | + expect(preId).toBe(component.instance().getEchartsInstance().id); |
| 90 | + }); |
93 | 91 |
|
| 92 | + test('getEchartsInstance', () => { |
| 93 | + const component = mount(<EchartsReact |
| 94 | + className="cls" |
| 95 | + option={option} |
| 96 | + />); |
94 | 97 |
|
95 | | -test('test echarts-for-react unmount.', () => { |
96 | | - const component = mount(<EchartsReact |
97 | | - option={option} |
98 | | - className="echarts-for-react" |
99 | | - />); |
| 98 | + // echarts instance, id 以 ec_ 开头 |
| 99 | + expect(component.instance().getEchartsInstance().id.substring(0, 3)).toBe('ec_'); |
| 100 | + }); |
| 101 | + |
| 102 | + test('upadte theme', () => { |
| 103 | + const component = mount(<EchartsReact |
| 104 | + option={option} |
| 105 | + theme="hello" |
| 106 | + />); |
100 | 107 |
|
101 | | - component.unmount(); |
102 | | - expect(true).toBe(true); |
| 108 | + const preId = component.instance().getEchartsInstance().id; |
| 109 | + // udpate props |
| 110 | + component.setProps({ |
| 111 | + theme: 'world', |
| 112 | + }); |
| 113 | + |
| 114 | + component.update(); // force update |
| 115 | + expect(preId).not.toBe(component.instance().getEchartsInstance().id); |
| 116 | + }); |
| 117 | + |
| 118 | + test('unmount', () => { |
| 119 | + const component = mount(<EchartsReact |
| 120 | + option={option} |
| 121 | + className="cls" |
| 122 | + />); |
| 123 | + |
| 124 | + component.unmount(); |
| 125 | + expect(() => { component.instance(); }).toThrow(); |
| 126 | + }); |
103 | 127 | }); |
0 commit comments