60 lines
2.3 KiB
JavaScript
60 lines
2.3 KiB
JavaScript
// Drive the generated viewer's logic under a stub DOM and assert the toggle.
|
|
const fs = require('fs');
|
|
const html = fs.readFileSync(process.argv[2] || '/tmp/minisite/viewer.html', 'utf8');
|
|
const script = html.split('<script>')[1].split('</script>')[0];
|
|
|
|
const handlers = {};
|
|
const listen = (t, f) => { (handlers[t] = handlers[t] || []).push(f); };
|
|
function node(extra) {
|
|
return Object.assign({
|
|
style: {}, classList: { add() {}, remove() {} },
|
|
addEventListener: listen,
|
|
getBoundingClientRect: () => ({ left: 0, top: 0, width: 1000, height: 800 }),
|
|
}, extra || {});
|
|
}
|
|
|
|
const imgStub = node({ naturalWidth: 2000, naturalHeight: 1000, src: '' });
|
|
const pctStub = node({ textContent: '' });
|
|
const modeStub = node({ textContent: '' });
|
|
const ids = { img: imgStub, container: node(), pct: pctStub, mode: modeStub };
|
|
|
|
global.document = { getElementById: (i) => ids[i], title: '' };
|
|
global.window = { innerWidth: 1000, innerHeight: 800, addEventListener: listen };
|
|
global.location = { search: '?src=graph.svg', href: '' };
|
|
|
|
new Function(script)();
|
|
imgStub.onload();
|
|
|
|
const fire = (t, e) => (handlers[t] || []).forEach((f) => f(e));
|
|
let ok = true;
|
|
const check = (name, cond) => {
|
|
console.log(` ${cond ? 'ok ' : 'FAIL'} ${name}`);
|
|
if (!cond) ok = false;
|
|
};
|
|
const click = (x, y) => {
|
|
fire('mousedown', { button: 0, clientX: x, clientY: y, preventDefault() {} });
|
|
fire('mouseup', { button: 0, clientX: x, clientY: y });
|
|
};
|
|
|
|
check('fits on load, below 100%', modeStub.textContent === 'fit' && parseInt(pctStub.textContent) < 100);
|
|
const fitPct = pctStub.textContent;
|
|
|
|
click(500, 400);
|
|
check('a click toggles to 1:1', modeStub.textContent === '1:1' && pctStub.textContent === '100%');
|
|
|
|
click(500, 400);
|
|
check('clicking again returns to fit', modeStub.textContent === 'fit' && pctStub.textContent === fitPct);
|
|
|
|
fire('mousedown', { button: 0, clientX: 100, clientY: 100, preventDefault() {} });
|
|
fire('mousemove', { clientX: 260, clientY: 180 });
|
|
fire('mouseup', { button: 0, clientX: 260, clientY: 180 });
|
|
check('a drag pans and does NOT toggle', modeStub.textContent === 'fit');
|
|
|
|
fire('wheel', { deltaY: -1, clientX: 500, clientY: 400, preventDefault() {} });
|
|
check('the wheel zooms in past fit', parseInt(pctStub.textContent) > parseInt(fitPct));
|
|
|
|
fire('dblclick', {});
|
|
check('double-click resets to fit', modeStub.textContent === 'fit');
|
|
|
|
process.exit(ok ? 0 : 1);
|