1
- import { SimpleChange } from '@angular/core' ;
1
+ import { SimpleChange , ComponentRef } from '@angular/core' ;
2
2
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
3
3
4
4
import { PlotlyComponent } from './plotly.component' ;
@@ -12,6 +12,7 @@ PlotlyService.setPlotly(PlotlyJS);
12
12
describe ( 'PlotlyComponent' , ( ) => {
13
13
let component : PlotlyComponent ;
14
14
let fixture : ComponentFixture < PlotlyComponent > ;
15
+ let componentRef : ComponentRef < PlotlyComponent > ;
15
16
let windowSpy : jasmine . SpyObj < Window > ;
16
17
17
18
beforeEach ( async ( ) => {
@@ -27,6 +28,7 @@ describe('PlotlyComponent', () => {
27
28
28
29
beforeEach ( ( ) => {
29
30
fixture = TestBed . createComponent ( PlotlyComponent ) ;
31
+ componentRef = fixture . componentRef ;
30
32
component = fixture . componentInstance ;
31
33
fixture . detectChanges ( ) ;
32
34
} ) ;
@@ -37,56 +39,53 @@ describe('PlotlyComponent', () => {
37
39
} ) ;
38
40
39
41
it ( 'should receive the style from the property' , ( ) => {
40
- component . style = { 'background-color' : 'red' } ;
42
+ componentRef . setInput ( ' style' , { 'background-color' : 'red' } ) ;
41
43
fixture . detectChanges ( ) ;
42
44
expect ( component . plotEl . nativeElement . style . backgroundColor ) . toBe ( 'red' ) ;
43
45
} ) ;
44
46
45
47
it ( 'should add the id in the #plotEl' , ( ) => {
46
48
expect ( component . plotEl . nativeElement . id ) . toBe ( '' ) ;
47
- component . divId = 'some-id' ;
49
+ componentRef . setInput ( ' divId' , 'some-id' ) ;
48
50
fixture . detectChanges ( ) ;
49
51
expect ( component . plotEl . nativeElement . id ) . toBe ( 'some-id' ) ;
50
- component . divId = undefined ;
52
+ componentRef . setInput ( ' divId' , undefined ) ;
51
53
fixture . detectChanges ( ) ;
52
54
expect ( component . plotEl . nativeElement . id ) . toBe ( '' ) ;
53
55
} ) ;
54
56
55
57
it ( 'should update when change the revision number' , ( ) => {
56
58
spyOn ( component , 'updatePlot' ) ;
57
59
58
- component . revision = 0 ;
59
- component . ngOnChanges ( { revision : new SimpleChange ( null , component . revision , true ) } ) ;
60
+ componentRef . setInput ( 'revision' , 0 ) ;
60
61
fixture . detectChanges ( ) ;
61
62
expect ( component . updatePlot ) . not . toHaveBeenCalled ( ) ;
62
63
63
- component . revision = 1 ;
64
- component . ngOnChanges ( { revision : new SimpleChange ( 0 , component . revision , false ) } ) ;
64
+ componentRef . setInput ( 'revision' , 1 ) ;
65
65
fixture . detectChanges ( ) ;
66
- expect ( component . updatePlot ) . toHaveBeenCalled ( ) ;
66
+ expect ( component . updatePlot ) . toHaveBeenCalledTimes ( 1 ) ;
67
67
68
- component . revision = 2 ;
69
- component . ngOnChanges ( { revision : new SimpleChange ( 1 , component . revision , false ) } ) ;
68
+ componentRef . setInput ( 'revision' , 2 ) ;
70
69
fixture . detectChanges ( ) ;
71
70
expect ( component . updatePlot ) . toHaveBeenCalledTimes ( 2 ) ;
72
71
} ) ;
73
72
74
73
it ( 'should update the graph when the data changes' , ( done ) => {
75
74
spyOn ( component , 'updatePlot' ) ;
76
- component . data = [ { y : [ 10 , 15 , 13 , 17 ] , type : 'box' } ] ;
75
+ componentRef . setInput ( ' data' , [ { y : [ 10 , 15 , 13 , 17 ] , type : 'box' } ] ) ;
77
76
78
77
component . createPlot ( ) . then ( ( ) => {
79
78
component . ngDoCheck ( ) ;
80
79
expect ( component . updatePlot ) . not . toHaveBeenCalled ( ) ;
81
80
82
- component . data = [ { y : [ 11 , 15 , 13 , 17 ] , type : 'box' } ] ;
81
+ componentRef . setInput ( ' data' , [ { y : [ 11 , 15 , 13 , 17 ] , type : 'box' } ] ) ;
83
82
component . ngDoCheck ( ) ;
84
83
expect ( component . updatePlot ) . toHaveBeenCalled ( ) ;
85
84
86
85
component . ngDoCheck ( ) ;
87
86
expect ( component . updatePlot ) . toHaveBeenCalledTimes ( 1 ) ;
88
87
89
- component . data [ 0 ] . y [ 0 ] = 12 ;
88
+ component . data ( ) [ 0 ] . y [ 0 ] = 12 ;
90
89
component . ngDoCheck ( ) ;
91
90
expect ( component . updatePlot ) . toHaveBeenCalledTimes ( 2 ) ;
92
91
done ( ) ;
@@ -95,19 +94,19 @@ describe('PlotlyComponent', () => {
95
94
96
95
it ( 'should update the layout when the object changes' , ( done ) => {
97
96
spyOn ( component , 'updatePlot' ) ;
98
- component . layout = { title : 'title one' } ;
97
+ componentRef . setInput ( ' layout' , { title : 'title one' } ) ;
99
98
component . createPlot ( ) . then ( ( ) => {
100
99
component . ngDoCheck ( ) ;
101
100
expect ( component . updatePlot ) . not . toHaveBeenCalled ( ) ;
102
101
103
- component . layout = { title : 'title two' } ;
102
+ componentRef . setInput ( ' layout' , { title : 'title two' } ) ;
104
103
component . ngDoCheck ( ) ;
105
104
expect ( component . updatePlot ) . toHaveBeenCalled ( ) ;
106
105
107
106
component . ngDoCheck ( ) ;
108
107
expect ( component . updatePlot ) . toHaveBeenCalledTimes ( 1 ) ;
109
108
110
- component . layout [ 'title' ] = 'title three ' ;
109
+ component . layout ( ) [ 'title' ] = 'title three ' ;
111
110
component . ngDoCheck ( ) ;
112
111
expect ( component . updatePlot ) . toHaveBeenCalledTimes ( 2 ) ;
113
112
done ( ) ;
@@ -118,12 +117,12 @@ describe('PlotlyComponent', () => {
118
117
expect ( component . getClassName ( ) ) . toBe ( 'js-plotly-plot' ) ;
119
118
expect ( component . plotEl . nativeElement . className ) . toBe ( 'js-plotly-plot' ) ;
120
119
121
- component . className = 'some-class' ;
120
+ componentRef . setInput ( ' className' , 'some-class' ) ;
122
121
fixture . detectChanges ( ) ;
123
122
expect ( component . getClassName ( ) ) . toBe ( 'js-plotly-plot some-class' ) ;
124
123
expect ( component . plotEl . nativeElement . className ) . toBe ( 'js-plotly-plot some-class' ) ;
125
124
126
- component . className = [ 'test2' , 'test3' ] ;
125
+ componentRef . setInput ( ' className' , [ 'test2' , 'test3' ] ) ;
127
126
fixture . detectChanges ( ) ;
128
127
expect ( component . getClassName ( ) ) . toBe ( 'js-plotly-plot test2 test3' ) ;
129
128
expect ( component . plotEl . nativeElement . className ) . toBe ( 'js-plotly-plot test2 test3' ) ;
@@ -135,9 +134,9 @@ describe('PlotlyComponent', () => {
135
134
136
135
expect ( component . getWindow ( ) . gd ) . toBeUndefined ( ) ;
137
136
component . plotlyInstance = document . createElement ( 'div' ) as any ;
138
- component . debug = true ;
137
+ componentRef . setInput ( ' debug' , true ) ;
139
138
fixture . detectChanges ( ) ;
140
- component . ngOnChanges ( { debug : new SimpleChange ( false , component . debug , false ) } ) ;
139
+ component . ngOnChanges ( { debug : new SimpleChange ( false , component . debug ( ) , false ) } ) ;
141
140
142
141
expect ( component . updatePlot ) . toHaveBeenCalled ( ) ;
143
142
setTimeout ( ( ) => {
@@ -163,11 +162,11 @@ describe('PlotlyComponent', () => {
163
162
expect ( component . getWindow ( ) . addEventListener ) . not . toHaveBeenCalled ( ) ;
164
163
expect ( component . resizeHandler ) . toBeUndefined ( ) ;
165
164
166
- component . useResizeHandler = true ;
165
+ componentRef . setInput ( ' useResizeHandler' , true ) ;
167
166
component . updateWindowResizeHandler ( ) ;
168
167
expect ( component . resizeHandler ) . toBeDefined ( ) ;
169
168
170
- component . useResizeHandler = false ;
169
+ componentRef . setInput ( ' useResizeHandler' , false ) ;
171
170
component . updateWindowResizeHandler ( ) ;
172
171
expect ( component . resizeHandler ) . toBeUndefined ( ) ;
173
172
} ) ;
@@ -176,9 +175,9 @@ describe('PlotlyComponent', () => {
176
175
const windowListenerCount = ( window as any ) . eventListeners ( ) . length ;
177
176
178
177
// make component responsive via both the lib and the component (at least 2 window events are added)
179
- component . layout = { title : 'responsive' , autosize : true } ;
180
- component . config = { responsive : true } ;
181
- component . useResizeHandler = true ;
178
+ componentRef . setInput ( ' layout' , { title : 'responsive' , autosize : true } ) ;
179
+ componentRef . setInput ( ' config' , { responsive : true } ) ;
180
+ componentRef . setInput ( ' useResizeHandler' , true ) ;
182
181
183
182
await component . createPlot ( ) ;
184
183
await fixture . whenStable ( ) ;
@@ -193,9 +192,9 @@ describe('PlotlyComponent', () => {
193
192
it ( 'should not cause errors if window is resized after a responsive chart is destroyed' , async ( ) => {
194
193
195
194
// make component responsive via both the lib and the component
196
- component . layout = { title : 'responsive' , autosize : true } ;
197
- component . config = { responsive : true } ;
198
- component . useResizeHandler = true ;
195
+ componentRef . setInput ( ' layout' , { title : 'responsive' , autosize : true } ) ;
196
+ componentRef . setInput ( ' config' , { responsive : true } ) ;
197
+ componentRef . setInput ( ' useResizeHandler' , true ) ;
199
198
200
199
await component . createPlot ( ) ;
201
200
await fixture . whenStable ( ) ;
@@ -217,31 +216,6 @@ describe('PlotlyComponent', () => {
217
216
expect ( PlotlyJS . Plots . resize ) . not . toHaveBeenCalled ( ) ;
218
217
} ) ;
219
218
220
- xit ( 'should load a theme' , async ( ) => {
221
- spyOn ( component , 'loadTheme' ) . and . callThrough ( ) ;
222
- spyOn ( component . themeLoader , 'load' ) . and . callThrough ( ) ;
223
-
224
- component . theme = 'plotly_dark' ;
225
-
226
- component . ngOnInit ( ) ;
227
- await fixture . whenStable ( ) ;
228
-
229
- expect ( component . loadTheme ) . toHaveBeenCalled ( ) ;
230
- expect ( component . themeLoader . load ) . toHaveBeenCalledOnceWith ( 'plotly_dark' ) ;
231
- } ) ;
232
-
233
- xit ( 'should load NOT a theme' , async ( ) => {
234
- spyOn ( component , 'loadTheme' ) . and . callThrough ( ) ;
235
- spyOn ( component . themeLoader , 'load' ) . and . callThrough ( ) ;
236
-
237
- component . theme = 'none' ;
238
-
239
- component . ngOnInit ( ) ;
240
- await fixture . whenStable ( ) ;
241
-
242
- expect ( component . loadTheme ) . not . toHaveBeenCalled ( ) ;
243
- expect ( component . themeLoader . load ) . not . toHaveBeenCalledOnceWith ( 'plotly_dark' ) ;
244
- } ) ;
245
219
246
220
it ( 'should not cause errors if ngOnDestroy is called before plotly is initialized' , ( ) => {
247
221
// note that this test intentionally does not call ngOnInit/whenStable
0 commit comments