Skip to content

Commit 0176724

Browse files
committed
test: move tests next to the sources and check types with tsc
Specs live beside the code they test, and tsd type tests become @ts-expect-error cases in them, checked by tsc and by Vitest typecheck.
1 parent d927473 commit 0176724

6 files changed

Lines changed: 107 additions & 68 deletions

File tree

‎.eslintrc.json‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"parser": "@typescript-eslint/parser",
3-
"plugins": ["@typescript-eslint", "prettier"],
3+
"plugins": [
4+
"@typescript-eslint",
5+
"prettier"
6+
],
47
"extends": [
58
"standard",
69
"standard-react",
@@ -22,11 +25,21 @@
2225
}
2326
},
2427
"rules": {
25-
"curly": [2, "multi-line"],
26-
"quotes": [2, "single", "avoid-escape"],
28+
"curly": [
29+
2,
30+
"multi-line"
31+
],
32+
"quotes": [
33+
2,
34+
"single",
35+
"avoid-escape"
36+
],
2737
"react/display-name": 0,
2838
"react/jsx-boolean-value": 1,
29-
"jsx-quotes": [1, "prefer-single"],
39+
"jsx-quotes": [
40+
1,
41+
"prefer-single"
42+
],
3043
"react/jsx-no-undef": 1,
3144
"react/jsx-sort-props": 0,
3245
"react/jsx-uses-react": 1,
@@ -37,11 +50,24 @@
3750
"react/prop-types": 0,
3851
"react/react-in-jsx-scope": 0,
3952
"react/self-closing-comp": 1,
40-
"semi": [2, "always"],
53+
"semi": [
54+
2,
55+
"always"
56+
],
4157
"strict": 0,
4258
"no-use-before-define": 0,
4359
"@typescript-eslint/no-use-before-define": 2,
4460
"@typescript-eslint/consistent-type-imports": "error",
4561
"@typescript-eslint/no-import-type-side-effects": "error"
46-
}
62+
},
63+
"overrides": [
64+
{
65+
"files": [
66+
"**/*.spec.tsx"
67+
],
68+
"rules": {
69+
"@typescript-eslint/no-explicit-any": "off"
70+
}
71+
}
72+
]
4773
}

‎.github/workflows/tests.yml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ jobs:
2525
cache: 'pnpm'
2626
- name: Install dependencies
2727
run: pnpm install
28-
- name: Build package
29-
run: pnpm build
3028
- name: Check types
31-
run: pnpm test:typings
29+
run: pnpm test:types
3230
lint:
3331
runs-on: ubuntu-latest
3432
name: lint
@@ -47,7 +45,7 @@ jobs:
4745
- name: Install dependencies
4846
run: pnpm install
4947
- name: Lint files
50-
run: pnpm test:lint
48+
run: pnpm lint
5149
unit:
5250
runs-on: ubuntu-latest
5351
name: unit
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { vi, beforeEach, afterEach, describe, it, expect } from 'vitest';
22
import { render, cleanup, fireEvent } from '@testing-library/react';
33
import 'chart.js/auto';
44
import { Chart as ChartJS } from 'chart.js';
5-
import { Chart } from '../src/index.js';
5+
import type { Plugin } from 'chart.js';
6+
import { Chart } from './chart.js';
67

7-
describe('<Chart />', () => {
8+
describe('Chart', () => {
89
const data = {
910
labels: ['red', 'blue'],
1011
datasets: [{ label: 'colors', data: [1, 2] }],
@@ -272,7 +273,7 @@ describe('<Chart />', () => {
272273
expect(chart).toBe(null);
273274
});
274275

275-
it('should add className ', () => {
276+
it('should add className', () => {
276277
render(
277278
<Chart
278279
data={data}
@@ -430,4 +431,32 @@ describe('<Chart />', () => {
430431
expect(update).toHaveBeenCalledTimes(1);
431432
expect(update).toBeCalledWith('active');
432433
});
434+
435+
it('should accept plugins of its chart type', () => {
436+
const element = (
437+
<Chart type='radar' data={data} plugins={[] as Plugin<'radar'>[]} />
438+
);
439+
440+
expect(element.props.plugins).toEqual([]);
441+
});
442+
443+
it('should accept datasets of different chart types', () => {
444+
const multiTypeData = {
445+
datasets: [
446+
{
447+
type: 'line' as const,
448+
label: 'Dataset 1',
449+
data: [],
450+
},
451+
{
452+
type: 'bar' as const,
453+
label: 'Dataset 2',
454+
data: [],
455+
},
456+
],
457+
};
458+
const element = <Chart type='bar' data={multiTypeData} />;
459+
460+
expect(element.props.data).toBe(multiTypeData);
461+
});
433462
});

‎src/typedCharts.spec.tsx‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, it, expect } from 'vitest';
2+
import type { Plugin } from 'chart.js';
3+
import { Doughnut, Scatter } from './typedCharts.js';
4+
5+
describe('typedCharts', () => {
6+
const data = {
7+
datasets: [],
8+
};
9+
10+
describe('Scatter', () => {
11+
it('should accept plugins of its chart type', () => {
12+
const element = (
13+
<Scatter data={data} plugins={[] as Plugin<'scatter'>[]} />
14+
);
15+
16+
expect(element.props.plugins).toEqual([]);
17+
});
18+
19+
it('should reject plugins of another chart type', () => {
20+
// @ts-expect-error Plugins of a bar chart do not fit a scatter chart
21+
const element = <Scatter data={data} plugins={[] as Plugin<'bar'>[]} />;
22+
23+
expect(element.props.plugins).toEqual([]);
24+
});
25+
26+
it('should reject options of another chart type', () => {
27+
// @ts-expect-error `cutout` is an option of doughnut and pie charts
28+
const element = <Scatter data={data} options={{ cutout: '75%' }} />;
29+
30+
expect(element.props.options).toEqual({ cutout: '75%' });
31+
});
32+
});
33+
34+
describe('Doughnut', () => {
35+
it('should accept options of its chart type', () => {
36+
const element = <Doughnut data={data} options={{ cutout: '75%' }} />;
37+
38+
expect(element.props.options).toEqual({ cutout: '75%' });
39+
});
40+
});
41+
});

‎test/chart.test-d.tsx‎

Lines changed: 0 additions & 52 deletions
This file was deleted.

‎test/setup.ts‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)