You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dianshang-qianduan/tests/training-toolbar-step-stora...

50 lines
2.4 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const { parse, compileScript } = require('@vue/compiler-sfc');
const babel = require('@babel/parser');
const descriptor = parse(fs.readFileSync(path.resolve(__dirname, '../src/views/components/TrainingTaskToolbar.vue'), 'utf8')).descriptor;
compileScript(descriptor, { id: 'toolbar-step-storage' });
const script = descriptor.scriptSetup.content;
const names = ['saveProgress', 'saveLegacyTraining', 'triggerExistingAction'];
const code = babel.parse(script, { sourceType: 'module' }).program.body
.filter(n => n.type === 'FunctionDeclaration' && names.includes(n.id.name)).map(n => script.slice(n.start, n.end)).join('\n');
const taskMap = fs.readFileSync(path.resolve(__dirname, '../src/views/training/taskKeyMap.js'), 'utf8');
const mapContext = {};
vm.runInNewContext(taskMap.replace(/export /g, '') + '\nglobalThis.counts = studentTrainingStepCountByTaskKey;', mapContext);
function context(taskKey, buttons = []) {
const warnings = [], writes = [];
const c = { studentTrainingStepCountByTaskKey: mapContext.counts, resolvedTaskKey: { value: taskKey }, savingProgress: { value: false },
props: {}, getCurrentTaskActionButtons: () => buttons, ElMessage: { warning: text => warnings.push(text), success() {} },
saveStudentTrainingAnswer: async (...args) => writes.push(args), setTimeout: callback => callback(), warnings, writes,
};
vm.createContext(c); vm.runInContext(code, c);
return c;
}
test('all 24 built-in tasks reject whole-page snapshot fallback for save and submit', async () => {
for (const key of Object.keys(mapContext.counts)) {
const c = context(key);
await c.saveProgress();
await c.saveLegacyTraining();
assert.equal(c.writes.length, 0, key);
assert.equal(c.warnings.length, 2, key);
assert.equal(c.savingProgress.value, false);
}
});
test('toolbar still delegates save and submit to the task page buttons', async () => {
let saved = 0, submitted = 0;
const c = context('target-user-profile', [
{ innerText: '保存', click: () => { saved++; } },
{ innerText: '提交任务', click: () => { submitted++; } },
]);
await c.saveProgress();
c.triggerExistingAction('submit');
assert.equal(saved, 1); assert.equal(submitted, 1);
assert.equal(c.writes.length, 0); assert.equal(c.warnings.length, 0);
});