Skip to content

Commit

Permalink
added jest test suite and ts supp:
Browse files Browse the repository at this point in the history
  • Loading branch information
Gururajj77 committed Dec 6, 2024
1 parent 948ef59 commit 3afa6ef
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/react/__mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
16 changes: 16 additions & 0 deletions packages/react/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
testEnvironment: 'jsdom',
preset: 'ts-jest',
moduleNameMapper: {
'\\.(css|less|scss|sass)$': '<rootDir>/__mocks__/styleMock.js',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
'^.+\\.(js|jsx)$': 'babel-jest',
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],

// Test file patterns to match
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
};
2 changes: 2 additions & 0 deletions packages/react/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// jest.setup.ts
import '@testing-library/jest-dom';
11 changes: 8 additions & 3 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"private": true,
"scripts": {
"storybook": "storybook dev -p 3000",
"build-storybook": "storybook build"
"build-storybook": "storybook build",
"test": "jest"
},
"browserslist": {
"production": [
Expand All @@ -22,19 +23,21 @@
"@carbon/react": "^1.71.1",
"@carbon/styles": "^1.70.0",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@testing-library/react": "^16.1.0",
"@testing-library/user-event": "^14.5.2",
"@types/jest": "^27.5.2",
"@types/node": "^16.18.121",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"jest": "^29.7.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"typescript": "^5.2.2"
},
"devDependencies": {
"@babel/preset-env": "^7.26.0",
"@babel/preset-react": "^7.26.3",
"@babel/preset-typescript": "^7.26.0",
"@chromatic-com/storybook": "^3.2.2",
"@storybook/addon-docs": "^8.4.6",
"@storybook/addon-essentials": "^8.4.6",
Expand All @@ -50,6 +53,7 @@
"@types/prop-types": "^15",
"css-loader": "^7.1.2",
"eslint-plugin-storybook": "^0.11.1",
"jest-environment-jsdom": "^29.7.0",
"mini-css-extract-plugin": "^2.9.2",
"postcss-loader": "^8.1.1",
"prop-types": "^15.8.1",
Expand All @@ -58,6 +62,7 @@
"storybook": "^8.4.6",
"storybook-addon-accessibility-checker": "^3.1.61-rc.3",
"style-loader": "^4.0.0",
"ts-jest": "^29.2.5",
"typescript-config-carbon": "^0.3.0",
"webpack": "^5.97.0"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import '@testing-library/jest-dom';

import { ExampleButton } from '../components/ExampleButton';
jest.mock('./button.scss', () => ({}));
describe('ExampleButton', () => {
describe('renders as expected - Component API', () => {
it('renders a button element', () => {
render(<ExampleButton />);
expect(screen.getByRole('button')).toBeInTheDocument();
});

it('renders with default text "Button"', () => {
render(<ExampleButton />);
expect(screen.getByRole('button')).toHaveTextContent(/^Button$/);
});

it('supports a custom class name', () => {
const { container } = render(<ExampleButton className="test" />);
expect(container.firstChild).toHaveClass('test');
});

it('supports additional props', () => {
render(<ExampleButton data-testid="test-button" />);
expect(screen.getByRole('button')).toHaveAttribute(
'data-testid',
'test-button'
);
});

it('supports disabled state', () => {
render(<ExampleButton disabled />);
expect(screen.getByRole('button')).toBeDisabled();
});
});

describe('behaves as expected', () => {
it('calls onClick handler when clicked', async () => {
const onClick = jest.fn();
render(<ExampleButton onClick={onClick} />);

expect(onClick).toHaveBeenCalledTimes(0);
await userEvent.click(screen.getByRole('button'));
expect(onClick).toHaveBeenCalledTimes(1);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React from 'react';
import { Button } from '@carbon/react';
import React, { PropsWithChildren } from 'react';
import { Button, ButtonProps } from '@carbon/react';
import './button.scss';

/** Primary UI component for user interaction */
export const ExampleButton = () => {
return <Button>Button</Button>;
};

type ExampleButtonProps = PropsWithChildren<ButtonProps<any>>;
export const ExampleButton = ({ children = 'Button', ...rest }: ExampleButtonProps ) => {
return <Button {...rest}>{children}</Button>;
};
3 changes: 2 additions & 1 deletion packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"types": ["jest", "@testing-library/jest-dom"],
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
Expand All @@ -16,5 +17,5 @@
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"]
"include": ["src", "__tests__/**/*"]
}

0 comments on commit 3afa6ef

Please sign in to comment.