diff --git a/.changeset/itchy-books-judge.md b/.changeset/itchy-books-judge.md new file mode 100644 index 00000000..74edc94f --- /dev/null +++ b/.changeset/itchy-books-judge.md @@ -0,0 +1,5 @@ +--- +"@lookit/lookit-initjspsych": patch +--- + +Fixes an error that is thrown when the experiment has only one trial. diff --git a/package-lock.json b/package-lock.json index 2584795c..b210659d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21002,7 +21002,7 @@ }, "packages/surveys": { "name": "@lookit/surveys", - "version": "2.0.0", + "version": "2.0.1", "license": "ISC", "dependencies": { "@jspsych/plugin-survey": "^2.0.0", diff --git a/packages/lookit-initjspsych/src/utils.spec.ts b/packages/lookit-initjspsych/src/utils.spec.ts index 69b551cc..fbb75b98 100644 --- a/packages/lookit-initjspsych/src/utils.spec.ts +++ b/packages/lookit-initjspsych/src/utils.spec.ts @@ -1,8 +1,7 @@ import { DataCollection } from "jspsych"; import { Child, JsPsychExpData, Study } from "@lookit/data/dist/types"; -import { nth } from "./errors"; -import { Timeline } from "./types"; +import { nth, SequenceExpDataError } from "./errors"; import { on_data_update, on_finish } from "./utils"; delete global.window.location; @@ -104,48 +103,6 @@ test("jsPsych's on_finish", async () => { expect(Request).toHaveBeenCalledTimes(2); }); -test("Is an error thrown when experiment data is empty?", () => { - const exp_data: Timeline[] = []; - const jsonData = { - data: { - attributes: { exp_data, sequence: ["0-value"] }, - }, - }; - const data = { - /** - * Mocked jsPsych Data Collection. - * - * @returns Exp data. - */ - values: () => exp_data, - } as DataCollection; - const response = { - /** - * Mocked json function used in API calls. - * - * @returns Promise containing mocked json data. - */ - json: () => Promise.resolve(jsonData), - ok: true, - } as Response; - - const userFn = jest.fn(); - global.fetch = jest.fn(() => Promise.resolve(response)); - global.Request = jest.fn(); - - Object.assign(window, { - chs: { - study: { attributes: { exit_url: "exit url" } } as Study, - child: {} as Child, - pastSessions: {} as Response[], - }, - }); - - expect(async () => { - await on_finish("some id", userFn)(data); - }).rejects.toThrow("Experiment sequence or data missing."); -}); - test("Is an error thrown when experiment sequence is undefined?", () => { const exp_data = [{ key: "value" }]; const jsonData = { @@ -185,7 +142,7 @@ test("Is an error thrown when experiment sequence is undefined?", () => { expect(async () => { await on_finish("some id", userFn)(data); - }).rejects.toThrow("Experiment sequence or data missing."); + }).rejects.toThrow(SequenceExpDataError); }); test("Ordinal format of numbers", () => { diff --git a/packages/lookit-initjspsych/src/utils.ts b/packages/lookit-initjspsych/src/utils.ts index 7921bcb5..f7cc210d 100644 --- a/packages/lookit-initjspsych/src/utils.ts +++ b/packages/lookit-initjspsych/src/utils.ts @@ -62,7 +62,7 @@ export const on_finish = ( const exp_data: JsPsychExpData[] = data.values(); - if (!sequence || sequence.length === 0 || exp_data.length === 0) { + if (!Array.isArray(sequence)) { throw new SequenceExpDataError(); }