mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-12 08:38:11 +00:00
298 lines
7.2 KiB
JavaScript
298 lines
7.2 KiB
JavaScript
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}));
|
|
});
|
|
});
|