Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart Library: Add initial testing infrastructure and PieChart component tests #41148

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions projects/js-packages/charts/changelog/add-chart-library-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Charts: adds dependencies and config for jest testing. Adds some initial tests to pie chart component
14 changes: 14 additions & 0 deletions projects/js-packages/charts/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference types="jest" />

declare global {
const describe: jest.Describe;
const test: jest.It;
const expect: jest.Expect;
const it: jest.It;
const beforeAll: jest.Lifecycle;
const afterAll: jest.Lifecycle;
const beforeEach: jest.Lifecycle;
const afterEach: jest.Lifecycle;
}

export {};
12 changes: 11 additions & 1 deletion projects/js-packages/charts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,17 @@
"tsconfig-paths-webpack-plugin": "4.2.0",
"typescript": "5.7.2",
"webpack": "^5.88.0",
"webpack-cli": "^5.1.0"
"webpack-cli": "^5.1.0",
"@testing-library/react": "^14.0.0",
"@testing-library/jest-dom": "^6.0.0",
"@types/jest": "^29.0.0",
"@types/testing-library__jest-dom": "^5.14.9",
"@jest/globals": "^29.0.0",
"babel-jest": "^29.7.0",
"@automattic/jetpack-webpack-config": "workspace:*",
"@wordpress/babel-plugin-import-jsx-pragma": "5.14.0",
"@wordpress/element": "6.14.0",
"@babel/plugin-transform-react-jsx": "7.25.9"
},
"peerDependencies": {
"react": "^17.0.0 || ^18.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @jest-environment jsdom
*/

import { render, screen } from '@testing-library/react';
import { ThemeProvider } from '../../../providers/theme';
import PieChart from '../pie-chart';

describe( 'PieChart', () => {
const defaultProps = {
size: 500,
data: [
{ label: 'A', percentage: 50, value: 50 },
{ label: 'B', percentage: 50, value: 50 },
],
};

const renderWithTheme = ( props = {} ) => {
return render(
<ThemeProvider>
<PieChart { ...defaultProps } { ...props } />
</ThemeProvider>
);
};

describe( 'Data Validation', () => {
test( 'validates total percentage equals 100', () => {
renderWithTheme( {
data: [
{ label: 'A', percentage: 60, value: 60 },
{ label: 'B', percentage: 50, value: 50 },
],
} );
expect( screen.getByText( /invalid percentage total/i ) ).toBeInTheDocument();
} );

test( 'handles negative values', () => {
renderWithTheme( {
data: [
{ label: 'A', percentage: -30, value: -30 },
{ label: 'B', percentage: 130, value: 130 },
],
} );
expect( screen.getByText( /invalid data/i ) ).toBeInTheDocument();
} );

test( 'handles empty data array', () => {
renderWithTheme( { data: [] } );
expect( screen.getByText( /no data available/i ) ).toBeInTheDocument();
} );

test( 'handles single data point', () => {
renderWithTheme( {
data: [ { label: 'A', percentage: 100, value: 100 } ],
} );
expect( screen.getByText( 'A' ) ).toBeInTheDocument();
} );
} );
} );
12 changes: 12 additions & 0 deletions projects/js-packages/charts/tests/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ const baseConfig = require( 'jetpack-js-tools/jest/config.base.js' );
module.exports = {
...baseConfig,
rootDir: path.join( __dirname, '..' ),
testEnvironment: 'jsdom',
setupFilesAfterEnv: [
...baseConfig.setupFilesAfterEnv,
'@testing-library/jest-dom',
'<rootDir>/tests/jest.setup.js',
],
transform: {
...baseConfig.transform,
'\\.[jt]sx?$': require( 'jetpack-js-tools/jest/babel-jest-config-factory.js' )(
require.resolve
),
},
};
13 changes: 13 additions & 0 deletions projects/js-packages/charts/tests/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ResizeObserver {
observe() {
// do nothing
}
unobserve() {
// do nothing
}
disconnect() {
// do nothing
}
}

global.ResizeObserver = ResizeObserver;
8 changes: 4 additions & 4 deletions projects/js-packages/charts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"extends": "jetpack-js-tools/tsconfig.tsc-declaration-only.json",
"compilerOptions": {
"typeRoots": [ "./node_modules/@types/", "src/*" ],
"typeRoots": [ "./node_modules/@types/" ],
"outDir": "./dist/types"
},
// List all sources and source-containing subdirs.
"include": [ "./src/index.ts" ],
"exclude": [ "node_modules", "dist", "**/stories/**" ]
"include": [ "./src/**/*.ts", "./src/**/*.tsx" ],
"exclude": [ "node_modules", "dist", "**/stories/**" ],
"files": [ "./global.d.ts" ]
}
Loading