Skip to content

Commit

Permalink
Fix error in single-trial experiments (#108)
Browse files Browse the repository at this point in the history
* modify on_finish logic to throw error if both sequence and data are empty

* update surveys version in package lock

* add changeset

* Small change to tests and sequence error logic

---------

Co-authored-by: CJ Green <[email protected]>
  • Loading branch information
becky-gilbert and okaycj authored Nov 6, 2024
1 parent c68f8a5 commit 7e4c63e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-books-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lookit/lookit-initjspsych": patch
---

Fixes an error that is thrown when the experiment has only one trial.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 2 additions & 45 deletions packages/lookit-initjspsych/src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/lookit-initjspsych/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 7e4c63e

Please sign in to comment.