Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useRef, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { ListboxDropdown, SearchBar } from '@douglasneuroinformatics/libui/components';
import type { ListboxDropdownOption } from '@douglasneuroinformatics/libui/components';
Expand All @@ -21,7 +21,6 @@ export const InstrumentShowcase: React.FC<{
const [filteredInstruments, setFilteredInstruments] = useState<TranslatedInstrumentInfo[]>(
availableInstruments.toSorted((a, b) => a.details.title.localeCompare(b.details.title))
);
const [tagOptions, setTagOptions] = useState<ListboxDropdownOption[]>([]);
const [selectedKinds, setSelectedKinds] = useState<InstrumentShowcaseKindOption[]>([]);
const [selectedLanguages, setSelectedLanguages] = useState<InstrumentShowcaseLanguageOption[]>([]);
const [selectedTags, setSelectedTags] = useState<ListboxDropdownOption[]>([]);
Expand Down Expand Up @@ -50,16 +49,16 @@ export const InstrumentShowcase: React.FC<{
setHighlightedIndex(0);
}, [availableInstruments, selectedKinds, selectedLanguages, selectedTags, searchTerm]);

useEffect(() => {
setTagOptions(
Array.from(new Set(filteredInstruments.flatMap((item) => item.tags)))
const tagOptions = useMemo<ListboxDropdownOption[]>(
() =>
Array.from(new Set(availableInstruments.flatMap((item) => item.tags)))
.map((item) => ({
key: item,
label: item
}))
.sort((a, b) => a.label.localeCompare(b.label))
);
}, [availableInstruments]);
.sort((a, b) => a.label.localeCompare(b.label)),
[availableInstruments]
);

useEffect(() => {
const items = listRef.current?.querySelectorAll<HTMLElement>('[data-instrument-index]');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,25 @@ describe('InstrumentShowcase', () => {
]);
});

it('should offer every available tag after the data is passed again, so an active search cannot shrink the tag filter', async () => {
const onSelect = vi.fn();
const { rerender } = render(<InstrumentShowcase data={instruments} onSelect={onSelect} />);

search('Alpha');
await expectCardIds(['alpha']);
// The parent re-renders with a new array instance, as Remote Assignment does on dialog open.
rerender(<InstrumentShowcase data={[...instruments]} onSelect={onSelect} />);
search('');
await expectCardIds(['alpha', 'beta', 'gamma']);

openFilter('instrument-tag-filter');
expect(screen.getAllByRole('menuitemcheckbox').map((option) => option.textContent)).toEqual([
'Anxiety',
'Memory',
'Mood'
]);
});

it('should highlight the first instrument, so Enter has something to open', () => {
renderShowcase();
expect(isHighlighted(alpha)).toBe(true);
Expand Down
32 changes: 32 additions & 0 deletions testing/src/specs/remote-assignment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@ test.describe('remote assignment', () => {
await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible();
});

test('should retain all tag options after searching, opening a remote assignment dialog, and clearing the search', async ({
getPageModel,
page,
uniqueId
}) => {
await startSession(getPageModel, `Tag${uniqueId}`, `Subject${uniqueId}`, 'Male');

await page.getByTestId('nav-button-/session/remote-assignment').click();
await page.waitForURL('**/session/remote-assignment');

const remoteAssignmentPage = new RemoteAssignmentPage(page);
await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible();
const tagFilter = page.getByTestId('instrument-tag-filter');
const tagButton = tagFilter.getByRole('button');
await tagButton.click();
await expect(page.getByRole('menuitemcheckbox').first()).toBeVisible();
const initialOptions = await page.getByRole('menuitemcheckbox').allTextContents();
expect(initialOptions.length).toBeGreaterThan(1);
await page.keyboard.press('Escape');

const searchInput = remoteAssignmentPage.instrumentShowcase.getByRole('searchbox');
await searchInput.fill('Happiness Questionnaire');
await remoteAssignmentPage.selectInstrument('Happiness Questionnaire');
await expect(remoteAssignmentPage.createDialog).toBeVisible();
await page.keyboard.press('Escape');
await expect(remoteAssignmentPage.createDialog).not.toBeVisible();

await searchInput.fill('');
await tagButton.click();
await expect.poll(() => page.getByRole('menuitemcheckbox').allTextContents()).toEqual(initialOptions);
});

test('should create a remote assignment and display the link', async ({ getPageModel, page, uniqueId }) => {
await startSession(getPageModel, `Assign${uniqueId}`, `User${uniqueId}`, 'Female');

Expand Down
Loading