From 890c9bd563316e50245bfa2f7cd28557ed43ff27 Mon Sep 17 00:00:00 2001 From: jadeddelta <101148768+jadeddelta@users.noreply.github.com> Date: Thu, 22 Sep 2022 13:42:08 -0400 Subject: [PATCH] test functions implemented --- examples/jspsych-cloze.html | 2 +- packages/plugin-cloze/src/index.spec.ts | 62 +++++++++++++++++++++++++ packages/plugin-cloze/src/index.ts | 12 +---- 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/examples/jspsych-cloze.html b/examples/jspsych-cloze.html index 845391a3..dd57ea00 100644 --- a/examples/jspsych-cloze.html +++ b/examples/jspsych-cloze.html @@ -25,7 +25,7 @@ // another example with checking if all the blanks are filled in timeline.push({ type: jsPsychCloze, - text: 'The %% is the largest terrestrial mammal. It lives in both %% and %%.', + text: 'Science notebooks have a %%-colored front cover. Math notebooks have a %%-colored front cover.', check_blanks: true, mistake_fn: function () { alert("You have not filled in all the blanks!"); } }); diff --git a/packages/plugin-cloze/src/index.spec.ts b/packages/plugin-cloze/src/index.spec.ts index 5821aa21..d05d57e2 100644 --- a/packages/plugin-cloze/src/index.spec.ts +++ b/packages/plugin-cloze/src/index.spec.ts @@ -73,6 +73,20 @@ describe("cloze", () => { await expectFinished(); }); + test("ends trial on button click when all answers are checked for completion and are complete", async () => { + const { expectFinished } = await startTimeline([ + { + type: cloze, + text: "This is a %cloze% text.", + check_blanks: true, + }, + ]); + + getInputElementById("input0").value = "filler"; + clickTarget(document.querySelector("#finish_cloze_button")); + await expectFinished(); + }); + test("does not end trial on button click when answers are checked and not correct", async () => { const { expectRunning } = await startTimeline([ { @@ -87,6 +101,20 @@ describe("cloze", () => { await expectRunning(); }); + test("does not end trial on button click when answers are checked for completion and some are missing", async () => { + const { expectRunning } = await startTimeline([ + { + type: cloze, + text: "This is a %cloze% text.", + check_answers: true, + }, + ]); + + getInputElementById("input0").value = ""; + clickTarget(document.querySelector("#finish_cloze_button")); + await expectRunning(); + }); + test("does not call mistake function on button click when answers are checked and correct", async () => { const mistakeFn = jest.fn(); @@ -104,6 +132,23 @@ describe("cloze", () => { expect(mistakeFn).not.toHaveBeenCalled(); }); + test("does not call mistake function on button click when answers are checked for completion and are complete", async () => { + const mistakeFn = jest.fn(); + + await startTimeline([ + { + type: cloze, + text: "This is a %cloze% text.", + check_blanks: true, + mistake_fn: mistakeFn, + }, + ]); + + getInputElementById("input0").value = "cloze"; + clickTarget(document.querySelector("#finish_cloze_button")); + expect(mistakeFn).not.toHaveBeenCalled(); + }); + test("calls mistake function on button click when answers are checked and not correct", async () => { const mistakeFn = jest.fn(); @@ -121,6 +166,23 @@ describe("cloze", () => { expect(mistakeFn).toHaveBeenCalled(); }); + test("calls mistake function on button click when answers are checked for completion and are not complete", async () => { + const mistakeFn = jest.fn(); + + await startTimeline([ + { + type: cloze, + text: "This is a %cloze% text.", + check_answers: true, + mistake_fn: mistakeFn, + }, + ]); + + getInputElementById("input0").value = ""; + clickTarget(document.querySelector("#finish_cloze_button")); + expect(mistakeFn).toHaveBeenCalled(); + }); + test("response data is stored as an array", async () => { const { getData, getHTML } = await startTimeline([ { diff --git a/packages/plugin-cloze/src/index.ts b/packages/plugin-cloze/src/index.ts index 18ac594c..5831f926 100644 --- a/packages/plugin-cloze/src/index.ts +++ b/packages/plugin-cloze/src/index.ts @@ -89,7 +89,7 @@ class ClozePlugin implements JsPsychPlugin { } } if (trial.check_blanks) { - if (answers[i].trim() === "") { + if (answers[i] === "") { answers_filled = false; } } @@ -105,16 +105,6 @@ class ClozePlugin implements JsPsychPlugin { display_element.innerHTML = ""; this.jsPsych.finishTrial(trial_data); } - // if (!trial.check_answers || (trial.check_answers && answers_correct)) { - // var trial_data = { - // response: answers, - // }; - - // display_element.innerHTML = ""; - // this.jsPsych.finishTrial(trial_data); - // } else { - // trial.mistake_fn(); - // } }; display_element.innerHTML +=