jsPsych/tests/plugins/plugin-cloze.test.js
2018-08-05 12:25:34 +02:00

140 lines
3.5 KiB
JavaScript

const utils = require('../testing-utils.js');
const root = '../../';
jest.useFakeTimers();
describe('cloze', function(){
beforeEach(function(){
require(root + 'jspsych.js');
require(root + 'plugins/jspsych-cloze');
});
test('loads correctly', function(){
expect(typeof window.jsPsych.plugins['cloze']).not.toBe('undefined');
});
test('displays cloze', function(){
var trial = {
type: 'cloze',
text: 'This is a %cloze% text.',
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch('<div class="cloze">This is a <input type="text" id="input0" value=""> text.</div>');
});
test('displays default button text', function(){
var trial = {
type: 'cloze',
text: 'This is a %cloze% text.'
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch(new RegExp('<button class="jspsych-html-button-response-button" type="button" id="finish_cloze_button">OK</button>'));
});
test('displays custom button text', function(){
var trial = {
type: 'cloze',
text: 'This is a %cloze% text.',
button_text: 'Next'
}
jsPsych.init({
timeline: [trial]
});
expect(jsPsych.getDisplayElement().innerHTML).toMatch(new RegExp('<button class="jspsych-html-button-response-button" type="button" id="finish_cloze_button">Next</button>'));
});
test('ends trial on button click when using default settings, i.e. answers are not checked', function(){
var trial = {
type: 'cloze',
text: 'This is a %cloze% text.'
}
jsPsych.init({
timeline: [trial]
});
utils.clickTarget(document.querySelector('#finish_cloze_button'));
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
test('ends trial on button click when answers are checked and correct', function(){
var trial = {
type: 'cloze',
text: 'This is a %cloze% text.',
check_answers: true
}
jsPsych.init({
timeline: [trial]
});
document.getElementById('input0').value = 'cloze';
utils.clickTarget(document.querySelector('#finish_cloze_button'));
expect(jsPsych.getDisplayElement().innerHTML).toBe('');
});
test('does not end trial on button click when answers are checked and not correct', function(){
var trial = {
type: 'cloze',
text: 'This is a %cloze% text.',
check_answers: true
}
jsPsych.init({
timeline: [trial]
});
document.getElementById('input0').value = 'some wrong answer';
utils.clickTarget(document.querySelector('#finish_cloze_button'));
expect(jsPsych.getDisplayElement().innerHTML).not.toBe('');
});
test('does not call mistake function on button click when answers are checked and correct', function(){
var trial = {
type: 'cloze',
text: 'This is a %cloze% text.',
check_answers: true,
mistake_fn: function(){called = true}
}
jsPsych.init({
timeline: [trial]
});
var called = false;
document.getElementById('input0').value = 'cloze';
utils.clickTarget(document.querySelector('#finish_cloze_button'));
expect(called).not.toBeTruthy();
});
test('calls mistake function on button click when answers are checked and not correct', function(){
var trial = {
type: 'cloze',
text: 'This is a %cloze% text.',
check_answers: true,
mistake_fn: function(){called = true}
}
jsPsych.init({
timeline: [trial]
});
var called = false;
document.getElementById('input0').value = 'some wrong answer';
utils.clickTarget(document.querySelector('#finish_cloze_button'));
expect(called).toBeTruthy();
});
});