56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { pageCountFor, parsePage } from "@/lib/pagination";
|
|
import { isValidLinkUrl } from "@/lib/validation";
|
|
|
|
describe("parsePage", () => {
|
|
it("parses plain positive integers", () => {
|
|
expect(parsePage("1")).toBe(1);
|
|
expect(parsePage("42")).toBe(42);
|
|
});
|
|
|
|
it("collapses missing values to page 1", () => {
|
|
expect(parsePage(undefined)).toBe(1);
|
|
expect(parsePage("")).toBe(1);
|
|
});
|
|
|
|
it("collapses malformed values to page 1", () => {
|
|
expect(parsePage("abc")).toBe(1);
|
|
expect(parsePage("-3")).toBe(1);
|
|
expect(parsePage("0")).toBe(1);
|
|
expect(parsePage("1.5")).toBe(1);
|
|
expect(parsePage("1e3")).toBe(1);
|
|
expect(parsePage("2abc")).toBe(1);
|
|
});
|
|
|
|
it("uses the first entry of repeated params", () => {
|
|
expect(parsePage(["3", "9"])).toBe(3);
|
|
});
|
|
|
|
it("caps absurdly large values", () => {
|
|
expect(parsePage("99999999999999")).toBe(100_000);
|
|
});
|
|
});
|
|
|
|
describe("pageCountFor", () => {
|
|
it("computes ceilings and never returns less than one page", () => {
|
|
expect(pageCountFor(0, 5)).toBe(1);
|
|
expect(pageCountFor(5, 5)).toBe(1);
|
|
expect(pageCountFor(6, 5)).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe("isValidLinkUrl", () => {
|
|
it("accepts http(s) URLs and site-relative paths", () => {
|
|
expect(isValidLinkUrl("https://example.com/x")).toBe(true);
|
|
expect(isValidLinkUrl("http://example.com")).toBe(true);
|
|
expect(isValidLinkUrl("/posts")).toBe(true);
|
|
});
|
|
|
|
it("rejects other schemes and protocol-relative URLs", () => {
|
|
expect(isValidLinkUrl("javascript:alert(1)")).toBe(false);
|
|
expect(isValidLinkUrl("ftp://example.com")).toBe(false);
|
|
expect(isValidLinkUrl("//evil.example")).toBe(false);
|
|
expect(isValidLinkUrl("not a url")).toBe(false);
|
|
});
|
|
});
|