Skip to content

Commit f86ba3b

Browse files
Create test controller per version (WIP)
1 parent 42fb419 commit f86ba3b

File tree

3 files changed

+107
-16
lines changed

3 files changed

+107
-16
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* This file is part of the Symfony package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
import { Controller } from '@hotwired/stimulus'
13+
14+
// Controller used to check the actual controller was properly booted
15+
export class CheckController extends Controller {
16+
connect() {
17+
this.element.addEventListener('chartjs:pre-connect', () => {
18+
this.element.classList.add('pre-connected');
19+
});
20+
21+
this.element.addEventListener('chartjs:connect', (event) => {
22+
this.element.classList.add('connected');
23+
this.element.chart = event.detail.chart;
24+
});
25+
}
26+
}

src/Chartjs/assets/test/controller.test.ts renamed to src/Chartjs/assets/test/chart_v3_controller.test.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,11 @@
99

1010
'use strict';
1111

12-
import { Application, Controller } from '@hotwired/stimulus';
12+
import { Application } from '@hotwired/stimulus';
13+
import { CheckController } from './chart_check_controller';
1314
import { getByTestId, waitFor } from '@testing-library/dom';
1415
import { clearDOM, mountDOM } from '@symfony/stimulus-testing';
15-
import ChartjsController from '../src/controller';
16-
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-
});
23-
24-
this.element.addEventListener('chartjs:connect', (event) => {
25-
this.element.classList.add('connected');
26-
this.element.chart = event.detail.chart;
27-
});
28-
}
29-
}
16+
import ChartjsController from '../src/chart_v3_controller';
3017

3118
const startStimulus = () => {
3219
const application = Application.start();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* This file is part of the Symfony package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
import { Application } from '@hotwired/stimulus';
13+
import { CheckController } from './chart_check_controller';
14+
import { getByTestId, waitFor } from '@testing-library/dom';
15+
import { clearDOM, mountDOM } from '@symfony/stimulus-testing';
16+
import ChartjsController from '../src/chart_v4_controller';
17+
18+
const startStimulus = () => {
19+
const application = Application.start();
20+
application.register('check', CheckController);
21+
application.register('chartjs', ChartjsController);
22+
23+
return application;
24+
};
25+
26+
describe('ChartjsController', () => {
27+
let application;
28+
29+
afterEach(() => {
30+
clearDOM();
31+
application.stop();
32+
});
33+
34+
it('connect without options', async () => {
35+
const container = mountDOM(`
36+
<canvas
37+
data-testid="canvas"
38+
data-controller="check chartjs"
39+
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;"
40+
></canvas>
41+
`);
42+
43+
expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected');
44+
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
45+
46+
application = startStimulus();
47+
48+
await waitFor(() => {
49+
expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected');
50+
expect(getByTestId(container, 'canvas')).toHaveClass('connected');
51+
});
52+
53+
const chart = getByTestId(container, 'canvas').chart;
54+
expect(chart.options.showLines).toBeUndefined();
55+
});
56+
57+
it('connect with options', async () => {
58+
const container = mountDOM(`
59+
<canvas
60+
data-testid="canvas"
61+
data-controller="check chartjs"
62+
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;"
63+
></canvas>
64+
`);
65+
66+
expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected');
67+
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
68+
69+
application = startStimulus();
70+
await waitFor(() => {
71+
expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected');
72+
expect(getByTestId(container, 'canvas')).toHaveClass('connected');
73+
});
74+
75+
const chart = getByTestId(container, 'canvas').chart;
76+
expect(chart.options.showLines).toBe(false);
77+
});
78+
});

0 commit comments

Comments
 (0)