Skip to content

Commit

Permalink
add a unit test for a promise in question preamble
Browse files Browse the repository at this point in the history
  • Loading branch information
christianp committed Jan 30, 2024
1 parent ee9c7b6 commit f89facf
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/parts/part-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,48 @@ Numbas.queueScript('part_tests',['qunit','json','jme','localisation','parts/numb
done();
});

QUnit.test('Promise in question preamble', async function(assert) {
var done = assert.async();
assert.expect(4);
var question_def = {
name: 'question',
parts: [
{
type: 'numberentry',
marks: 1,
minvalue: '1',
maxvalue: '1'
}
],
preamble: {
js: `
return new Promise(resolve => {
setTimeout(() => {
question.scope.setVariable('a', Numbas.jme.wrapValue(1));
resolve()
}, 1);
});`
},
variables: {
b: {
name: "b",
definition: "a+2",
description: "2 more than a",
templateType: "anything"
}
}
};
var question = Numbas.createQuestionFromJSON(question_def);
assert.notOk(question.scope.getVariable('a'));
await question.signals.on('preambleRun');
assert.ok(question.scope.getVariable('a'));
assert.notOk(question.scope.getVariable('b'));
question.generateVariables();
await question.signals.on('variablesGenerated');
assert.equal(question.scope.getVariable('b').value, 3);
done();
});

QUnit.module('Explore mode');
question_test(
'One next part',
Expand Down Expand Up @@ -1739,6 +1781,7 @@ next_actions:

done();
});

QUnit.test('Question signals - explore mode', async function(assert) {
var question_def = {
name: 'question',
Expand Down

0 comments on commit f89facf

Please sign in to comment.