Skip to content

Commit 233df73

Browse files
committed
improving chartjs tests
1 parent f8502fb commit 233df73

File tree

1 file changed

+35
-47
lines changed

1 file changed

+35
-47
lines changed

src/Chartjs/assets/test/controller.test.ts

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,71 @@
99

1010
'use strict';
1111

12-
import { Application, Controller } from '@hotwired/stimulus';
13-
import { getByTestId, waitFor } from '@testing-library/dom';
14-
import { clearDOM, mountDOM } from '@symfony/stimulus-testing';
12+
import { Application } from '@hotwired/stimulus';
13+
import { waitFor } from '@testing-library/dom';
1514
import ChartjsController from '../src/controller';
1615

17-
// Controller used to check the actual controller was properly booted
18-
class CheckController extends Controller {
19-
connect() {
20-
this.element.addEventListener('chartjs:pre-connect', () => {
21-
this.element.classList.add('pre-connected');
22-
});
16+
const startChartTest = async (canvasHtml: string): Promise<{ canvas: HTMLCanvasElement, chart: Chart }> => {
17+
let chart: Chart | null = null;
2318

24-
this.element.addEventListener('chartjs:connect', (event) => {
25-
this.element.classList.add('connected');
26-
this.element.chart = event.detail.chart;
27-
});
19+
document.body.addEventListener('chartjs:pre-connect', () => {
20+
document.body.classList.add('pre-connected');
21+
});
22+
23+
document.body.addEventListener('chartjs:connect', (event: any) => {
24+
chart = (event.detail).chart;
25+
document.body.classList.add('connected');
26+
});
27+
28+
document.body.innerHTML = canvasHtml;
29+
const canvasElement = document.querySelector('canvas');
30+
if (!canvasElement) {
31+
throw 'Missing canvas element';
2832
}
29-
}
3033

31-
const startStimulus = () => {
32-
const application = Application.start();
33-
application.register('check', CheckController);
34-
application.register('chartjs', ChartjsController);
34+
await waitFor(() => {
35+
expect(document.body).toHaveClass('pre-connected');
36+
expect(document.body).toHaveClass('connected');
37+
});
38+
39+
if (!chart) {
40+
throw 'Missing TomSelect instance';
41+
}
3542

36-
return application;
37-
};
43+
return { canvas: canvasElement, chart };
44+
}
3845

3946
describe('ChartjsController', () => {
40-
let application;
47+
beforeAll(() => {
48+
const application = Application.start();
49+
application.register('chartjs', ChartjsController);
50+
});
4151

4252
afterEach(() => {
43-
clearDOM();
44-
application.stop();
53+
document.body.innerHTML = '';
4554
});
4655

4756
it('connect without options', async () => {
48-
const container = mountDOM(`
57+
const { chart } = await startChartTest(`
4958
<canvas
5059
data-testid="canvas"
51-
data-controller="check chartjs"
60+
data-controller="chartjs"
5261
data-chartjs-view-value="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x5B;&#x5D;&#x7D;"
5362
></canvas>
5463
`);
5564

56-
expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected');
57-
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
58-
59-
application = startStimulus();
60-
61-
await waitFor(() => {
62-
expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected');
63-
expect(getByTestId(container, 'canvas')).toHaveClass('connected');
64-
});
65-
66-
const chart = getByTestId(container, 'canvas').chart;
6765
expect(chart.options.showLines).toBeUndefined();
6866
});
6967

7068
it('connect with options', async () => {
71-
const container = mountDOM(`
69+
const { chart } = await startChartTest(`
7270
<canvas
7371
data-testid="canvas"
7472
data-controller="check chartjs"
7573
data-chartjs-view-value="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x7B;&quot;showLines&quot;&#x3A;false&#x7D;&#x7D;"
7674
></canvas>
7775
`);
7876

79-
expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected');
80-
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
81-
82-
application = startStimulus();
83-
await waitFor(() => {
84-
expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected');
85-
expect(getByTestId(container, 'canvas')).toHaveClass('connected');
86-
});
87-
88-
const chart = getByTestId(container, 'canvas').chart;
8977
expect(chart.options.showLines).toBe(false);
9078
});
9179
});

0 commit comments

Comments
 (0)