1
1
import { createElement } from 'lwc' ;
2
- import { loadScript } from 'lightning/platformResourceLoader' ;
2
+ // import { loadScript } from 'lightning/platformResourceLoader';
3
3
4
4
import { BAR_CHART_TYPE , LINE_CHART_TYPE } from 'c/constants' ;
5
5
6
6
import Chart from 'c/chart' ;
7
-
7
+ import DataSet from 'c/dataset' ;
8
+ import Data from 'c/data' ;
9
+ import Title from 'c/title' ;
10
+ import Legend from 'c/legend' ;
8
11
const STATIC_RESOURCE_NAME = 'chartjs_v280' ;
9
12
13
+ let mockScriptSuccess = true ;
14
+ const flushPromises = ( ) => {
15
+ // eslint-disable-next-line no-undef
16
+ return new Promise ( resolve => setImmediate ( resolve ) ) ;
17
+ } ;
18
+
19
+ jest . mock (
20
+ 'lightning/platformResourceLoader' ,
21
+ ( ) => {
22
+ return {
23
+ loadScript ( ) {
24
+ return new Promise ( ( resolve , reject ) => {
25
+ // If the variable is false we're simulating an error when loading
26
+ // the script resource.
27
+ if ( ! mockScriptSuccess ) {
28
+ reject ( 'Could not load script' ) ;
29
+ } else {
30
+ global . window = { } ;
31
+ global . window . Chart = require ( `../../force-app/main/default/staticresources/chartjs_v280.js` ) ;
32
+ resolve ( ) ;
33
+ }
34
+ } ) ;
35
+ }
36
+ } ;
37
+ } ,
38
+ { virtual : true }
39
+ ) ;
40
+
10
41
const CHARTS = [
11
42
{ class : Chart , type : BAR_CHART_TYPE } ,
12
43
{ class : Chart , type : LINE_CHART_TYPE }
@@ -20,13 +51,10 @@ describe('Chart: ChartJs library', () => {
20
51
} ) ;
21
52
element . type = LINE_CHART_TYPE ;
22
53
document . body . appendChild ( element ) ;
23
-
24
- // Validation that the loadScript promise is called once.
25
- expect ( loadScript . mock . calls . length ) . toBe ( 1 ) ;
26
- // Validation that the chartjs static resource is passed as parameter.
27
- expect ( loadScript . mock . calls [ 0 ] [ 1 ] ) . toEqual ( STATIC_RESOURCE_NAME ) ;
28
-
29
- document . body . removeChild ( element ) ;
54
+ Promise . resolve ( ) . then ( ( ) => {
55
+ expect ( element . uuid ) . toBeDefined ( ) ;
56
+ document . body . removeChild ( element ) ;
57
+ } ) ;
30
58
} ) ;
31
59
} ) ;
32
60
@@ -107,6 +135,123 @@ describe('Chart: property', () => {
107
135
await expect ( element . uuid ) . toBe ( 'xyz' ) ;
108
136
} ) ;
109
137
} ) ;
138
+
139
+ describe ( 'Chart: methods' , ( ) => {
140
+ let chart , title , legend , dataSet , data ;
141
+ beforeAll ( ( ) => {
142
+ chart = createElement ( 'x-chart' , { is : Chart } ) ;
143
+ document . body . appendChild ( chart ) ;
144
+ title = createElement ( 'x-title' , { is : Title } ) ;
145
+ chart . appendChild ( title ) ;
146
+ legend = createElement ( 'x-legend' , { is : Legend } ) ;
147
+ chart . appendChild ( legend ) ;
148
+ dataSet = createElement ( 'x-dataset' , { is : DataSet } ) ;
149
+ chart . appendChild ( dataSet ) ;
150
+ data = createElement ( 'x-data' , { is : Data } ) ;
151
+ dataSet . appendChild ( data ) ;
152
+ data . label = 'data' ;
153
+ data . detail = '[10]' ;
154
+ title . text = 'test' ;
155
+ legend . position = 'top' ;
156
+ chart . type = LINE_CHART_TYPE ;
157
+ } ) ;
158
+
159
+ afterAll ( ( ) => {
160
+ dataSet . removeChild ( data ) ;
161
+ chart . removeChild ( dataSet ) ;
162
+ chart . removeChild ( title ) ;
163
+ chart . removeChild ( legend ) ;
164
+ document . body . removeChild ( chart ) ;
165
+ } ) ;
166
+ test ( 'updateChart' , async ( ) => {
167
+ return flushPromises ( )
168
+ . then ( flushPromises )
169
+ . then ( flushPromises )
170
+ . then ( ( ) => {
171
+ chart . updateChart ( ) ;
172
+ expect ( chart . uuid ) . toBeDefined ( ) ;
173
+ } ) ;
174
+ } ) ;
175
+ test ( 'resetChart' , async ( ) => {
176
+ return flushPromises ( ) . then ( ( ) => {
177
+ chart . resetChart ( ) ;
178
+ expect ( chart . uuid ) . toBeDefined ( ) ;
179
+ } ) ;
180
+ } ) ;
181
+ test ( 'renderChart' , async ( ) => {
182
+ return flushPromises ( ) . then ( ( ) => {
183
+ chart . renderChart ( ) ;
184
+ expect ( chart . uuid ) . toBeDefined ( ) ;
185
+ } ) ;
186
+ } ) ;
187
+ test ( 'stopChart' , async ( ) => {
188
+ return flushPromises ( ) . then ( ( ) => {
189
+ const r = chart . stopChart ( ) ;
190
+ expect ( r ) . toBeInstanceOf ( Chart ) ;
191
+ } ) ;
192
+ } ) ;
193
+ test ( 'resizeChart' , async ( ) => {
194
+ return flushPromises ( ) . then ( ( ) => {
195
+ const r = chart . resizeChart ( ) ;
196
+ expect ( r ) . toBeInstanceOf ( Chart ) ;
197
+ } ) ;
198
+ } ) ;
199
+ test ( 'clearChart' , async ( ) => {
200
+ return flushPromises ( ) . then ( ( ) => {
201
+ const r = chart . clearChart ( ) ;
202
+ expect ( r ) . toBeInstanceOf ( Chart ) ;
203
+ } ) ;
204
+ } ) ;
205
+ test ( 'toBase64ImageChart' , async ( ) => {
206
+ return flushPromises ( ) . then ( ( ) => {
207
+ const b64 = chart . toBase64ImageChart ( ) ;
208
+ expect ( b64 ) . toBeDefined ( ) ;
209
+ } ) ;
210
+ } ) ;
211
+ test ( 'generateLegendChart' , async ( ) => {
212
+ return flushPromises ( ) . then ( ( ) => {
213
+ const legendChart = chart . generateLegendChart ( ) ;
214
+ expect ( legendChart ) . toBeDefined ( ) ;
215
+ } ) ;
216
+ } ) ;
217
+ test ( 'getElementAtEventChart' , async ( ) => {
218
+ return flushPromises ( ) . then ( ( ) => {
219
+ // todo expose events on the canvas
220
+ // expose catcher to implement.
221
+ // define one catcher and trigger an event
222
+ // Fetch the event and call getElementAtEventChart
223
+ //const el = chart.getElementAtEventChart();
224
+ //expect(el).toBeDefined();
225
+ } ) ;
226
+ } ) ;
227
+ test ( 'getElementsAtEventChart' , async ( ) => {
228
+ return flushPromises ( ) . then ( ( ) => {
229
+ //const el = chart.getElementsAtEventChart();
230
+ //expect(el).toBeDefined();
231
+ } ) ;
232
+ } ) ;
233
+ test ( 'getDatasetAtEventChart' , async ( ) => {
234
+ return flushPromises ( ) . then ( ( ) => {
235
+ //const el = chart.getDatasetAtEventChart();
236
+ //expect(el).toBeDefined();
237
+ } ) ;
238
+ } ) ;
239
+ test ( 'getDatasetMetaChart' , async ( ) => {
240
+ return flushPromises ( ) . then ( ( ) => {
241
+ const el = chart . getDatasetMetaChart ( 0 ) ;
242
+ expect ( el ) . toBeDefined ( ) ;
243
+ } ) ;
244
+ } ) ;
245
+
246
+ test ( 'destroyChart' , async ( ) => {
247
+ return flushPromises ( )
248
+ . then ( flushPromises )
249
+ . then ( ( ) => {
250
+ chart . destroyChart ( ) ;
251
+ expect ( chart . uuid ) . toBeDefined ( ) ;
252
+ } ) ;
253
+ } ) ;
254
+ } ) ;
110
255
// TODO: checkOptions()
111
256
112
257
// TODO: Option event Listener & Disconnect Event Listener
0 commit comments