mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-12 08:38:11 +00:00
implement event testing e.g., #349
This commit is contained in:
parent
5ad4e4c9a4
commit
a3a2df6ce0
297
tests/events.test.js
Normal file
297
tests/events.test.js
Normal file
@ -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}));
|
||||||
|
});
|
||||||
|
});
|
@ -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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user