image-button-response test

This commit is contained in:
KristinDiep 2017-07-07 14:38:44 -04:00
parent 9e715e2457
commit ee8e23d8d8
6 changed files with 146 additions and 392 deletions

View File

@ -1,136 +0,0 @@
const root = '../../';
const utils = require('../testing-utils.js');
jest.useFakeTimers();
describe('button-response', function(){
beforeEach(function(){
require(root + 'jspsych.js');
require(root + 'plugins/jspsych-button-response.js');
});
test('loads correctly', function(){
expect(typeof window.jsPsych.plugins['button-response']).not.toBe('undefined');
});
test('displays image by default', function(){
var trial = {
type: 'button-response',
stimulus: '../media/blue.png'
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toBe('<img src="../media/blue.png" id="jspsych-button-response-stimulus">')
});
test('displays html when is_html is true', function(){
var trial = {
type: 'button-response',
stimulus: '<p>button</p>',
choices: ['HAPPY'],
is_html: true,
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch('<div id=\"jspsych-button-response-stimulus\"><p>button</p></div>');
});
test('displays buttons in button array', function(){
var trial = {
type: 'button-response',
button_html: ['img/button1.png', 'img/button2.png'],
choices: ['HAPPY', 'SAD'],
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch(new RegExp('<div class=\"jspsych-button-response-button\" style=\"display: inline-block; margin:0px 8px\" id=\"jspsych-button-response-button-0\" data-choice=\"0\">img/button1.png</div><div class=\"jspsych-button-response-button\" style=\"display: inline-block; margin:0px 8px\" id=\"jspsych-button-response-button-1\" data-choice=\"1\">img/button2.png</div>'));
});
test('prompt should append html below stimulus', function(){
var trial = {
type: 'button-response',
stimulus: '<p>hello</p>',
is_html: true,
choices: ['HAPPY'],
prompt: '<div id="foo">this is the prompt</div>'
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch('<div id=\"foo\">this is the prompt</div>');
});
test('timing_stim should set visibility of content to hidden after time has elapsed', function(){
var trial = {
type: 'button-response',
stimulus: '<p>hello</p>',
is_html: true,
choices: ['HAPPY'],
timing_stim: 500
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch('<div id="jspsych-button-response-stimulus"><p>hello</p></div>');
jest.runAllTimers();
expect(jsPsych.getDisplayElement().innerHTML).toMatch('<div id="jspsych-button-response-stimulus" style="visibility: hidden;"><p>hello</p></div>');
});
test('timing_response should end trial after time has elapsed', function(){
var trial = {
type: 'button-response',
stimulus: '<p>hello</p>',
is_html: true,
choices: ['HAPPY'],
timing_response: 500
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch('<div id="jspsych-button-response-stimulus"><p>hello</p></div>');
jest.runAllTimers();
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
test('trial should not end when response_ends_trial is false and stimulus should get responded class', function(){
var trial = {
type: 'button-response',
stimulus: '<p>hello</p>',
is_html: true,
choices: ['HAPPY'],
response_ends_trial: false,
timing_response: 500
}
jsPsych.init({
timeline: [trial]
});
document.querySelector('#jspsych-button-response-button-0').click();
console.log(jsPsych.getDisplayElement().innerHTML);
expect(jsPsych.getDisplayElement().querySelector('#jspsych-button-response-stimulus').className).toMatch("responded");
jest.runAllTimers();
});
});

View File

@ -0,0 +1,145 @@
const utils = require('../testing-utils.js');
const root = '../../';
jest.useFakeTimers();
describe('image-button-response', function(){
beforeEach(function(){
require(root + 'jspsych.js');
require(root + 'plugins/jspsych-image-button-response');
});
test('loads correctly', function(){
expect(typeof window.jsPsych.plugins['image-button-response']).not.toBe('undefined');
});
test('displays image stimulus', function(){
var trial = {
type: 'image-button-response',
stimulus: '../media/blue.png'
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toBe('<img src="../media/blue.png" id="jspsych-image-button-response-stimulus">');
});
test('display button label', function(){
var trial = {
type: 'image-button-response',
stimulus: '../media/blue.png',
choices: ['button-choice1', 'button-choice2']
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch(new RegExp('<button class=\"jspsych-btn\">button-choice1</button>'));
expect(jsPsych.getDisplayElement().innerHTML).toMatch(new RegExp('<button class=\"jspsych-btn\">button-choice2</button>'));
});
test('display button html', function(){
var trial = {
type: 'image-button-response',
stimulus: '../media/blue.png',
choices: ['buttonChoice'],
button_html: '<button class="jspsych-custom-button">%choice%</button>',
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch(new RegExp('<button class="jspsych-custom-button">buttonChoice</button>'));
});
test('display should clear after button click', function(){
var trial = {
type: 'image-button-response',
stimulus: '../media/blue.png',
choices: ['button-choice'],
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch(new RegExp('<img src="../media/blue.png" id="jspsych-image-button-response-stimulus">'));
utils.clickTarget(document.querySelector('#jspsych-image-button-response-button-0'));
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
test('prompt should append below stimulus', function(){
var trial = {
type: 'image-button-response',
stimulus: '../media/blue.png',
choices: ['button-choice'],
prompt: '<p>This is a prompt</p>'
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch('<button class=\"jspsych-btn\">button-choice</button></div></div><p>This is a prompt</p>');
});
test('should hide stimulus if stimulus-duration is set', function(){
var trial = {
type: 'image-button-response',
stimulus: '../media/blue.png',
choices: ['button-choice'],
stimulus_duration: 500
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().querySelector('#jspsych-image-button-response-stimulus').style.visibility).toMatch("");
jest.runTimersToTime(500);
expect(jsPsych.getDisplayElement().querySelector('#jspsych-image-button-response-stimulus').style.visibility).toMatch("hidden");
});
test('should end trial when trial duration is reached', function(){
var trial = {
type: 'image-button-response',
stimulus: '../media/blue.png',
choices: ['f','j'],
trial_duration: 500
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch('<img src="../media/blue.png" id="jspsych-image-button-response-stimulus">');
jest.runTimersToTime(500);
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
test('should end trial when button is clicked', function(){
var trial = {
type: 'image-button-response',
stimulus: '../media/blue.png',
choices: ['button-choice'],
response_ends_trial: true,
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch('<img src="../media/blue.png" id="jspsych-image-button-response-stimulus">');
utils.clickTarget(document.querySelector('#jspsych-image-button-response-button-0'));
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
});

View File

@ -91,7 +91,7 @@ describe('image-keyboard-response', function(){
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch(new RegExp('<img src="../media/blue.png" id="jspsych-image-keyboard-response-stimulus">'));
jest.runTimersToTime(550);
jest.runTimersToTime(500);
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});

View File

@ -1,16 +0,0 @@
const root = '../../';
jest.useFakeTimers();
describe('single-audio plugin', function(){
beforeEach(function(){
require(root + 'jspsych.js');
require(root + 'plugins/jspsych-single-audio.js');
});
test('loads correctly', function(){
expect(typeof window.jsPsych.plugins['single-audio']).not.toBe('undefined');
});
});

View File

@ -1,223 +0,0 @@
const root = '../../';
jest.useFakeTimers();
describe('single-stim plugin', function(){
beforeEach(function(){
require(root + 'jspsych.js');
require(root + 'plugins/jspsych-single-stim.js');
});
test('loads correctly', function(){
expect(typeof window.jsPsych.plugins['single-stim']).not.toBe('undefined');
});
test('displays image by default', function(){
var trial = {
type: 'single-stim',
stimulus: '../media/blue.png'
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toBe('<img src="../media/blue.png" id="jspsych-single-stim-stimulus">');
});
test('displays html when is_html is true', function(){
var trial = {
type: 'single-stim',
stimulus: '<p>hello</p>',
is_html: true
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus"><p>hello</p></div>');
});
test('display should clear after key press', function(){
var trial = {
type: 'single-stim',
stimulus: '<p>hello</p>',
is_html: true
}
jsPsych.init({
timeline: [trial]
});
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32}));
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32}));
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
test('display should not clear after key press when choices is jsPsych.NO_KEYS', function(){
var trial = {
type: 'single-stim',
stimulus: '<p>hello</p>',
is_html: true,
choices: jsPsych.NO_KEYS
}
jsPsych.init({
timeline: [trial]
});
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32}));
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32}));
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus"><p>hello</p></div>');
});
test('display should only clear when key is in choices array', function(){
var trial = {
type: 'single-stim',
stimulus: '<p>hello</p>',
is_html: true,
choices: ['f']
}
jsPsych.init({
timeline: [trial]
});
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32}));
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32}));
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus"><p>hello</p></div>');
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keydown', {keyCode: 70}));
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keyup', {keyCode: 70}));
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
test('prompt should append html below stimulus', function(){
var trial = {
type: 'single-stim',
stimulus: '<p>hello</p>',
is_html: true,
prompt: '<div id="foo">this is the prompt</div>'
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus"><p>hello</p></div><div id="foo">this is the prompt</div>');
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keydown', {keyCode: 70}));
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keyup', {keyCode: 70}));
});
test('timing_stim should set visibility of content to hidden after time has elapsed', function(){
var trial = {
type: 'single-stim',
stimulus: '<p>hello</p>',
is_html: true,
timing_stim: 500
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus"><p>hello</p></div>');
jest.runAllTimers();
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus" style="visibility: hidden;"><p>hello</p></div>');
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keydown', {keyCode: 70}));
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keyup', {keyCode: 70}));
});
test('timing_response should end trial after time has elapsed', function(){
var trial = {
type: 'single-stim',
stimulus: '<p>hello</p>',
is_html: true,
timing_response: 500
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus"><p>hello</p></div>');
jest.runAllTimers();
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
test('trial should not end when response_ends_trial is false and stimulus should get responded class', function(){
var trial = {
type: 'single-stim',
stimulus: '<p>hello</p>',
is_html: true,
response_ends_trial: false,
timing_response: 500
}
jsPsych.init({
timeline: [trial]
});
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keydown', {keyCode: 70}));
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keyup', {keyCode: 70}));
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus" class=" responded"><p>hello</p></div>');
jest.runAllTimers();
});
test('should accept functions as parameters', function(){
var trial = {
type: 'single-stim',
stimulus: function(){ return '<p>hello</p>'; },
is_html: function(){ return true; },
choices: function(){ return ['j']; },
prompt: function(){ return '<div>prompt</div>'; },
timing_response: function(){ return 1000; },
timing_stim: function(){ return 500; },
response_ends_trial: function(){ return false; }
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus"><p>hello</p></div><div>prompt</div>');
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32}));
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32}));
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus"><p>hello</p></div><div>prompt</div>');
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keydown', {keyCode: 70}));
document.querySelector('.jspsych-display-element').dispatchEvent(new KeyboardEvent('keyup', {keyCode: 70}));
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus"><p>hello</p></div><div>prompt</div>');
jest.runTimersToTime(500);
expect(jsPsych.getDisplayElement().innerHTML).toBe('<div id="jspsych-single-stim-stimulus" style="visibility: hidden;"><p>hello</p></div><div>prompt</div>');
jest.runTimersToTime(1000);
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
});

View File

@ -1,16 +0,0 @@
const root = '../../';
jest.useFakeTimers();
describe('slider-response plugin', function(){
beforeEach(function(){
require(root + 'jspsych.js');
require(root + 'plugins/jspsych-slider-response.js');
});
test('loads correctly', function(){
expect(typeof window.jsPsych.plugins['slider-response']).not.toBe('undefined');
});
});