Skip to content

Commit

Permalink
add tests for dynamically removing timeline nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
cherriechang committed Nov 4, 2024
1 parent 4e88af1 commit 9b55e9d
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions packages/jspsych/src/timeline/Timeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,72 @@ describe("Timeline", () => {
expect(timeline.children.length).toEqual(2);
});

it("respects dynamically removed end child node descriptions", async () => {
TestPlugin.setManualFinishTrialMode();

const timelineDescription: TimelineArray = [
{ type: TestPlugin },
{ type: TestPlugin },
{ timeline: [{ type: TestPlugin }] },
];
const timeline = createTimeline(timelineDescription);

const runPromise = timeline.run();
expect(timeline.children.length).toEqual(1); // Only the first child is instantiated because they are incrementally instantiated now

timelineDescription.pop();
await TestPlugin.finishTrial();
await TestPlugin.finishTrial();
await runPromise;

expect(timeline.children.length).toEqual(2);
expect(timeline.children).toEqual([expect.any(Trial), expect.any(Trial)]);
});

it("respects dynamically removed first child node descriptions", async () => {
TestPlugin.setManualFinishTrialMode();

const timelineDescription: TimelineArray = [
{ type: TestPlugin },
{ timeline: [{ type: TestPlugin }] },
{ type: TestPlugin },
];
const timeline = createTimeline(timelineDescription);

const runPromise = timeline.run();
expect(timeline.children.length).toEqual(1);

timelineDescription.shift();
await TestPlugin.finishTrial();
await TestPlugin.finishTrial();
await runPromise;

expect(timeline.children.length).toEqual(2);
expect(timeline.children).toEqual([expect.any(Timeline), expect.any(Trial)]);

This comment has been minimized.

Copy link
@cherriechang

cherriechang Nov 4, 2024

Author Collaborator

This test fails! The received children are [expect.any(Trial), expect.any(Trial)] and I'm not sure which is wrong between the code and the test

This comment has been minimized.

Copy link
@jodeleeuw

jodeleeuw Nov 5, 2024

Member

I think the test might be wrong.

If you tun the timeline L110 then we should already be in the first timeline node, which is the {type: TestPlugin} node. then you shift that node off the array, but we're already in it.

I think there's no way to remove the first TimelineNode once the timeline is already running and we're in that node. Instead, what about removing the second node while the first one is running?

This comment has been minimized.

Copy link
@cherriechang

cherriechang Nov 5, 2024

Author Collaborator

yea that has been tested! line122

});

it("respects dynamically removed middle child node descriptions", async () => {
TestPlugin.setManualFinishTrialMode();

const timelineDescription: TimelineArray = [
{ type: TestPlugin },
{ timeline: [{ type: TestPlugin }] },
{ type: TestPlugin },
];
const timeline = createTimeline(timelineDescription);

const runPromise = timeline.run();
expect(timeline.children.length).toEqual(1);

timelineDescription.splice(1, 1);
await TestPlugin.finishTrial();
await TestPlugin.finishTrial();
await runPromise;

expect(timeline.children.length).toEqual(2);
expect(timeline.children).toEqual([expect.any(Trial), expect.any(Trial)]);
});

describe("with `pause()` and `resume()` calls`", () => {
beforeEach(() => {
TestPlugin.setManualFinishTrialMode();
Expand Down

0 comments on commit 9b55e9d

Please sign in to comment.