Files
wekan/tests/domainTablePage.test.cjs
T
Lauri Ojansivu b7589737f5 File docs/Features the way the WeKan menus are.
A feature's guide now sits under the menu it is used from, read from the
templates rather than from where somebody once put it: Header-Bar,
All-Boards, Pages, Board-Header (search and every Board View), Board-Sidebar
(Board Settings, Filter, Members, Labels, Archive), Rules, Swimlanes, Lists,
Cards, Member-Settings, Admin-Panel and Login-Page. Topics used from several
menus (editor, history, theme, translations, email, troubleshooting) are in
General.

- Each area has a README mapping its menu entries to their guides and to the
  template or server module that implements them. Board-Header/README.md
  lists all 40 Board View entries with the template boardBody.jade renders;
  five views (Table, Calendar Mode, Time, Timeline, Map) have no guide of
  their own yet and say so.
- Board-View.md listed 25 views while BOARD_VIEWS has 40; it now lists all
  of them with their English labels.
- Pages keep their file names. Where a released changelog links an old path,
  a "# Moved" page stays there and points to the new one; test-menu.sh skips
  folders holding only those.
- Undo was filed under Login; it is General/Undo now.
- Every link in docs, code comments, scripts and tests follows the move;
  docsLinksResolve checks them all.

Tests that read a doc follow it to its new place. boardViewSettings now fails
when Board-View.md or Board-Header/README.md differs from BOARD_VIEWS, or
names a template boardBody.jade does not render.

Thanks to xet7 !
2026-10-16 23:45:35 +03:00

111 lines
5.0 KiB
JavaScript

'use strict';
// Plain-Node unit test (no Meteor) for the Admin Panel Domains table paging.
// Run: node tests/domainTablePage.test.cjs
//
// The Domains table now behaves like the Board Table view: only one small page
// is sent to the browser, ordered by a column and filtered by a search term.
// paginateDomains() is the server-side search + sort + slice that backs it.
const assert = require('assert');
const { paginateDomains } = require('../models/lib/domainTablePage');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
const rows = [
{ domain: 'gmail.com', count: 5 },
{ domain: 'example.com', count: 12 },
{ domain: 'apple.com', count: 3 },
{ domain: 'zeta.org', count: 12 },
{ domain: 'beta.io', count: 1 },
];
const names = r => r.rows.map(x => x.domain);
// --- Pagination (only a page is returned) -----------------------------------
test('returns only one page and the correct total', () => {
const r = paginateDomains(rows, { perPage: 2, page: 1, sortField: 'domain', sortDirection: 1 });
assert.strictEqual(r.total, 5);
assert.strictEqual(r.totalPages, 3);
assert.strictEqual(r.rows.length, 2);
});
test('page 2 returns the next slice; page beyond range clamps to the last page', () => {
// sorted asc: apple.com, beta.io, example.com, gmail.com, zeta.org
const p2 = paginateDomains(rows, { perPage: 2, page: 2, sortField: 'domain', sortDirection: 1 });
assert.deepStrictEqual(names(p2), ['example.com', 'gmail.com']);
const clamped = paginateDomains(rows, { perPage: 2, page: 99, sortField: 'domain', sortDirection: 1 });
assert.strictEqual(clamped.page, 3);
assert.strictEqual(clamped.rows.length, 1);
});
// --- Column sorting ----------------------------------------------------------
test('sort by domain ascending / descending', () => {
const asc = paginateDomains(rows, { perPage: 25, sortField: 'domain', sortDirection: 1 });
assert.deepStrictEqual(names(asc), ['apple.com', 'beta.io', 'example.com', 'gmail.com', 'zeta.org']);
const desc = paginateDomains(rows, { perPage: 25, sortField: 'domain', sortDirection: -1 });
assert.deepStrictEqual(names(desc), ['zeta.org', 'gmail.com', 'example.com', 'beta.io', 'apple.com']);
});
test('sort by count descending, tie-broken by domain name', () => {
const r = paginateDomains(rows, { perPage: 25, sortField: 'count', sortDirection: -1 });
// 12/12 tie -> example.com before zeta.org, then 5, 3, 1
assert.deepStrictEqual(names(r), ['example.com', 'zeta.org', 'gmail.com', 'apple.com', 'beta.io']);
});
test('an unknown sortField falls back to sorting by domain', () => {
const r = paginateDomains(rows, { perPage: 25, sortField: 'bogus', sortDirection: 1 });
assert.deepStrictEqual(names(r), ['apple.com', 'beta.io', 'example.com', 'gmail.com', 'zeta.org']);
});
// --- Search ------------------------------------------------------------------
test('search filters by domain substring (case-insensitive) and adjusts total', () => {
const r = paginateDomains(rows, { perPage: 25, search: 'COM', sortField: 'domain', sortDirection: 1 });
assert.deepStrictEqual(names(r), ['apple.com', 'example.com', 'gmail.com']);
assert.strictEqual(r.total, 3);
});
// --- NEGATIVE / edges --------------------------------------------------------
test('a search with no matches returns an empty page and total 0', () => {
const r = paginateDomains(rows, { perPage: 25, search: 'no-such-domain' });
assert.deepStrictEqual(r.rows, []);
assert.strictEqual(r.total, 0);
assert.strictEqual(r.totalPages, 1);
});
test('non-array / empty input is handled without throwing', () => {
assert.deepStrictEqual(paginateDomains(undefined).rows, []);
assert.deepStrictEqual(paginateDomains(null).rows, []);
const empty = paginateDomains([]);
assert.strictEqual(empty.total, 0);
assert.strictEqual(empty.page, 1);
});
test('perPage is clamped to a sane range (>=1, <=200)', () => {
// The default is the app's ONE rows-per-page (10, TABLE_PAGE_ROWS_PER_PAGE in
// models/lib/tablePage.js and docs/Features/General/Table.md) - it used to be 25
// here alone, which is exactly the drift that setting exists to stop.
// models/lib/tablePage.js is an ES module, so read the constant out of it
// rather than require() it - the same thing tests/tablePage.test.cjs does.
const fs = require('fs');
const path = require('path');
const src = fs.readFileSync(path.join(__dirname, '..', 'models/lib/tablePage.js'), 'utf8');
const TABLE_PAGE_ROWS_PER_PAGE = Number(
src.match(/TABLE_PAGE_ROWS_PER_PAGE\s*=\s*(\d+)/)[1]);
assert.strictEqual(TABLE_PAGE_ROWS_PER_PAGE, 10, 'the app-wide rows per page');
assert.strictEqual(paginateDomains(rows, { perPage: 0 }).perPage, TABLE_PAGE_ROWS_PER_PAGE);
assert.strictEqual(paginateDomains(rows, { perPage: 9999 }).perPage, 200);
assert.strictEqual(paginateDomains(rows, { perPage: -5 }).perPage, TABLE_PAGE_ROWS_PER_PAGE);
});
test('page is never below 1', () => {
const r = paginateDomains(rows, { perPage: 2, page: -3 });
assert.strictEqual(r.page, 1);
});
console.log(`\n${passed} tests passed`);