From a3a2df6ce05eed4cedfbf8fa1f4d771a44d4a353 Mon Sep 17 00:00:00 2001 From: Josh de Leeuw Date: Sun, 19 Mar 2017 16:48:38 -0400 Subject: [PATCH] implement event testing e.g., #349 --- tests/events.test.js | 297 ++++++++++++++++++++++++++++ tests/on_finish-trial-level.test.js | 132 ------------- 2 files changed, 297 insertions(+), 132 deletions(-) create mode 100644 tests/events.test.js delete mode 100644 tests/on_finish-trial-level.test.js diff --git a/tests/events.test.js b/tests/events.test.js new file mode 100644 index 00000000..d330d3d5 --- /dev/null +++ b/tests/events.test.js @@ -0,0 +1,297 @@ +describe('on_finish (trial)', function(){ + test('should get an object of data generated by the trial', function(){ + + require('../jspsych.js'); + require('../plugins/jspsych-text.js'); + + return (new Promise(function(resolve, reject){ + + var key_data = null; + + var trial = { + type: 'text', + text: 'hello', + on_finish: function(data){ + key_data = data.key_press; + } + } + + jsPsych.init({ + timeline: [trial], + on_finish: function() { + resolve({key_data}); + } + }); + + document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); + document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); + + })).then(function(data) { expect(data.key_data).toBe(32) }); + }); + + test('should be able to write to the data', function(){ + + require('../jspsych.js'); + require('../plugins/jspsych-text.js'); + + return (new Promise(function(resolve, reject){ + + var promise_data = {}; + + var trial = { + type: 'text', + text: 'hello', + on_finish: function(data){ + data.key_press = 1; + } + } + + jsPsych.init({ + timeline: [trial], + on_finish: function() { + promise_data.final_key_press = jsPsych.data.get().values()[0].key_press; + resolve(promise_data); + } + }); + + document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); + document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); + + //resolve(); + })).then(function(pd) { + expect(pd.final_key_press).toBe(1); + }); + }); +}); + +describe('on_trial_finish (experiment level)', function(){ + test('should get an object containing the trial data', function(){ + require('../jspsych.js'); + require('../plugins/jspsych-text.js'); + + return (new Promise(function(resolve, reject){ + + var promise_data = {}; + + var trial = { + type: 'text', + text: 'hello' + } + + jsPsych.init({ + timeline: [trial], + on_trial_finish: function(data){ + promise_data.key = data.key_press; + }, + on_finish: function(){ + resolve(promise_data); + } + }); + + document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); + document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); + + //resolve(); + })).then(function(pd) { + expect(pd.key).toBe(32); + }); + }); + + test('should allow writing to the data object', function(){ + require('../jspsych.js'); + require('../plugins/jspsych-text.js'); + + return (new Promise(function(resolve, reject){ + + var promise_data = {}; + + var trial = { + type: 'text', + text: 'hello' + } + + jsPsych.init({ + timeline: [trial], + on_trial_finish: function(data){ + data.write = true; + }, + on_finish: function(data){ + promise_data.write = data.values()[0].write; + resolve(promise_data); + } + }); + + document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); + document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); + + //resolve(); + })).then(function(pd) { + expect(pd.write).toBe(true); + }); + }); +}); + +describe('on_data_update', function(){ + test('should get an object containing the trial data', function(){ + require('../jspsych.js'); + require('../plugins/jspsych-text.js'); + + return (new Promise(function(resolve, reject){ + + var promise_data = {}; + + var trial = { + type: 'text', + text: 'hello' + } + + jsPsych.init({ + timeline: [trial], + on_data_update: function(data){ + promise_data.key = data.key_press; + }, + on_finish: function(){ + resolve(promise_data); + } + }); + + document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); + document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); + + //resolve(); + })).then(function(pd) { + expect(pd.key).toBe(32); + }); + }); + + test('should contain data added with on_finish (trial level)', function(){ + require('../jspsych.js'); + require('../plugins/jspsych-text.js'); + + return (new Promise(function(resolve, reject){ + + var promise_data = {}; + + var trial = { + type: 'text', + text: 'hello', + on_finish: function(data){ + data.trial_level = true; + } + } + + jsPsych.init({ + timeline: [trial], + on_data_update: function(data){ + promise_data.trial_level = data.trial_level; + }, + on_finish: function(){ + resolve(promise_data); + } + }); + + document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); + document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); + + //resolve(); + })).then(function(pd) { + expect(pd.trial_level).toBe(true); + }); + }); + + test('should contain data added with on_trial_finish (experiment level)', function(){ + require('../jspsych.js'); + require('../plugins/jspsych-text.js'); + + return (new Promise(function(resolve, reject){ + + var promise_data = {}; + + var trial = { + type: 'text', + text: 'hello' + } + + jsPsych.init({ + timeline: [trial], + on_trial_finish: function(data){ + data.experiment_level = true; + }, + on_data_update: function(data){ + promise_data.experiment_level = data.experiment_level; + }, + on_finish: function(){ + resolve(promise_data); + } + }); + + document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); + document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); + + //resolve(); + })).then(function(pd) { + expect(pd.experiment_level).toBe(true); + }); + }); +}); + +describe('on_trial_start', function(){ + + test('should get an object containing the trial properties', function(){ + require('../jspsych.js'); + require('../plugins/jspsych-text.js'); + + return (new Promise(function(resolve, reject){ + + var promise_data = {}; + + var trial = { + type: 'text', + text: 'hello' + } + + jsPsych.init({ + timeline: [trial], + on_trial_start: function(trial){ + promise_data.text = trial.text; + }, + on_finish: function(){ + resolve(promise_data); + } + }); + + document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); + document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); + + //resolve(); + })).then(function(pd) { + expect(pd.text).toBe('hello'); + }); + }); + + test('should allow modification of the trial properties', function(){ + require('../jspsych.js'); + require('../plugins/jspsych-text.js'); + + var trial = { + type: 'text', + text: 'hello' + } + + jsPsych.init({ + timeline: [trial], + on_trial_start: function(trial){ + trial.text = 'goodbye'; + }, + on_finish: function(){ + resolve(promise_data); + } + }); + + var display_element = jsPsych.getDisplayElement(); + expect(display_element.innerHTML).toBe('goodbye'); + + document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); + document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); + }); +}); diff --git a/tests/on_finish-trial-level.test.js b/tests/on_finish-trial-level.test.js deleted file mode 100644 index 4b62ab2f..00000000 --- a/tests/on_finish-trial-level.test.js +++ /dev/null @@ -1,132 +0,0 @@ -describe('The on_finish trial level event handler', function(){ - test('should get an object of data generated by the trial', function(){ - - require('../jspsych.js'); - require('../plugins/jspsych-text.js'); - - return (new Promise(function(resolve, reject){ - - var key_data = null; - - var trial = { - type: 'text', - text: 'hello', - on_finish: function(data){ - key_data = data.key_press; - } - } - - jsPsych.init({ - timeline: [trial], - on_finish: function() { - resolve({key_data}); - } - }); - - document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); - document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); - - //resolve(); - })).then(function(data) { expect(data.key_data).toBe(32) }); - }); - - test('data should be writeable', function(){ - - require('../jspsych.js'); - require('../plugins/jspsych-text.js'); - - return (new Promise(function(resolve, reject){ - - var promise_data = {}; - - var trial = { - type: 'text', - text: 'hello', - on_finish: function(data){ - data.key_press = 1; - } - } - - jsPsych.init({ - timeline: [trial], - on_finish: function() { - promise_data.final_key_press = jsPsych.data.get().values()[0].key_press; - resolve(promise_data); - } - }); - - document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); - document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); - - //resolve(); - })).then(function(pd) { - expect(pd.final_key_press).toBe(1); - }); - }); -}); - -describe('The on_trial_finish handler for trials set at the experiment level', function(){ - test('should get an object containing the trial data', function(){ - require('../jspsych.js'); - require('../plugins/jspsych-text.js'); - - return (new Promise(function(resolve, reject){ - - var promise_data = {}; - - var trial = { - type: 'text', - text: 'hello' - } - - jsPsych.init({ - timeline: [trial], - on_trial_finish: function(data){ - promise_data.key = data.key_press; - }, - on_finish: function(){ - resolve(promise_data); - } - }); - - document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); - document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); - - //resolve(); - })).then(function(pd) { - expect(pd.key).toBe(32); - }); - }); - test('should allow writing to the data object', function(){ - require('../jspsych.js'); - require('../plugins/jspsych-text.js'); - - return (new Promise(function(resolve, reject){ - - var promise_data = {}; - - var trial = { - type: 'text', - text: 'hello' - } - - jsPsych.init({ - timeline: [trial], - on_trial_finish: function(data){ - data.write = true; - }, - on_finish: function(data){ - promise_data.write = data.values()[0].write; - resolve(promise_data); - } - }); - - document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32})); - document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32})); - - //resolve(); - })).then(function(pd) { - expect(pd.write).toBe(true); - }); - }); -});