Skip to content

Commit 444b7ea

Browse files
committed
Fixing chartjs test and rollup process
1 parent 38bd8c2 commit 444b7ea

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

rollup.config.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@ import glob from 'glob';
44
import path from 'path';
55
import pkgUp from 'pkg-up';
66

7+
/**
8+
* Guarantees that any files imported from a peer dependency are treated as an external.
9+
*
10+
* For example, if we import `chart.js/auto`, that would not normally
11+
* match the "chart.js" we pass to the "externals" config. This plugin
12+
* catches that case and adds it as an external.
13+
*
14+
* Inspired by https://github.com/oat-sa/rollup-plugin-wildcard-external
15+
*/
16+
const wildcardExternalsPlugin = (peerDependencies) => ({
17+
name: 'wildcard-externals',
18+
resolveId(source, importer) {
19+
if (importer) {
20+
let matchesExternal = false;
21+
peerDependencies.forEach((peerDependency) => {
22+
if (source.includes(`/${peerDependency}/`)) {
23+
matchesExternal = true;
24+
}
25+
});
26+
27+
if (matchesExternal) {
28+
return {
29+
id: source,
30+
external: true,
31+
moduleSideEffects: true
32+
};
33+
}
34+
}
35+
36+
return null; // other ids should be handled as usually
37+
}
38+
});
39+
740
const files = glob.sync("src/**/assets/src/*controller.ts");
841
const packages = files.map((file) => {
942
const absolutePath = path.join(__dirname, file);
@@ -23,6 +56,7 @@ const packages = files.map((file) => {
2356
plugins: [
2457
resolve(),
2558
typescript(),
59+
wildcardExternalsPlugin(peerDependencies)
2660
],
2761
};
2862
});

src/Chartjs/Resources/assets/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@hotwired/stimulus": "^3.0.0",
2222
"@types/chart.js": "^2.9.34",
2323
"chart.js": "^3.4.1",
24-
"jest-canvas-mock": "^2.3.0"
24+
"jest-canvas-mock": "^2.3.0",
25+
"resize-observer-polyfill": "^1.5.1"
2526
}
2627
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,20 @@ const startStimulus = () => {
3232
const application = Application.start();
3333
application.register('check', CheckController);
3434
application.register('chartjs', ChartjsController);
35+
36+
return application;
3537
};
3638

3739
describe('ChartjsController', () => {
38-
let container;
40+
let application;
3941

4042
afterEach(() => {
4143
clearDOM();
44+
application.stop();
4245
});
4346

4447
it('connect without options', async () => {
45-
container = mountDOM(`
48+
const container = mountDOM(`
4649
<canvas
4750
data-testid="canvas"
4851
data-controller="check chartjs"
@@ -53,18 +56,19 @@ describe('ChartjsController', () => {
5356
expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected');
5457
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
5558

56-
startStimulus();
59+
application = startStimulus();
60+
5761
await waitFor(() => {
5862
expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected');
5963
expect(getByTestId(container, 'canvas')).toHaveClass('connected');
6064
});
6165

6266
const chart = getByTestId(container, 'canvas').chart;
63-
expect(chart.options.showLines).toBe(true);
67+
expect(chart.options.showLines).toBeUndefined();
6468
});
6569

6670
it('connect with options', async () => {
67-
container = mountDOM(`
71+
const container = mountDOM(`
6872
<canvas
6973
data-testid="canvas"
7074
data-controller="check chartjs"
@@ -75,7 +79,7 @@ describe('ChartjsController', () => {
7579
expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected');
7680
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
7781

78-
startStimulus();
82+
application = startStimulus();
7983
await waitFor(() => {
8084
expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected');
8185
expect(getByTestId(container, 'canvas')).toHaveClass('connected');

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
'use strict';
1111

1212
import 'jest-canvas-mock';
13+
// eslint-disable-next-line
14+
global.ResizeObserver = require('resize-observer-polyfill');

0 commit comments

Comments
 (0)