Files
vue-journal-layer/tests/pagination.test.ts
Jakub Zych a4dca42d77 test(16-05): Journal layer util tests
- node --test over routes, client (fake fetcher, include_empty=1),
  pagination, htmlGate (Go FormatHTML parity cases), sitemap, seo and
  pl/en i18n parity
- package script test and a Testing line in the README
2026-10-09 20:03:46 +02:00

48 lines
1.7 KiB
TypeScript

// ?page=N parsing, out-of-range detection and the page window.
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { pageOutOfRange, paginationWindow, parsePageParam } from '../app/utils/pagination.ts'
test('a missing page is page 1', () => {
assert.equal(parsePageParam(undefined), 1)
assert.equal(parsePageParam(null), 1)
})
test('a string of digits above 0 is that page', () => {
assert.equal(parsePageParam('1'), 1)
assert.equal(parsePageParam('2'), 2)
assert.equal(parsePageParam('017'), 17)
})
test('anything else is a 404 (null)', () => {
for (const raw of ['0', '-1', '1.5', 'abc', '', ' 2', '2 ', '1e3', '0x2', '99999999999999999999']) {
assert.equal(parsePageParam(raw), null, JSON.stringify(raw))
}
assert.equal(parsePageParam(['2']), null)
assert.equal(parsePageParam(2), null)
})
test('pageOutOfRange: past the last page only, page 1 never', () => {
assert.equal(pageOutOfRange(3, 2), true)
assert.equal(pageOutOfRange(2, 2), false)
assert.equal(pageOutOfRange(1, 0), false)
assert.equal(pageOutOfRange(1, 1), false)
assert.equal(pageOutOfRange(2, 0), true)
})
test('paginationWindow centres on the current page', () => {
assert.deepEqual(paginationWindow(5, 10), [3, 4, 5, 6, 7])
})
test('paginationWindow stays full width near either end', () => {
assert.deepEqual(paginationWindow(1, 10), [1, 2, 3, 4, 5])
assert.deepEqual(paginationWindow(10, 10), [6, 7, 8, 9, 10])
assert.deepEqual(paginationWindow(9, 10, 3), [8, 9, 10])
})
test('paginationWindow clips to the page count and handles empty input', () => {
assert.deepEqual(paginationWindow(1, 2), [1, 2])
assert.deepEqual(paginationWindow(1, 0), [])
assert.deepEqual(paginationWindow(1, 5, 0), [])
})