-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Adds jest-axe and Card component test (#2440)
## What's the purpose of this pull request? - Setup `@testing-library/jest-dom` to help to test our components when checking an element's attributes, its text content, its css classes... - Adds `Card` component tests - Introduces accessibility tests: This won't guarantee that what we're building is accessible, but it's a first step towards it. 🥳 - Updates `CheckboxField` component test TODO: Document ## How it works? Currently, our components library do not have tests, but we want to introduce them for upcoming components. For existing components, we will gradually add tests retroactively. ## How to test it? 1. navigate to `packages/components` -> run `yarn install` 2. run `yarn test`, you will be able to see the tests running <img width="873" alt="image" src="https://github.com/user-attachments/assets/1e4c0fcc-4ded-45c5-a078-0fad99c52dba"> _____ 3. removing `<Label>` component from `CheckboxField`, when running the step 2, you will get: <img width="1005" alt="image" src="https://github.com/user-attachments/assets/898360ba-2a9a-40d5-afd4-a6c0f74d11a4"> ## References https://testing-library.com/docs/ecosystem-jest-dom testing-library/jest-dom#546 NickColley/jest-axe#79
- Loading branch information
1 parent
ea14927
commit cd93d30
Showing
8 changed files
with
2,735 additions
and
3,003 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
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
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,3 @@ | ||
import '@testing-library/jest-dom'; | ||
import '@testing-library/jest-dom/jest-globals'; | ||
|
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
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,55 @@ | ||
import { describe, expect, it } from '@jest/globals' | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import { axe, toHaveNoViolations } from 'jest-axe' | ||
|
||
import React from 'react' | ||
|
||
import { Card } from '../../../src/index' | ||
|
||
expect.extend(toHaveNoViolations) | ||
|
||
describe('Card', () => { | ||
const mockIconAction = jest.fn() | ||
it('renders with title', () => { | ||
render(<Card title="Test Card" />) | ||
|
||
const cardTitle = screen.getByText('Test Card') | ||
expect(cardTitle).toBeInTheDocument() | ||
}) | ||
|
||
it('renders with custom max width', () => { | ||
render(<Card title="Test Card" maxWidth="500px" />) | ||
|
||
const card = screen.getByTestId('fs-card') | ||
expect(card).toHaveStyle('max-width: 500px') | ||
}) | ||
|
||
it('renders with icon', () => { | ||
render(<Card title="Test Card" iconName="star" />) | ||
|
||
const iconButton = screen.getByLabelText( | ||
'Test Card action' | ||
) as HTMLButtonElement | ||
expect(iconButton).toBeInTheDocument() | ||
}) | ||
|
||
it('executes the icon action when clicked', () => { | ||
render( | ||
<Card title="Test Card" iconName="star" iconAction={mockIconAction} /> | ||
) | ||
|
||
const iconButton = screen.getByLabelText( | ||
'Test Card action' | ||
) as HTMLButtonElement | ||
iconButton.click() | ||
expect(mockIconAction).toHaveBeenCalled() | ||
}) | ||
|
||
it('should not fail any accessibility tests', async () => { | ||
const { container } = render( | ||
<Card title="Test Card" iconName="star" iconAction={mockIconAction} /> | ||
) | ||
expect(await axe(container)).toHaveNoViolations() | ||
}) | ||
}) |
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
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,14 +1,15 @@ | ||
{ | ||
"extends": "@faststore/shared/tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
"outDir": "dist", | ||
"types": ["@testing-library/jest-dom/jest-globals"] | ||
}, | ||
"include": ["src"], | ||
"exclude": [ | ||
"src/**/*.test.ts", | ||
"src/**/*.test.tsx", | ||
"src/**/*.stories.ts", | ||
"src/**/*.stories.tsx", | ||
"src/typings/utils.d.ts", | ||
"src/typings/utils.d.ts" | ||
] | ||
} |
Oops, something went wrong.