diff --git a/packages/react-core/src/components/InstrumentRenderer/SeriesInstrumentRenderer.tsx b/packages/react-core/src/components/InstrumentRenderer/SeriesInstrumentRenderer.tsx index 5a16dfcb8..7e74af183 100644 --- a/packages/react-core/src/components/InstrumentRenderer/SeriesInstrumentRenderer.tsx +++ b/packages/react-core/src/components/InstrumentRenderer/SeriesInstrumentRenderer.tsx @@ -67,7 +67,7 @@ export const SeriesInstrumentRenderer = ({ const scalarId = target.items[currentItemIndex]?.id; const rootState = useInterpretedInstrument(target.bundle); - const scalarState = useInterpretedInstrument(scalarBundle ?? ''); + const scalarState = useInterpretedInstrument(scalarBundle ?? null); const [isInstrumentInProgress, setIsInstrumentInProgress] = useState(false); const [completion, setCompletion] = useState(null); diff --git a/packages/react-core/src/components/InstrumentRenderer/__tests__/SeriesInstrumentRenderer.test.tsx b/packages/react-core/src/components/InstrumentRenderer/__tests__/SeriesInstrumentRenderer.test.tsx index 5136ef58b..68041bcb3 100644 --- a/packages/react-core/src/components/InstrumentRenderer/__tests__/SeriesInstrumentRenderer.test.tsx +++ b/packages/react-core/src/components/InstrumentRenderer/__tests__/SeriesInstrumentRenderer.test.tsx @@ -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'; @@ -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); diff --git a/packages/react-core/src/hooks/__tests__/useInterpretedInstrument.test.ts b/packages/react-core/src/hooks/__tests__/useInterpretedInstrument.test.ts index 900c606d8..1630cbf91 100644 --- a/packages/react-core/src/hooks/__tests__/useInterpretedInstrument.test.ts +++ b/packages/react-core/src/hooks/__tests__/useInterpretedInstrument.test.ts @@ -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, diff --git a/packages/react-core/src/hooks/useInterpretedInstrument.ts b/packages/react-core/src/hooks/useInterpretedInstrument.ts index 45dccb95e..707cb122b 100644 --- a/packages/react-core/src/hooks/useInterpretedInstrument.ts +++ b/packages/react-core/src/hooks/useInterpretedInstrument.ts @@ -19,11 +19,11 @@ export type InterpretedInstrumentState( - bundle: string, + bundle: null | string, options?: InterpretOptions ): InterpretedInstrumentState { const interpreter = useMemo(() => new InstrumentInterpreter(), []); @@ -31,6 +31,9 @@ export function useInterpretedInstrument { + 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; diff --git a/testing/src/specs/series-instrument.spec.ts b/testing/src/specs/series-instrument.spec.ts index 1fb4c285d..aaeebc930 100644 --- a/testing/src/specs/series-instrument.spec.ts +++ b/testing/src/specs/series-instrument.spec.ts @@ -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(); @@ -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