-
-
Notifications
You must be signed in to change notification settings - Fork 321
Feature/test sortby component 1915 #1950
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
Merged
kasya
merged 8 commits into
OWASP:main
from
Dhirajsharma2060:feature/test-sortby-component-1915
Aug 3, 2025
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4d339f7
test: add unit tests for SortBy component
Dhirajsharma2060 873742c
test: update SortBy component tests for prop naming and accessibility
Dhirajsharma2060 ab51c73
test: import React in SortBy component tests for consistency
Dhirajsharma2060 9b5197d
made some chnages
Dhirajsharma2060 a247251
test: update SortBy component tests for improved accessibility and clβ¦
Dhirajsharma2060 1da6630
Merge branch 'main' into feature/test-sortby-component-1915
Dhirajsharma2060 c1de740
test: ensure order toggle button functionality in SortBy component tests
Dhirajsharma2060 aa7143a
Merge branch 'main' into feature/test-sortby-component-1915
kasya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,94 @@ | ||
| import { render, screen, fireEvent } from '@testing-library/react' | ||
| import { act } from 'react' | ||
| import SortBy from 'components/SortBy' | ||
|
|
||
| describe('<SortBy />', () => { | ||
| const defaultProps = { | ||
| sortOptions: [ | ||
| { label: 'Name', key: 'name' }, | ||
| { label: 'Date', key: 'date' }, | ||
| ], | ||
| selectedSortOption: 'name', | ||
| onSortChange: jest.fn(), | ||
| selectedOrder: 'asc', | ||
| onOrderChange: jest.fn(), | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| it('renders successfully with minimal required props', async () => { | ||
| await act(async () => { | ||
| render(<SortBy {...defaultProps} />) | ||
| }) | ||
| const select = screen.getByLabelText('Sort By :') | ||
| expect(select).toBeInTheDocument() | ||
| expect(select).toHaveValue('name') | ||
| }) | ||
|
|
||
| it('renders all options and selects the correct one', async () => { | ||
| await act(async () => { | ||
| render(<SortBy {...defaultProps} />) | ||
| }) | ||
| const select = screen.getByLabelText('Sort By :') | ||
| expect(select).toHaveValue('name') | ||
| // The dropdown options aren't directly visible in the DOM | ||
| // We're checking for the selected value instead | ||
| const selectedOption = screen.getByText('Name', { selector: '[data-slot="value"]' }) | ||
| expect(selectedOption).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('calls onSortChange when a different option is selected', async () => { | ||
| await act(async () => { | ||
| render(<SortBy {...defaultProps} />) | ||
| }) | ||
| await act(async () => { | ||
| fireEvent.change(screen.getByLabelText('Sort By :'), { target: { value: 'date' } }) | ||
| }) | ||
| expect(defaultProps.onSortChange).toHaveBeenCalledWith('date') | ||
| }) | ||
|
|
||
| it('renders ascending icon and tooltip when order is "asc"', async () => { | ||
| await act(async () => { | ||
| render(<SortBy {...defaultProps} selectedOrder="asc" />) | ||
| }) | ||
| // Look for the icon that indicates ascending order | ||
| const sortIcon = screen.getByRole('img', { hidden: true }) | ||
| expect(sortIcon.classList.contains('fa-arrow-up-wide-short')).toBe(true) | ||
| }) | ||
|
|
||
| it('renders descending icon and tooltip when order is "desc"', async () => { | ||
| await act(async () => { | ||
| render(<SortBy {...defaultProps} selectedOrder="desc" />) | ||
| }) | ||
| // Look for the icon that indicates descending order | ||
| const sortIcon = screen.getByRole('img', { hidden: true }) | ||
| expect(sortIcon.classList.contains('fa-arrow-down-wide-short')).toBe(true) | ||
| }) | ||
|
|
||
| it('toggles order when the button is clicked', async () => { | ||
| await act(async () => { | ||
| render(<SortBy {...defaultProps} selectedOrder="asc" />) | ||
| }) | ||
| await act(async () => { | ||
| // Get the second button (the sort order button) | ||
| const buttons = screen.getAllByRole('button') | ||
| fireEvent.click(buttons[1]) // The sort order button is the second button | ||
| }) | ||
| expect(defaultProps.onOrderChange).toHaveBeenCalledWith('desc') | ||
| }) | ||
|
|
||
| it('uses proper accessibility attributes', async () => { | ||
| await act(async () => { | ||
| render(<SortBy {...defaultProps} />) | ||
| }) | ||
| const select = screen.getByLabelText('Sort By :') | ||
| expect(select.tagName).toBe('SELECT') | ||
|
|
||
| // Use getAllByText to handle multiple elements with same text | ||
| const containers = screen.getAllByText('Sort By :') | ||
| const container = containers[0].closest('div') | ||
| expect(container).toBeInTheDocument() | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.