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.
153 lines
6.8 KiB
JavaScript
153 lines
6.8 KiB
JavaScript
|
1 month ago
|
const assert = require('node:assert/strict');
|
||
|
|
const fs = require('node:fs');
|
||
|
|
const path = require('node:path');
|
||
|
|
const vm = require('node:vm');
|
||
|
|
const { parseDOM } = require('htmlparser2');
|
||
|
|
const css = require('css-select');
|
||
|
|
|
||
|
|
// Use the installed HTML parser/selector engine to exercise the directive without a browser.
|
||
|
|
// Only DOM event, animation-frame, and inline-style primitives are stubbed.
|
||
|
|
const frames = new Map();
|
||
|
|
const observers = [];
|
||
|
|
let frameId = 0;
|
||
|
|
const context = {
|
||
|
|
requestAnimationFrame(fn) { frames.set(++frameId, fn); return frameId; },
|
||
|
|
cancelAnimationFrame(id) { frames.delete(id); },
|
||
|
|
MutationObserver: class {
|
||
|
|
constructor(fn) { this.fn = fn; observers.push(this); }
|
||
|
|
observe() {}
|
||
|
|
disconnect() { this.disconnected = true; }
|
||
|
|
},
|
||
|
|
};
|
||
|
|
vm.runInNewContext(fs.readFileSync(path.join(__dirname, '../src/directive/common/trainingFillHints.js'), 'utf8')
|
||
|
|
.replace('export default', 'globalThis.directive ='), context);
|
||
|
|
const directive = context.directive;
|
||
|
|
const flush = () => { for (const [id, fn] of frames) { frames.delete(id); fn(); } };
|
||
|
|
function document(html) {
|
||
|
|
const doc = parseDOM(html);
|
||
|
|
for (const element of css.selectAll('*', doc)) {
|
||
|
|
element.tagName = element.name.toUpperCase();
|
||
|
|
element.value = element.attribs.value || '';
|
||
|
|
Object.defineProperty(element, 'readOnly', { get: () => 'readonly' in element.attribs });
|
||
|
|
const properties = new Map();
|
||
|
|
element.style = {
|
||
|
|
getPropertyValue: (key) => properties.get(key)?.[0] || '',
|
||
|
|
getPropertyPriority: (key) => properties.get(key)?.[1] || '',
|
||
|
|
setProperty: (key, value, priority = '') => properties.set(key, [value, priority]),
|
||
|
|
removeProperty: (key) => properties.delete(key),
|
||
|
|
};
|
||
|
|
element.getAttribute = (key) => element.attribs[key] ?? null;
|
||
|
|
element.setAttribute = (key, value) => { element.attribs[key] = value; };
|
||
|
|
element.removeAttribute = (key) => { delete element.attribs[key]; };
|
||
|
|
element.matches = (selector) => css.is(element, selector);
|
||
|
|
element.querySelectorAll = (selector) => css.selectAll(selector, element);
|
||
|
|
element.querySelector = (selector) => css.selectOne(selector, element);
|
||
|
|
element.closest = (selector) => {
|
||
|
|
for (let current = element; current?.type === 'tag'; current = current.parent) {
|
||
|
|
if (css.is(current, selector)) return current;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
};
|
||
|
|
const listeners = new Map();
|
||
|
|
element.addEventListener = (type, fn) => listeners.set(type, fn);
|
||
|
|
element.removeEventListener = (type) => listeners.delete(type);
|
||
|
|
element.emit = (type) => listeners.get(type)?.();
|
||
|
|
}
|
||
|
|
return { doc, get: (id) => css.selectOne(`#${id}`, doc) };
|
||
|
|
}
|
||
|
|
const { get } = document(`<div id="root">
|
||
|
|
<textarea id="empty"></textarea><input id="space" value=" ">
|
||
|
|
<div class="el-input"><div class="el-input__wrapper" id="wrapper"><input id="zero" type="number" value="0"></div></div>
|
||
|
|
<input id="readonly" readonly><input id="disabled" disabled><input id="upload" type="file"><input id="hidden" type="hidden">
|
||
|
|
<select id="native-select"></select>
|
||
|
|
<div class="el-select"><div class="el-input__wrapper" id="select-wrapper"><input id="select" class="el-input__inner" readonly></div></div>
|
||
|
|
<div class="el-radio-group" id="radio"><input id="radio-answer" type="radio"></div>
|
||
|
|
<div class="el-checkbox-group" id="checkbox"><input id="checkbox-answer" type="checkbox"></div>
|
||
|
|
<div class="training-ai-sidebar"><textarea id="ai"></textarea></div>
|
||
|
|
<div class="training-excel-tools"><input id="excel"></div>
|
||
|
|
<div id="nested"><textarea id="nested-answer"></textarea></div>
|
||
|
|
</div>`);
|
||
|
|
const root = get('root');
|
||
|
|
const status = (id) => get(id).getAttribute('data-training-fill-state');
|
||
|
|
const background = (id) => get(id).style.getPropertyValue('background-color');
|
||
|
|
get('empty').style.setProperty('background-color', '#fafafa');
|
||
|
|
directive.mounted(get('nested'));
|
||
|
|
directive.mounted(root);
|
||
|
|
flush();
|
||
|
|
assert.equal(status('empty'), 'empty');
|
||
|
|
assert.equal(status('space'), 'empty');
|
||
|
|
assert.equal(status('zero'), 'filled');
|
||
|
|
assert.equal(status('wrapper'), 'filled');
|
||
|
|
assert.match(background('empty'), /#fff7d6/);
|
||
|
|
assert.match(background('zero'), /#ffffff/);
|
||
|
|
assert.equal(get('empty').style.getPropertyPriority('background-color'), 'important');
|
||
|
|
for (const id of ['readonly', 'disabled', 'upload', 'hidden', 'ai', 'excel']) assert.equal(status(id), null, id);
|
||
|
|
assert.equal(status('native-select'), 'empty');
|
||
|
|
assert.equal(status('select'), 'empty');
|
||
|
|
assert.equal(status('select-wrapper'), 'empty');
|
||
|
|
assert.equal(status('radio'), 'empty');
|
||
|
|
assert.equal(status('checkbox'), 'empty');
|
||
|
|
assert.equal(status('nested-answer'), 'empty');
|
||
|
|
|
||
|
|
get('empty').value = '学生填写的项目背景';
|
||
|
|
root.emit('input');
|
||
|
|
flush();
|
||
|
|
assert.equal(status('empty'), 'filled');
|
||
|
|
assert.match(background('empty'), /#ffffff/);
|
||
|
|
get('empty').value = '';
|
||
|
|
directive.updated(root);
|
||
|
|
flush();
|
||
|
|
assert.equal(status('empty'), 'empty');
|
||
|
|
get('empty').value = '异步加载的已保存内容';
|
||
|
|
get('select').value = '风险低';
|
||
|
|
get('native-select').value = 'chosen';
|
||
|
|
get('radio-answer').setAttribute('checked', '');
|
||
|
|
get('checkbox-answer').setAttribute('checked', '');
|
||
|
|
directive.updated(root);
|
||
|
|
flush();
|
||
|
|
for (const id of ['empty', 'select', 'native-select', 'radio', 'checkbox']) assert.equal(status(id), 'filled', id);
|
||
|
|
get('select').value = '';
|
||
|
|
get('radio-answer').removeAttribute('checked');
|
||
|
|
root.emit('change');
|
||
|
|
flush();
|
||
|
|
assert.equal(status('select'), 'empty');
|
||
|
|
assert.equal(status('radio'), 'empty');
|
||
|
|
get('empty').setAttribute('disabled', '');
|
||
|
|
observers.at(-1).fn();
|
||
|
|
flush();
|
||
|
|
assert.equal(status('empty'), null);
|
||
|
|
assert.equal(background('empty'), '#fafafa');
|
||
|
|
get('empty').removeAttribute('disabled');
|
||
|
|
observers.at(-1).fn();
|
||
|
|
flush();
|
||
|
|
assert.equal(status('empty'), 'filled');
|
||
|
|
|
||
|
|
// A newly rendered row must be discovered by the subtree observer.
|
||
|
|
const added = document('<input id="added">').get('added');
|
||
|
|
added.parent = root;
|
||
|
|
root.children.push(added);
|
||
|
|
observers.at(-1).fn();
|
||
|
|
flush();
|
||
|
|
assert.equal(added.getAttribute('data-training-fill-state'), 'empty');
|
||
|
|
added.value = 'Excel 导入内容';
|
||
|
|
directive.updated(root);
|
||
|
|
flush();
|
||
|
|
assert.equal(added.getAttribute('data-training-fill-state'), 'filled');
|
||
|
|
|
||
|
|
const preview = document('<div class="student-task-readonly-preview"><div id="preview"><textarea id="answer"></textarea></div></div>');
|
||
|
|
directive.mounted(preview.get('preview'));
|
||
|
|
flush();
|
||
|
|
assert.equal(preview.get('answer').getAttribute('data-training-fill-state'), null);
|
||
|
|
|
||
|
|
root.emit('input');
|
||
|
|
directive.beforeUnmount(root);
|
||
|
|
flush();
|
||
|
|
assert.equal(frames.size, 0);
|
||
|
|
assert.equal(status('empty'), null);
|
||
|
|
assert.equal(background('empty'), '#fafafa');
|
||
|
|
assert.equal(status('nested-answer'), 'empty');
|
||
|
|
assert.equal(observers.at(-1).disconnected, true);
|
||
|
|
directive.beforeUnmount(get('nested'));
|
||
|
|
assert.equal(status('nested-answer'), null);
|
||
|
|
console.log('Training fill hints passed: empty/filled/cleared, whitespace, zero, select, choices, exclusions, async restore, dynamic rows, preview, nested scope, cleanup.');
|