Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit URL fix #109

Merged
merged 6 commits into from
Nov 6, 2024
Merged
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
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.
5 changes: 5 additions & 0 deletions .changeset/thin-oranges-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lookit/lookit-initjspsych": patch
---

Fix the exit URL interupting final Response patch
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
9 changes: 4 additions & 5 deletions 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 All @@ -74,13 +74,12 @@ export const on_finish = (
userFunc(data);
}

await Api.finish();
await Api.updateResponse(responseUuid, {
exp_data,
sequence: [...sequence, `${last_exp.trial_index}-${last_exp.trial_type}`],
completed: true,
});

exit_url && window.location.replace(exit_url);
})
.then(() => Api.finish())
.then(() => exit_url && window.location.replace(exit_url));
};
};