-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
948ef59
commit 3afa6ef
Showing
7 changed files
with
85 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// jest.setup.ts | ||
import '@testing-library/jest-dom'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/react/src/components/ExampleButton/__tests__/ExampleButton-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
12 changes: 7 additions & 5 deletions
12
packages/react/src/components/ExampleButton/components/ExampleButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters