Skip to content

Commit

Permalink
add private getCurrentPluginName method to trial extension, get plugi…
Browse files Browse the repository at this point in the history
…n name from info, fix types, fix/add tests
  • Loading branch information
becky-gilbert committed Oct 18, 2024
1 parent 4ff7d3e commit f6b667b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/record/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ jest.mock("jspsych", () => ({
...jest.requireActual("jspsych"),
initJsPsych: jest.fn().mockReturnValue({
finishTrial: jest.fn().mockImplementation(),
getCurrentTrial: jest.fn().mockReturnValue({ type: "test-type" }),
getCurrentTrial: jest
.fn()
.mockReturnValue({ type: { info: { name: "test-type" } } }),
}),
}));

Expand Down Expand Up @@ -42,14 +44,17 @@ test("Trial recording", () => {
const mockRecStop = jest.spyOn(Recorder.prototype, "stop");
const jsPsych = initJsPsych();
const trialRec = new Rec.TrialRecordExtension(jsPsych);
const getCurrentPluginNameSpy = jest.spyOn(trialRec, "getCurrentPluginName");

trialRec.on_start();
trialRec.on_load();
trialRec.on_finish();

expect(Recorder).toHaveBeenCalledTimes(1);
expect(mockRecStart).toHaveBeenCalledTimes(1);
expect(mockRecStart).toHaveBeenCalledWith(false, "test-type");
expect(mockRecStop).toHaveBeenCalledTimes(1);
expect(getCurrentPluginNameSpy).toHaveBeenCalledTimes(1);
});

test("Trial recording's initialize does nothing", async () => {
Expand Down
16 changes: 15 additions & 1 deletion packages/record/src/trial.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import autoBind from "auto-bind";
import { JsPsych, JsPsychExtension, JsPsychExtensionInfo } from "jspsych";
import Recorder from "./recorder";
import { jsPsychPluginWithInfo } from "./types";

/** This extension will allow reasearchers to record trials. */
export default class TrialRecordExtension implements JsPsychExtension {
Expand All @@ -9,6 +10,7 @@ export default class TrialRecordExtension implements JsPsychExtension {
};

private recorder?: Recorder;
private pluginName: string | undefined;

/**
* Video recording extension.
Expand All @@ -32,7 +34,8 @@ export default class TrialRecordExtension implements JsPsychExtension {

/** Ran when the trial has loaded. */
public on_load() {
this.recorder?.start(false, `${this.jsPsych.getCurrentTrial().type}`);
this.pluginName = this.getCurrentPluginName();
this.recorder?.start(false, `${this.pluginName}`);
}

/**
Expand All @@ -44,4 +47,15 @@ export default class TrialRecordExtension implements JsPsychExtension {
this.recorder?.stop();
return {};
}

/**
* Gets the plugin name for the trial that is being extended. This is same as
* the "trial_type" value that is stored in the data for this trial.
*
* @returns Plugin name string from the plugin class's info.
*/
private getCurrentPluginName() {
const current_plugin_class = this.jsPsych.getCurrentTrial().type;
return (current_plugin_class as jsPsychPluginWithInfo).info.name;
}
}
8 changes: 8 additions & 0 deletions packages/record/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { JsPsychPlugin, PluginInfo } from "jspsych";
import { Class } from "type-fest";

export interface jsPsychPluginWithInfo
extends Class<JsPsychPlugin<PluginInfo>> {
info: PluginInfo;
}

/**
* A valid CSS height/width value, which can be a number, a string containing a
* number with units, or 'auto'.
Expand Down

0 comments on commit f6b667b

Please sign in to comment.