40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
import { describe, it, expect, beforeEach } from "vitest";
|
|
import { initPageJump } from "../scripts/init-page-jump.mjs";
|
|
|
|
function mountPagination(totalPages = 8, current = 1) {
|
|
document.body.innerHTML = `
|
|
<div id="pagination-top" class="pagination-controls">
|
|
<span class="jump-to-page">
|
|
<input type="number" class="jump-input" min="1" max="${totalPages}" value="${current}">
|
|
<button type="button" class="jump-btn">Go</button>
|
|
</span>
|
|
</div>`;
|
|
}
|
|
|
|
describe("page jump binder", () => {
|
|
beforeEach(() => {
|
|
document.body.innerHTML = "";
|
|
window.location.hash = "";
|
|
});
|
|
|
|
it("clicking Go updates hash to input value (clamped)", () => {
|
|
mountPagination(8, 1);
|
|
initPageJump();
|
|
const input = document.querySelector(".jump-input");
|
|
const btn = document.querySelector(".jump-btn");
|
|
input.value = "5";
|
|
btn.click();
|
|
expect(window.location.hash.includes("page=5")).toBe(true);
|
|
});
|
|
|
|
it("clamps values below min to 1", () => {
|
|
mountPagination(8, 1);
|
|
initPageJump();
|
|
const input = document.querySelector(".jump-input");
|
|
const btn = document.querySelector(".jump-btn");
|
|
input.value = "0";
|
|
btn.click();
|
|
expect(window.location.hash.includes("page=1")).toBe(true);
|
|
});
|
|
});
|