Skip to content

Commit

Permalink
add/fix tests: add response id to window.chs, add data mock, update n…
Browse files Browse the repository at this point in the history
…ext button test, add test for end trial
  • Loading branch information
becky-gilbert committed Oct 15, 2024
1 parent 1acfb6f commit 97ca963
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions packages/record/src/consentVideo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Data from "@lookit/data";
import { LookitWindow } from "@lookit/data/dist/types";
import Handlebars from "handlebars";
import { initJsPsych, PluginInfo, TrialType } from "jspsych";
Expand All @@ -13,7 +14,22 @@ import Recorder from "./recorder";

declare const window: LookitWindow;

window.chs = {
study: {
attributes: {
name: "name",
duration: "duration",
},
},
response: {
id: "some id",
},
} as typeof window.chs;

jest.mock("./recorder");
jest.mock("@lookit/data", () => ({
updateResponse: jest.fn().mockReturnValue("Response"),
}));

test("Instantiate recorder", () => {
const jsPsych = initJsPsych();
Expand All @@ -27,15 +43,6 @@ test("Trial", () => {
const display = document.createElement("div");
const trial = { locale: "en-us" } as unknown as TrialType<PluginInfo>;

window.chs = {
study: {
attributes: {
name: "name",
duration: "duration",
},
},
} as typeof window.chs;

plugin["recordFeed"] = jest.fn();
plugin["recordButton"] = jest.fn();
plugin["stopButton"] = jest.fn();
Expand Down Expand Up @@ -263,14 +270,25 @@ test("nextButton", () => {
const display = document.createElement("div");

display.innerHTML = Handlebars.compile(consentVideoTrial)({});
jsPsych.finishTrial = jest.fn();
plugin["endTrial"] = jest.fn();

plugin["nextButton"](display);
display
.querySelector<HTMLButtonElement>("button#next")!
.dispatchEvent(new Event("click"));

expect(jsPsych.finishTrial).toHaveBeenCalledTimes(1);
expect(plugin["endTrial"]).toHaveBeenCalledTimes(1);
});

test("endTrial", () => {
const jsPsych = initJsPsych();
const plugin = new VideoConsentPlugin(jsPsych);

plugin["endTrial"]();

expect(Data.updateResponse).toHaveBeenCalledWith(window.chs.response.id, {
completed_consent_frame: true,
});
});

test("Does video consent plugin return chsData correctly?", () => {
Expand Down

0 comments on commit 97ca963

Please sign in to comment.