Skip to content

Commit e6db498

Browse files
committed
Improve chart.test.js
1 parent bf1948a commit e6db498

File tree

5 files changed

+194
-12
lines changed

5 files changed

+194
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
- [About the project](https://scolladon.github.io/lwcc/docs/general/about.html)
3939
- [Playground](https://scolladon.github.io/lwcc/docs/general/playground.html)
4040

41-
4241
## Contributing
4342

4443
Contributions are what make the trailblazer community such an amazing place. I regard this component as a way to inspire and learn from others. Any contributions you make are **greatly appreciated**.
@@ -51,4 +50,5 @@ See [contributing.md](/CONTRIBUTING.md) for lwcc principles.
5150
- [nanoid.js v3.1.3](https://github.com/ai/nanoid)
5251

5352
## License
54-
LWCC is available under the [MIT license](LICENSE.md)
53+
54+
LWCC is available under the [MIT license](LICENSE.md)

__tests__/unit/chart.test.js

Lines changed: 154 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11
import { createElement } from 'lwc';
2-
import { loadScript } from 'lightning/platformResourceLoader';
2+
//import { loadScript } from 'lightning/platformResourceLoader';
33

44
import { BAR_CHART_TYPE, LINE_CHART_TYPE } from 'c/constants';
55

66
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';
811
const STATIC_RESOURCE_NAME = 'chartjs_v280';
912

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+
1041
const CHARTS = [
1142
{ class: Chart, type: BAR_CHART_TYPE },
1243
{ class: Chart, type: LINE_CHART_TYPE }
@@ -20,13 +51,10 @@ describe('Chart: ChartJs library', () => {
2051
});
2152
element.type = LINE_CHART_TYPE;
2253
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+
});
3058
});
3159
});
3260

@@ -107,6 +135,123 @@ describe('Chart: property', () => {
107135
await expect(element.uuid).toBe('xyz');
108136
});
109137
});
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+
});
110255
// TODO: checkOptions()
111256

112257
// TODO: Option event Listener & Disconnect Event Listener

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ module.exports = {
124124
// runner: "jest-runner",
125125

126126
// The paths to modules that run some code to configure or set up the testing environment before each test
127-
setupFiles: ['./__tests__/__utils__/testAttributeHandler.js'],
127+
setupFiles: [
128+
'./__tests__/__utils__/testAttributeHandler.js',
129+
'jest-canvas-mock'
130+
],
128131

129132
// A list of paths to modules that run some code to configure or set up the testing framework before each test
130133
// setupFilesAfterEnv: [],

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@salesforce/sfdx-lwc-jest": "^0.7.1",
3131
"eslint": "^5.16.0",
3232
"husky": "^4.2.3",
33+
"jest-canvas-mock": "^2.2.0",
3334
"lint-staged": "^10.1.2",
3435
"prettier": "^1.19.1",
3536
"prettier-plugin-apex": "^1.0.0",

0 commit comments

Comments
 (0)