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
Expand Up @@ -67,7 +67,7 @@ export const SeriesInstrumentRenderer = ({
const scalarId = target.items[currentItemIndex]?.id;

const rootState = useInterpretedInstrument<UnilingualSeriesInstrument>(target.bundle);
const scalarState = useInterpretedInstrument<AnyUnilingualScalarInstrument>(scalarBundle ?? '');
const scalarState = useInterpretedInstrument<AnyUnilingualScalarInstrument>(scalarBundle ?? null);

const [isInstrumentInProgress, setIsInstrumentInProgress] = useState(false);
const [completion, setCompletion] = useState<null | { itemName?: string; terminated: boolean }>(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useNotificationsStore } from '@douglasneuroinformatics/libui/hooks';
import { i18n } from '@douglasneuroinformatics/libui/i18n';
import { InstrumentInterpreter } from '@opendatacapture/instrument-interpreter';
import type { SeriesInstrumentBundleContainer } from '@opendatacapture/schemas/instrument';
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
Expand Down Expand Up @@ -229,6 +230,18 @@ describe('SeriesInstrumentRenderer', () => {
});
});

it('should not interpret an empty bundle once every item has been submitted', async () => {
const interpretSpy = vi.spyOn(InstrumentInterpreter.prototype, 'interpret');
const { onSubmit } = await beginSeries({ skipProgress: true });
await answerAndSubmit('first administration', onSubmit);
await waitFor(() => {
expect(getAnswerInput().value).toBe('');
});
await answerAndSubmit('second administration', onSubmit);
await screen.findByText('Thank You!');
expect(interpretSpy.mock.calls.map(([bundle]) => bundle)).not.toContain('');
});

it('should keep blocking navigation between items, so leaving cannot abandon the series partway', async () => {
const { onSubmit } = await beginSeries({ skipProgress: false });
await answerAndSubmit('first administration', onSubmit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ describe('useInterpretedInstrument', () => {
expect(result.current).toMatchObject({ error: { cause: 'not an Error instance' } });
});

it('should report LOADING for a null bundle without interpreting anything', () => {
const interpretSpy = vi.spyOn(InstrumentInterpreter.prototype, 'interpret');
const { result } = renderHook(() => useInterpretedInstrument(null));
expect(result.current.status).toBe('LOADING');
expect(interpretSpy).not.toHaveBeenCalled();
});

it('should report every language of a multilingual instrument as supported', async () => {
const bundle = `(async () => ({
__runtimeVersion: 1,
Expand Down
7 changes: 5 additions & 2 deletions packages/react-core/src/hooks/useInterpretedInstrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ export type InterpretedInstrumentState<TInstrument extends AnyUnilingualInstrume
/**
* Interpret an instrument bundle directly in the browser
*
* @param bundle - the JavaScript code to be interpreted directly in the browser
* @param bundle - the JavaScript code to be interpreted directly in the browser, or `null` while there is none
* @returns The instrument generated from the code, translated into the current locale, if possible, otherwise the default
*/
export function useInterpretedInstrument<TInstrument extends AnyUnilingualInstrument = AnyUnilingualInstrument>(
bundle: string,
bundle: null | string,
options?: InterpretOptions
): InterpretedInstrumentState<TInstrument & { supportedLanguages: Language[] }> {
const interpreter = useMemo(() => new InstrumentInterpreter(), []);
const [interpretation, setInterpretation] = useState<Interpretation | null>(null);
const { resolvedLanguage } = useTranslation();

useEffect(() => {
if (bundle === null) {
return;
}
// A bundle that resolves after the hook has moved on to another one would otherwise be reported
// as the outcome of that other bundle, or leave this stuck reporting LOADING for it forever.
let isCurrent = true;
Expand Down
7 changes: 7 additions & 0 deletions testing/src/specs/series-instrument.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ test.describe('series instrument', () => {
page,
uniqueId
}) => {
const errors: string[] = [];
page.on('console', (message) => {
if (message.type() === 'error') {
errors.push(message.text());
}
});
const instrumentPage = await openSeries({ getPageModel, page, uniqueId });
await instrumentPage.begin();
await instrumentPage.completeHappinessQuestionnaire();
Expand All @@ -118,6 +124,7 @@ test.describe('series instrument', () => {
// Each item is stored under its own instrument, so one pass through the series leaves two
// records rather than one.
await expect(page.getByTestId('data-table-body').getByTestId('data-table-row')).toHaveCount(2);
expect(errors.filter((error) => error.includes("reading 'kind'"))).toStrictEqual([]);
});

// A skipProgress series has no interstitial screen, so the bar on each item's form is the only
Expand Down
Loading