Skip to content

Commit 3bef9e5

Browse files
authored
Feature/various improvements (#47)
* Add a callback when Chartjs is loaded * Put flushPromise as a utils method and refactor * Test reactivity manager
1 parent 935703c commit 3bef9e5

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

__tests__/__utils__/testAttributeHandler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ global.testAttribute = (constructor, listChartOptionMock, eventName) => {
7878
testDOMElementInteraction(constructor);
7979
testChartOptions(constructor, listChartOptionMock, eventName);
8080
};
81+
82+
global.flushPromises = () => {
83+
// eslint-disable-next-line no-undef
84+
return new Promise(resolve => setImmediate(resolve));
85+
};

__tests__/unit/chart.test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ import DataSet from 'c/dataset';
88
import Data from 'c/data';
99
import Title from 'c/title';
1010
import Legend from 'c/legend';
11-
const STATIC_RESOURCE_NAME = 'chartjs_v280';
1211

1312
let mockScriptSuccess = true;
14-
const flushPromises = () => {
15-
// eslint-disable-next-line no-undef
16-
return new Promise(resolve => setImmediate(resolve));
17-
};
1813

1914
jest.mock(
2015
'lightning/platformResourceLoader',
@@ -166,6 +161,14 @@ describe('Chart: property', () => {
166161

167162
expect(element.canvasOnmouseout).toEqual({});
168163
});
164+
test('chartjsLoadedCallback was called', async () => {
165+
let chartjsCb = jest.fn();
166+
element.chartjsloadedCallback = chartjsCb;
167+
return flushPromises().then(() => {
168+
expect(chartjsCb).toBeCalled();
169+
expect(element.chartjsloadedCallback).toEqual(chartjsCb);
170+
});
171+
});
169172
});
170173

171174
describe('Chart: methods', () => {

__tests__/unit/chartBuilder.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ describe('test property', () => {
109109
});
110110
});
111111

112-
const flushPromises = () => {
113-
// eslint-disable-next-line no-undef
114-
return new Promise(resolve => setImmediate(resolve));
115-
};
116-
117112
const MOCK_GETCHARTDATA = [{ label: 'test', detail: [10] }];
118113
// Sample error for imperative Apex call
119114
const APEX_ERROR = {

__tests__/unit/microTaskHandler.test.js

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import ReactivityManager from 'c/reactivityManager';
2+
3+
describe('Reactivity Manager: ', () => {
4+
const throttledFn = jest.fn();
5+
const rm = new ReactivityManager();
6+
rm.registerJob(throttledFn);
7+
let reactivityProxy;
8+
9+
test('get reactivity Proxy', () => {
10+
reactivityProxy = rm.getReactivityProxy();
11+
expect(reactivityProxy).toEqual({});
12+
expect(throttledFn).not.toBeCalled();
13+
});
14+
15+
test('proxy trigger throttling of the registered job', async () => {
16+
reactivityProxy.test = 'test';
17+
expect(throttledFn).not.toBeCalled();
18+
await flushPromises().then(() => {
19+
expect(throttledFn).toBeCalled();
20+
});
21+
});
22+
test('manual call trigger throttling', async () => {
23+
rm.throttleRegisteredJob();
24+
expect(throttledFn).not.toBeCalled();
25+
await flushPromises().then(() => {
26+
expect(throttledFn).toBeCalled();
27+
});
28+
});
29+
});

force-app/main/default/lwc/chart/chart.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ export default class Chart extends LightningElement {
2828
this._uuid = v;
2929
}
3030

31+
// callback used for knowing when chart js is loaded in the dom
32+
// convenient to define new charts, new axes type and new plugins
33+
_chartjsLoadedCallback;
34+
@api
35+
get chartjsloadedCallback() {
36+
return this._chartjsLoadedCallback;
37+
}
38+
set chartjsloadedCallback(v) {
39+
this._chartjsLoadedCallback = v;
40+
this._callChartjsloadedCallback();
41+
}
42+
3143
_canvasOnchange;
3244
@api
3345
get canvasOnchange() {
@@ -327,6 +339,7 @@ export default class Chart extends LightningElement {
327339
loadScript(this, ChartJS).then(
328340
() => {
329341
this._chartjsLoaded = true;
342+
this._callChartjsloadedCallback();
330343
this._reactivityManager.throttleRegisteredJob();
331344
},
332345
reason => {
@@ -390,6 +403,15 @@ export default class Chart extends LightningElement {
390403
);
391404
}
392405

406+
_callChartjsloadedCallback() {
407+
if (
408+
this._chartjsLoaded === true &&
409+
typeof this._chartjsLoadedCallback === 'function'
410+
) {
411+
this._chartjsLoadedCallback();
412+
}
413+
}
414+
393415
_listenerHandlers = {
394416
// store option and throttle a drawChart
395417
handleOption: evt => {

0 commit comments

Comments
 (0)