1
1
import React from 'react' ;
2
2
import { mount , shallow } from 'enzyme' ;
3
- import { ChartProps , ChartMetadata , ChartPlugin , ChartFormData , SuperChart } from '../../src' ;
3
+ import { ChartProps } from '../../src' ;
4
+ import {
5
+ ChartKeys ,
6
+ DiligentChartPlugin ,
7
+ LazyChartPlugin ,
8
+ SlowChartPlugin ,
9
+ } from './MockChartPlugins' ;
10
+ import SuperChart from '../../src/components/SuperChart' ;
4
11
5
12
describe ( 'SuperChart' , ( ) => {
6
- const TestComponent = ( props : any ) => (
7
- < div className = "test-component" > { props . character || 'test-component' } </ div >
8
- ) ;
9
13
const chartProps = new ChartProps ( ) ;
10
14
11
- class MyChartPlugin extends ChartPlugin < ChartFormData > {
12
- constructor ( ) {
13
- super ( {
14
- metadata : new ChartMetadata ( {
15
- name : 'my-chart' ,
16
- thumbnail : '' ,
17
- } ) ,
18
- Chart : TestComponent ,
19
- transformProps : x => x ,
20
- } ) ;
21
- }
22
- }
23
-
24
- class SecondChartPlugin extends ChartPlugin < ChartFormData > {
25
- constructor ( ) {
26
- super ( {
27
- metadata : new ChartMetadata ( {
28
- name : 'second-chart' ,
29
- thumbnail : '' ,
30
- } ) ,
31
- // this mirrors `() => import(module)` syntax
32
- loadChart : ( ) => Promise . resolve ( { default : TestComponent } ) ,
33
- // promise without .default
34
- loadTransformProps : ( ) => Promise . resolve ( ( x : any ) => x ) ,
35
- } ) ;
36
- }
37
- }
38
-
39
- class SlowChartPlugin extends ChartPlugin < ChartFormData > {
40
- constructor ( ) {
41
- super ( {
42
- metadata : new ChartMetadata ( {
43
- name : 'slow-chart' ,
44
- thumbnail : '' ,
45
- } ) ,
46
- loadChart : ( ) =>
47
- new Promise ( resolve => {
48
- setTimeout ( ( ) => {
49
- resolve ( TestComponent ) ;
50
- } , 1000 ) ;
51
- } ) ,
52
- transformProps : x => x ,
53
- } ) ;
54
- }
55
- }
56
-
57
- new MyChartPlugin ( ) . configure ( { key : 'my-chart' } ) . register ( ) ;
58
- new SecondChartPlugin ( ) . configure ( { key : 'second-chart' } ) . register ( ) ;
59
- new SlowChartPlugin ( ) . configure ( { key : 'slow-chart' } ) . register ( ) ;
15
+ new DiligentChartPlugin ( ) . configure ( { key : ChartKeys . DILIGENT } ) . register ( ) ;
16
+ new LazyChartPlugin ( ) . configure ( { key : ChartKeys . LAZY } ) . register ( ) ;
17
+ new SlowChartPlugin ( ) . configure ( { key : ChartKeys . SLOW } ) . register ( ) ;
60
18
61
19
describe ( 'registered charts' , ( ) => {
62
20
it ( 'renders registered chart' , done => {
63
- const wrapper = shallow ( < SuperChart chartType = "my-chart" chartProps = { chartProps } /> ) ;
21
+ const wrapper = shallow (
22
+ < SuperChart chartType = { ChartKeys . DILIGENT } chartProps = { chartProps } /> ,
23
+ ) ;
64
24
setTimeout ( ( ) => {
65
25
expect ( wrapper . render ( ) . find ( 'div.test-component' ) ) . toHaveLength ( 1 ) ;
66
26
done ( ) ;
67
27
} , 0 ) ;
68
28
} ) ;
69
29
it ( 'renders registered chart with default export' , done => {
70
- const wrapper = shallow ( < SuperChart chartType = "second-chart" /> ) ;
30
+ const wrapper = shallow ( < SuperChart chartType = { ChartKeys . LAZY } /> ) ;
71
31
setTimeout ( ( ) => {
72
32
expect ( wrapper . render ( ) . find ( 'div.test-component' ) ) . toHaveLength ( 1 ) ;
73
33
done ( ) ;
@@ -82,40 +42,43 @@ describe('SuperChart', () => {
82
42
} , 5 ) ;
83
43
} ) ;
84
44
it ( 'adds id to container if specified' , done => {
85
- const wrapper = shallow ( < SuperChart chartType = "my-chart" id = "the-chart" /> ) ;
45
+ const wrapper = shallow ( < SuperChart chartType = { ChartKeys . DILIGENT } id = "the-chart" /> ) ;
86
46
setTimeout ( ( ) => {
87
47
expect ( wrapper . render ( ) . attr ( 'id' ) ) . toEqual ( 'the-chart' ) ;
88
48
done ( ) ;
89
49
} , 0 ) ;
90
50
} ) ;
91
51
it ( 'adds class to container if specified' , done => {
92
- const wrapper = shallow ( < SuperChart chartType = "my-chart" className = "the-chart" /> ) ;
52
+ const wrapper = shallow ( < SuperChart chartType = { ChartKeys . DILIGENT } className = "the-chart" /> ) ;
93
53
setTimeout ( ( ) => {
94
54
expect ( wrapper . hasClass ( 'the-chart' ) ) . toBeTruthy ( ) ;
95
55
done ( ) ;
96
56
} , 0 ) ;
97
57
} ) ;
98
58
it ( 'uses overrideTransformProps when specified' , done => {
99
59
const wrapper = shallow (
100
- < SuperChart chartType = "my-chart" overrideTransformProps = { ( ) => ( { character : 'hulk' } ) } /> ,
60
+ < SuperChart
61
+ chartType = { ChartKeys . DILIGENT }
62
+ overrideTransformProps = { ( ) => ( { message : 'hulk' } ) }
63
+ /> ,
101
64
) ;
102
65
setTimeout ( ( ) => {
103
66
expect (
104
67
wrapper
105
68
. render ( )
106
- . find ( 'div.test-component ' )
69
+ . find ( 'span.message ' )
107
70
. text ( ) ,
108
71
) . toEqual ( 'hulk' ) ;
109
72
done ( ) ;
110
73
} , 0 ) ;
111
74
} ) ;
112
75
it ( 'uses preTransformProps when specified' , done => {
113
76
const chartPropsWithPayload = new ChartProps ( {
114
- payload : { character : 'hulk' } ,
77
+ payload : { message : 'hulk' } ,
115
78
} ) ;
116
79
const wrapper = shallow (
117
80
< SuperChart
118
- chartType = "my-chart"
81
+ chartType = { ChartKeys . DILIGENT }
119
82
preTransformProps = { ( ) => chartPropsWithPayload }
120
83
overrideTransformProps = { props => props . payload }
121
84
/> ,
@@ -124,42 +87,52 @@ describe('SuperChart', () => {
124
87
expect (
125
88
wrapper
126
89
. render ( )
127
- . find ( 'div.test-component ' )
90
+ . find ( 'span.message ' )
128
91
. text ( ) ,
129
92
) . toEqual ( 'hulk' ) ;
130
93
done ( ) ;
131
94
} , 0 ) ;
132
95
} ) ;
133
96
it ( 'uses postTransformProps when specified' , done => {
134
97
const wrapper = shallow (
135
- < SuperChart chartType = "my-chart" postTransformProps = { ( ) => ( { character : 'hulk' } ) } /> ,
98
+ < SuperChart
99
+ chartType = { ChartKeys . DILIGENT }
100
+ postTransformProps = { ( ) => ( { message : 'hulk' } ) }
101
+ /> ,
136
102
) ;
137
103
setTimeout ( ( ) => {
138
104
expect (
139
105
wrapper
140
106
. render ( )
141
- . find ( 'div.test-component ' )
107
+ . find ( 'span.message ' )
142
108
. text ( ) ,
143
109
) . toEqual ( 'hulk' ) ;
144
110
done ( ) ;
145
111
} , 0 ) ;
146
112
} ) ;
147
113
it ( 'renders if chartProps is not specified' , done => {
148
- const wrapper = shallow ( < SuperChart chartType = "my-chart" /> ) ;
114
+ const wrapper = shallow ( < SuperChart chartType = { ChartKeys . DILIGENT } /> ) ;
149
115
setTimeout ( ( ) => {
150
116
expect ( wrapper . render ( ) . find ( 'div.test-component' ) ) . toHaveLength ( 1 ) ;
151
117
done ( ) ;
152
118
} , 0 ) ;
153
119
} ) ;
154
120
it ( 'does not render anything while waiting for Chart code to load' , done => {
155
- const wrapper = shallow ( < SuperChart chartType = "slow-chart" /> ) ;
121
+ const wrapper = shallow ( < SuperChart chartType = { ChartKeys . SLOW } /> ) ;
156
122
setTimeout ( ( ) => {
157
123
expect ( wrapper . render ( ) . children ( ) ) . toHaveLength ( 0 ) ;
158
124
done ( ) ;
159
125
} , 0 ) ;
160
126
} ) ;
127
+ it ( 'eventually renders after Chart is loaded' , done => {
128
+ const wrapper = shallow ( < SuperChart chartType = { ChartKeys . SLOW } /> ) ;
129
+ setTimeout ( ( ) => {
130
+ expect ( wrapper . render ( ) . find ( 'div.test-component' ) ) . toHaveLength ( 1 ) ;
131
+ done ( ) ;
132
+ } , 1500 ) ;
133
+ } ) ;
161
134
it ( 'does not render if chartProps is null' , done => {
162
- const wrapper = shallow ( < SuperChart chartType = "my-chart" chartProps = { null } /> ) ;
135
+ const wrapper = shallow ( < SuperChart chartType = { ChartKeys . DILIGENT } chartProps = { null } /> ) ;
163
136
setTimeout ( ( ) => {
164
137
expect ( wrapper . render ( ) . find ( 'div.test-component' ) ) . toHaveLength ( 0 ) ;
165
138
done ( ) ;
@@ -180,7 +153,7 @@ describe('SuperChart', () => {
180
153
describe ( '.processChartProps()' , ( ) => {
181
154
it ( 'use identity functions for unspecified transforms' , ( ) => {
182
155
const chart = new SuperChart ( {
183
- chartType : 'my-chart' ,
156
+ chartType : ChartKeys . DILIGENT ,
184
157
} ) ;
185
158
const chartProps2 = new ChartProps ( ) ;
186
159
expect ( chart . processChartProps ( { chartProps : chartProps2 } ) ) . toBe ( chartProps2 ) ;
0 commit comments