1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3import assert from 'node:assert/strict';
4import { readFileSync } from 'node:fs';
5import { test } from 'node:test';
6import { runInNewContext } from 'node:vm';
7import { el, installDom } from './dom.mjs';
8
9const source = readFileSync(new URL('../static/js/ermite.js', import.meta.url), 'utf8');
10
11test('main navigation ignores rail rows, but still drives main tables', () => {
12 const doc = installDom();
13 const main = el('main', { id: 'main' });
14 const recent = el('tr');
15 const rail = el('aside', { cls: 'rail' }, el('tbody', { attrs: { 'data-rows': '' } }, recent));
16 doc.body.append(main, rail);
17 const scrolls = [];
18 main.scrollBy = (_, y) => scrolls.push(['main-by', y]);
19 main.scrollTo = (_, y) => scrolls.push(['main-to', y]);
20 const context = {
21 window: { ermiteRail: { step: () => false } },
22 $: (s) => doc.querySelector(s),
23 $$: (s) => {
24 const child = s.startsWith('[data-rows] > ') ? s.slice('[data-rows] > '.length) : null;
25 if (child) return [...doc.querySelectorAll('[data-rows]')].flatMap((table) => table.children.filter((row) => row.matches(child)));
26 return [...doc.querySelectorAll(s)];
27 },
28 body: () => doc.body,
29 flash() {}, setTimeout() {},
30 scrollBy: (_, y) => scrolls.push(['page-by', y]),
31 scrollTo: (_, y) => scrolls.push(['page-to', y]),
32 };
33 runInNewContext(source.slice(source.indexOf(' const visibleRows ='), source.indexOf(' window.ermite = ermite;')) + '\nwindow.ermite = ermite;', context);
34 const E = context.window.ermite;
35 E.move(1); E.move(-1); E.edge(false); E.edge(true);
36 assert.deepEqual(scrolls, [['page-by', 180], ['main-by', 180], ['page-by', -180], ['main-by', -180], ['page-to', 0], ['main-to', 0], ['page-to', 1e9], ['main-to', 1e9]]);
37 assert.equal(recent.hasAttribute('data-cursor'), false);
38 const first = el('tr');
39 const last = el('tr');
40 main.append(el('tbody', { attrs: { 'data-rows': '' } }, first, last));
41 E.move(1);
42 assert.equal(first.hasAttribute('data-cursor'), true);
43 E.edge(true);
44 assert.equal(last.hasAttribute('data-cursor'), true);
45 assert.equal(first.hasAttribute('data-cursor'), false);
46 assert.equal(recent.hasAttribute('data-cursor'), false);
47});