Skip to content

Commit

Permalink
Fix duplicate on_load invocations in simulation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoluc committed Nov 24, 2022
1 parent bbdf158 commit 0b55a32
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/jspsych/src/timeline/Trial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,17 @@ describe("Trial", () => {
);
});

it("doesn't invoke `on_load`, even when `simulate` doesn't return a promise", async () => {
TestPlugin.simulate = () => {
dependencies.finishTrialPromise.resolve({});
};

const onLoad = jest.fn();
await createTrial({ type: TestPlugin, on_load: onLoad }).run();

expect(onLoad).not.toHaveBeenCalled();
});

it("invokes the plugin's `trial` method if the plugin has no `simulate` method", async () => {
const trial = createTrial({
type: class implements JsPsychPlugin<any> {
Expand Down
7 changes: 6 additions & 1 deletion packages/jspsych/src/timeline/Trial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ export class Trial extends TimelineNode {
result = await trialPromise;
}
} else {
this.onLoad();
// The `simulate` method always invokes `onLoad()`, so we don't call `onLoad()` in simulation
// mode
if (!this.dependencies.getSimulationMode()) {
this.onLoad();
}

result = await trialPromise;
}

Expand Down

0 comments on commit 0b55a32

Please sign in to comment.