first draft of #832

This commit is contained in:
Josh de Leeuw 2020-12-23 09:56:40 -05:00
parent 42962039af
commit f6ac098b4c
2 changed files with 48 additions and 0 deletions

View File

@ -262,6 +262,11 @@ window.jsPsych = (function() {
if(current_trial_finished){ return; }
current_trial_finished = true;
// remove any CSS classes that were added to the DOM via css_classes parameter
if(typeof current_trial.css_classes !== 'undefined' && Array.isArray(current_trial.css_classes)){
DOM_target.classList.remove(...current_trial.css_classes);
}
// write the data from the trial
data = typeof data == 'undefined' ? {} : data;
jsPsych.data.write(data);
@ -885,6 +890,11 @@ window.jsPsych = (function() {
// reset the scroll on the DOM target
DOM_target.scrollTop = 0;
// add CSS classes to the DOM_target if they exist in trial.css_classes
if(typeof trial.css_classes !== 'undefined' && Array.isArray(trial.css_classes)){
DOM_target.classList.add(...trial.css_classes)
}
// execute trial method
jsPsych.plugins[trial.type].trial(DOM_target, trial);

View File

@ -0,0 +1,38 @@
const root = '../../';
const utils = require('../testing-utils.js');
beforeEach(function(){
require(root + 'jspsych.js');
require(root + 'plugins/jspsych-html-keyboard-response.js');
});
describe('The css_classes parameter for trials', function(){
test('Adds a single CSS class to the root jsPsych element', function(){
var trial = {
type: 'html-keyboard-response',
stimulus: '<p>foo</p>',
css_classes: ['foo']
}
jsPsych.init({timeline:[trial]});
expect(jsPsych.getDisplayElement().classList.contains('foo')).toBe(true);
utils.pressKey(32);
})
test('Removes the added classes at the end of the trial', function(){
var trial = {
type: 'html-keyboard-response',
stimulus: '<p>foo</p>',
css_classes: ['foo']
}
jsPsych.init({timeline:[trial]});
expect(jsPsych.getDisplayElement().classList.contains('foo')).toBe(true);
utils.pressKey(32);
expect(jsPsych.getDisplayElement().classList.contains('foo')).toBe(false);
})
})