Skip to content

Commit badf309

Browse files
committed
bug #17 Fix Chartjs options support (tgalopin)
This PR was merged into the main branch. Discussion ---------- Fix Chartjs options support | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | Fix #11 | License | MIT Fix Chart.js options handling and add better tests to ensure no regression there. Commits ------- 73ba629 Fix Chartjs options support
2 parents 67577a4 + 73ba629 commit badf309

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

src/Chartjs/Resources/assets/dist/controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var _default = /*#__PURE__*/function (_Controller) {
5555
value: function connect() {
5656
var payload = JSON.parse(this.element.getAttribute('data-view'));
5757

58-
if (!payload.options.length) {
58+
if (Array.isArray(payload.options) && 0 === payload.options.length) {
5959
payload.options = {};
6060
}
6161

src/Chartjs/Resources/assets/src/controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Chart } from 'chart.js';
1515
export default class extends Controller {
1616
connect() {
1717
const payload = JSON.parse(this.element.getAttribute('data-view'));
18-
if (!payload.options.length) {
18+
if (Array.isArray(payload.options) && 0 === payload.options.length) {
1919
payload.options = {};
2020
}
2121

src/Chartjs/Resources/assets/test/controller.test.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import ChartjsController from '../dist/controller';
1717
// Controller used to check the actual controller was properly booted
1818
class CheckController extends Controller {
1919
connect() {
20-
this.element.addEventListener('chartjs:connect', () => {
20+
this.element.addEventListener('chartjs:connect', (event) => {
2121
this.element.classList.add('connected');
22+
this.element.chart = event.detail.chart;
2223
});
2324
}
2425
}
@@ -32,24 +33,43 @@ const startStimulus = () => {
3233
describe('ChartjsController', () => {
3334
let container;
3435

35-
beforeEach(() => {
36+
afterEach(() => {
37+
clearDOM();
38+
});
39+
40+
it('connect without options', async () => {
3641
container = mountDOM(`
3742
<canvas
3843
data-testid="canvas"
3944
data-controller="check chartjs"
4045
data-view="&#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;"
4146
></canvas>
4247
`);
43-
});
4448

45-
afterEach(() => {
46-
clearDOM();
49+
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
50+
51+
startStimulus();
52+
await waitFor(() => expect(getByTestId(container, 'canvas')).toHaveClass('connected'));
53+
54+
const chart = getByTestId(container, 'canvas').chart;
55+
expect(chart.options.showLines).toBe(true);
4756
});
4857

49-
it('connect', async () => {
58+
it('connect with options', async () => {
59+
container = mountDOM(`
60+
<canvas
61+
data-testid="canvas"
62+
data-controller="check chartjs"
63+
data-view="&#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;"
64+
></canvas>
65+
`);
66+
5067
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
5168

5269
startStimulus();
5370
await waitFor(() => expect(getByTestId(container, 'canvas')).toHaveClass('connected'));
71+
72+
const chart = getByTestId(container, 'canvas').chart;
73+
expect(chart.options.showLines).toBe(false);
5474
});
5575
});

src/Chartjs/Tests/Twig/ChartExtensionTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function testRenderChart()
3232
$builder = $kernel->getContainer()->get('test.chartjs.builder');
3333

3434
$chart = $builder->createChart(Chart::TYPE_LINE);
35+
3536
$chart->setData([
3637
'labels' => ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
3738
'datasets' => [
@@ -44,6 +45,10 @@ public function testRenderChart()
4445
],
4546
]);
4647

48+
$chart->setOptions([
49+
'showLines' => false,
50+
]);
51+
4752
$rendered = $kernel->getContainer()->get('test.chartjs.twig_extension')->renderChart(
4853
$kernel->getContainer()->get('twig'),
4954
$chart,
@@ -53,7 +58,7 @@ public function testRenderChart()
5358
$this->assertSame(
5459
'<canvas
5560
data-controller="mycontroller @symfony/ux-chartjs/chart"
56-
data-view="&#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;"
61+
data-view="&#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;"
5762
class="myclass"></canvas>',
5863
$rendered
5964
);

0 commit comments

Comments
 (0)