-
Notifications
You must be signed in to change notification settings - Fork 8
feat(website): add Required Segments filter for multi-segmented viruses #6057
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
189 changes: 189 additions & 0 deletions
189
website/src/components/SearchPage/SegmentFilter.spec.tsx
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,189 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { SegmentFilter } from './SegmentFilter.tsx'; | ||
| import type { MetadataFilter } from '../../types/config.ts'; | ||
| import { | ||
| MULTI_SEG_MULTI_REF_REFERENCEGENOMES, | ||
| MULTI_SEG_SINGLE_REF_REFERENCEGENOMES, | ||
| SINGLE_SEG_SINGLE_REF_REFERENCEGENOMES, | ||
| } from '../../types/referenceGenomes.spec.ts'; | ||
| import { MetadataFilterSchema } from '../../utils/search.ts'; | ||
|
|
||
| // Schema with length_From fields for both segments (S and L) | ||
| const lengthFields: MetadataFilter[] = [ | ||
| { name: 'length_SFrom', type: 'int' }, | ||
| { name: 'length_LFrom', type: 'int' }, | ||
| ]; | ||
| const filterSchemaWithLengthFields = new MetadataFilterSchema(lengthFields); | ||
| const filterSchemaEmpty = new MetadataFilterSchema([]); | ||
|
|
||
| describe('SegmentFilter', () => { | ||
| it('renders nothing for single-segmented organism', () => { | ||
| const { container } = render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={SINGLE_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| fieldValues={{}} | ||
| setSomeFieldValues={vi.fn()} | ||
| filterSchema={filterSchemaWithLengthFields} | ||
| />, | ||
| ); | ||
|
|
||
| expect(container).toBeEmptyDOMElement(); | ||
| }); | ||
|
|
||
| it('renders nothing when no length fields are in the schema', () => { | ||
| const { container } = render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| fieldValues={{}} | ||
| setSomeFieldValues={vi.fn()} | ||
| filterSchema={filterSchemaEmpty} | ||
| />, | ||
| ); | ||
|
|
||
| expect(container).toBeEmptyDOMElement(); | ||
| }); | ||
|
|
||
| it('renders a checkbox for each segment that has a length field', () => { | ||
| render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| fieldValues={{}} | ||
| setSomeFieldValues={vi.fn()} | ||
| filterSchema={filterSchemaWithLengthFields} | ||
| />, | ||
| ); | ||
|
|
||
| const checkboxes = screen.getAllByRole('checkbox'); | ||
| expect(checkboxes).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('shows segment display names when configured', () => { | ||
| render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_MULTI_REF_REFERENCEGENOMES} | ||
| fieldValues={{}} | ||
| setSomeFieldValues={vi.fn()} | ||
| filterSchema={filterSchemaWithLengthFields} | ||
| />, | ||
| ); | ||
|
|
||
| expect(screen.getByText('S (segment)')).toBeInTheDocument(); | ||
| expect(screen.getByText('L (segment)')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('falls back to segment name when no display name is configured', () => { | ||
| render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| fieldValues={{}} | ||
| setSomeFieldValues={vi.fn()} | ||
| filterSchema={filterSchemaWithLengthFields} | ||
| />, | ||
| ); | ||
|
|
||
| expect(screen.getByText('S')).toBeInTheDocument(); | ||
| expect(screen.getByText('L')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders checkbox as checked when length field value is greater than 0', () => { | ||
| render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| fieldValues={{ length_SFrom: '1' }} | ||
| setSomeFieldValues={vi.fn()} | ||
| filterSchema={filterSchemaWithLengthFields} | ||
| />, | ||
| ); | ||
|
|
||
| const sCheckbox = screen.getByRole('checkbox', { name: 'S' }); | ||
| expect(sCheckbox).toBeChecked(); | ||
| }); | ||
|
|
||
| it('renders checkbox as unchecked when length field value is empty', () => { | ||
| render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| fieldValues={{ length_SFrom: '' }} | ||
| setSomeFieldValues={vi.fn()} | ||
| filterSchema={filterSchemaWithLengthFields} | ||
| />, | ||
| ); | ||
|
|
||
| const checkboxes = screen.getAllByRole('checkbox'); | ||
| checkboxes.forEach((cb) => expect(cb).not.toBeChecked()); | ||
| }); | ||
|
|
||
| it('renders checkbox as unchecked when length field is absent from fieldValues', () => { | ||
| render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| fieldValues={{}} | ||
| setSomeFieldValues={vi.fn()} | ||
| filterSchema={filterSchemaWithLengthFields} | ||
| />, | ||
| ); | ||
|
|
||
| const checkboxes = screen.getAllByRole('checkbox'); | ||
| checkboxes.forEach((cb) => expect(cb).not.toBeChecked()); | ||
| }); | ||
|
|
||
| it('calls setSomeFieldValues with "1" when an unchecked box is clicked', async () => { | ||
| const setSomeFieldValues = vi.fn(); | ||
| render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| fieldValues={{}} | ||
| setSomeFieldValues={setSomeFieldValues} | ||
| filterSchema={filterSchemaWithLengthFields} | ||
| />, | ||
| ); | ||
|
|
||
| const sLabel = screen.getByText('S').closest('label')!; | ||
| const sCheckbox = sLabel.querySelector('input[type="checkbox"]')!; | ||
| await userEvent.click(sCheckbox); | ||
|
|
||
| expect(setSomeFieldValues).toHaveBeenCalledWith(['length_SFrom', '1']); | ||
| }); | ||
|
|
||
| it('calls setSomeFieldValues with null when a checked box is clicked', async () => { | ||
| const setSomeFieldValues = vi.fn(); | ||
| render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| fieldValues={{ length_SFrom: '1' }} | ||
| setSomeFieldValues={setSomeFieldValues} | ||
| filterSchema={filterSchemaWithLengthFields} | ||
| />, | ||
| ); | ||
|
|
||
| const sLabel = screen.getByText('S').closest('label')!; | ||
| const sCheckbox = sLabel.querySelector('input[type="checkbox"]')!; | ||
| await userEvent.click(sCheckbox); | ||
|
|
||
| expect(setSomeFieldValues).toHaveBeenCalledWith(['length_SFrom', null]); | ||
| }); | ||
|
|
||
| it('only shows segments that have a matching length field in the schema', () => { | ||
| // Only provide a length field for S, not L | ||
| const partialSchema = new MetadataFilterSchema([{ name: 'length_SFrom', type: 'int' }]); | ||
|
|
||
| render( | ||
| <SegmentFilter | ||
| referenceGenomesInfo={MULTI_SEG_SINGLE_REF_REFERENCEGENOMES} | ||
| fieldValues={{}} | ||
| setSomeFieldValues={vi.fn()} | ||
| filterSchema={partialSchema} | ||
| />, | ||
| ); | ||
|
|
||
| expect(screen.getByText('S')).toBeInTheDocument(); | ||
| expect(screen.queryByText('L')).not.toBeInTheDocument(); | ||
| expect(screen.getAllByRole('checkbox')).toHaveLength(1); | ||
| }); | ||
| }); |
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,70 @@ | ||
| import { useMemo, type FC } from 'react'; | ||
|
|
||
| import type { FieldValues, SetSomeFieldValues } from '../../types/config.ts'; | ||
| import type { ReferenceGenomesInfo } from '../../types/referencesGenomes.ts'; | ||
| import type { MetadataFilterSchema } from '../../utils/search.ts'; | ||
| import { getSegmentNames } from '../../utils/sequenceTypeHelpers.ts'; | ||
| import DisabledUntilHydrated from '../DisabledUntilHydrated.tsx'; | ||
|
|
||
| interface SegmentFilterProps { | ||
| referenceGenomesInfo: ReferenceGenomesInfo; | ||
| fieldValues: FieldValues; | ||
| setSomeFieldValues: SetSomeFieldValues; | ||
| filterSchema: MetadataFilterSchema; | ||
| } | ||
|
|
||
| /** | ||
| * Filter component for multi-segmented organisms that lets users require specific segments | ||
| * to be present by setting the corresponding length filter to > 0. | ||
| */ | ||
| export const SegmentFilter: FC<SegmentFilterProps> = ({ | ||
| referenceGenomesInfo, | ||
| fieldValues, | ||
| setSomeFieldValues, | ||
| filterSchema, | ||
| }) => { | ||
| const segmentNames = getSegmentNames(referenceGenomesInfo); | ||
|
|
||
| const ungroupedFilters = useMemo(() => filterSchema.ungroupedMetadataFilters(), [filterSchema]); | ||
|
|
||
| // Only show segments that have a length filter field in the schema | ||
| const segmentsWithLengthFields = segmentNames.filter((segmentName) => { | ||
| const lengthFromFieldName = `length_${segmentName}From`; | ||
| return ungroupedFilters.some((f) => f.name === lengthFromFieldName); | ||
| }); | ||
|
|
||
| if (segmentsWithLengthFields.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div className='mb-3 px-1'> | ||
| <div className='text-sm text-gray-500 mb-1.5'>Required segments</div> | ||
| <div className='grid grid-cols-2 gap-x-4 gap-y-1.5'> | ||
| {segmentsWithLengthFields.map((segmentName) => { | ||
| const lengthFromFieldName = `length_${segmentName}From`; | ||
| const currentValue = fieldValues[lengthFromFieldName]; | ||
| const isChecked = | ||
| typeof currentValue === 'string' && currentValue !== '' && Number(currentValue) > 0; | ||
|
anna-parker marked this conversation as resolved.
|
||
| const displayName = referenceGenomesInfo.segmentDisplayNames[segmentName] ?? segmentName; | ||
|
|
||
| return ( | ||
| <label key={segmentName} className='flex items-center gap-1.5 cursor-pointer text-sm'> | ||
| <DisabledUntilHydrated> | ||
| <input | ||
| type='checkbox' | ||
| className='h-4 w-4 rounded border-gray-300 text-primary-600 focus:ring-primary-600 cursor-pointer' | ||
| checked={isChecked} | ||
| onChange={() => { | ||
| setSomeFieldValues([lengthFromFieldName, isChecked ? null : '1']); | ||
| }} | ||
| /> | ||
| </DisabledUntilHydrated> | ||
| {displayName} | ||
| </label> | ||
| ); | ||
| })} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
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.