-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(PC-33528)[PRO] feat: add test to the new component
- Loading branch information
1 parent
c66bf1a
commit 90334f3
Showing
6 changed files
with
246 additions
and
36 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
132 changes: 132 additions & 0 deletions
132
pro/src/ui-kit/MultiSelect/__specs__/MultiSelect.spec.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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react' | ||
import { userEvent } from '@testing-library/user-event' | ||
import { axe } from 'vitest-axe' | ||
|
||
import { MultiSelect, Option } from '../MultiSelect' | ||
|
||
describe('<MultiSelect />', () => { | ||
const options: Option[] = [ | ||
{ id: '1', label: 'Option 1' }, | ||
{ id: '2', label: 'Option 2' }, | ||
{ id: '3', label: 'Option 3' }, | ||
] | ||
|
||
const defaultOptions: Option[] = [{ id: '1', label: 'Option 1' }] | ||
|
||
it('should render correctly', async () => { | ||
render( | ||
<MultiSelect | ||
options={options} | ||
label="Select Options" | ||
legend="Legend" | ||
defaultOptions={defaultOptions} | ||
/> | ||
) | ||
|
||
expect(await screen.findByText('Select Options')).toBeInTheDocument() | ||
}) | ||
|
||
it('should not have accessibility violations', async () => { | ||
const { container } = render( | ||
<MultiSelect | ||
options={options} | ||
label="Select Options" | ||
legend="Legend" | ||
defaultOptions={defaultOptions} | ||
/> | ||
) | ||
|
||
expect(await axe(container)).toHaveNoViolations() | ||
}) | ||
|
||
it('renders the MultiSelect component with the correct initial selected options', () => { | ||
render( | ||
<MultiSelect | ||
options={options} | ||
label="Select Options" | ||
legend="Legend" | ||
defaultOptions={defaultOptions} | ||
/> | ||
) | ||
// Check that the initial selected options are rendered in the SelectedValuesTags | ||
expect(screen.getByText('Option 1')).toBeInTheDocument() | ||
}) | ||
|
||
it('toggles the dropdown when the trigger is clicked', async () => { | ||
render( | ||
<MultiSelect | ||
options={options} | ||
label="Select Options" | ||
legend="Legend" | ||
hasSelectAllOptions={true} | ||
/> | ||
) | ||
const toggleButton = screen.getByText('Select Options') | ||
await userEvent.click(toggleButton) // Open the dropdown | ||
expect(screen.getByText(/Tout sélectionner/i)).toBeInTheDocument() | ||
|
||
await userEvent.click(toggleButton) // Close the dropdown | ||
expect(screen.queryByText(/Tout sélectionner/i)).not.toBeInTheDocument() | ||
}) | ||
|
||
it('selects all options when "Select All" is clicked', async () => { | ||
render( | ||
<MultiSelect | ||
options={options} | ||
label="Select Options" | ||
legend="Legend" | ||
hasSelectAllOptions={true} | ||
/> | ||
) | ||
|
||
const toggleButton = screen.getByText('Select Options') | ||
await userEvent.click(toggleButton) // Open the dropdown | ||
|
||
const selectAllCheckbox = screen.getByLabelText(/Tout sélectionner/i) | ||
fireEvent.click(selectAllCheckbox) // Select all options | ||
|
||
// Verify that all options are selected | ||
options.forEach((option) => { | ||
expect(screen.getByLabelText(option.label)).toBeChecked() | ||
}) | ||
}) | ||
|
||
it('removes an option from the selected items when clicked in SelectedValuesTags', async () => { | ||
render( | ||
<MultiSelect | ||
options={options} | ||
label="Select Options" | ||
legend="Legend" | ||
defaultOptions={defaultOptions} | ||
/> | ||
) | ||
|
||
// Initially, Option 1 is selected | ||
const selectedTag = screen.getByText('Option 1') | ||
await userEvent.click(selectedTag) // Remove Option 1 | ||
expect(screen.queryByText('Option 1')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('closes the dropdown when clicked outside or when Escape key is pressed', async () => { | ||
render( | ||
<MultiSelect options={options} label="Select Options" legend="Legend" /> | ||
) | ||
|
||
const toggleButton = screen.getByText('Select Options') | ||
|
||
fireEvent.click(toggleButton) // Open the dropdown | ||
expect(screen.queryByText('Option 1')).toBeInTheDocument() | ||
|
||
// Simulate a click outside the dropdown | ||
fireEvent.click(document.body) | ||
await waitFor(() => | ||
expect(screen.queryByText('Option 1')).not.toBeInTheDocument() | ||
) | ||
|
||
fireEvent.click(toggleButton) // Open the dropdown again | ||
fireEvent.keyDown(toggleButton, { key: 'Escape' }) // Close with Escape key | ||
await waitFor(() => | ||
expect(screen.queryByText('Option 1')).not.toBeInTheDocument() | ||
) | ||
}) | ||
}) |
99 changes: 99 additions & 0 deletions
99
pro/src/ui-kit/MultiSelect/__specs__/MultiSelectPanel.spec.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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import { axe } from 'vitest-axe' | ||
|
||
import { Option } from '../MultiSelect' | ||
import { MultiSelectPanel } from '../MultiSelectPanel' | ||
|
||
describe('<MultiSelectPanel />', () => { | ||
const options: (Option & { checked: boolean })[] = [ | ||
{ id: '1', label: 'Option 1', checked: false }, | ||
{ id: '2', label: 'Option 2', checked: false }, | ||
{ id: '3', label: 'Option 3', checked: false }, | ||
] | ||
|
||
const defaultOptions: Option[] = [{ id: '1', label: 'Option 1' }] | ||
|
||
const onOptionSelect = vi.fn() | ||
|
||
it('renders options with checkboxes', () => { | ||
render( | ||
<MultiSelectPanel | ||
options={options} | ||
label={''} | ||
onOptionSelect={function (option: Option | 'all' | undefined): void { | ||
throw new Error('Function not implemented.') | ||
}} | ||
/> | ||
) | ||
|
||
expect(screen.getByLabelText('Option 1')).toBeInTheDocument() | ||
expect(screen.getByLabelText('Option 2')).toBeInTheDocument() | ||
expect(screen.getByLabelText('Option 3')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders the search input if hasSearch is true', () => { | ||
render( | ||
<MultiSelectPanel | ||
options={[]} | ||
label={''} | ||
onOptionSelect={function (option: Option | 'all' | undefined): void { | ||
throw new Error('Function not implemented.') | ||
}} | ||
hasSearch={true} | ||
searchExample="Exemple: Nantes" | ||
/> | ||
) | ||
|
||
expect(screen.getByText(/Exemple: Nantes/i)).toBeInTheDocument() | ||
}) | ||
|
||
it('not renders the search input if hasSearch is false', () => { | ||
render( | ||
<MultiSelectPanel | ||
options={[]} | ||
label={''} | ||
onOptionSelect={function (option: Option | 'all' | undefined): void { | ||
throw new Error('Function not implemented.') | ||
}} | ||
hasSearch={false} | ||
searchExample="Exemple: Nantes" | ||
/> | ||
) | ||
|
||
expect(screen.queryByText(/Exemple: Nantes/i)).not.toBeInTheDocument() | ||
}) | ||
|
||
it('should not have accessibility violations', async () => { | ||
const { container } = render( | ||
<MultiSelectPanel | ||
options={[]} | ||
label={''} | ||
onOptionSelect={function (option: Option | 'all' | undefined): void { | ||
throw new Error('Function not implemented.') | ||
}} | ||
hasSearch={false} | ||
/> | ||
) | ||
|
||
expect(await axe(container)).toHaveNoViolations() | ||
}) | ||
|
||
it('selects and deselects individual options', async () => { | ||
render( | ||
<MultiSelectPanel | ||
options={options} | ||
label="" | ||
onOptionSelect={onOptionSelect} | ||
/> | ||
) | ||
|
||
// Initially, only Option 1 is selected | ||
const option2Checkbox = screen.getByLabelText(/Option 2/i) | ||
await userEvent.click(option2Checkbox) // Select Option 2 | ||
expect(onOptionSelect).toHaveBeenCalledWith(options[1]) | ||
|
||
await userEvent.click(option2Checkbox) // Deselect Option 2 | ||
expect(onOptionSelect).toHaveBeenCalledWith(options[1]) | ||
}) | ||
}) |