Testing a Page
elements man tests/pagesA page test renders the page's template on the server and checks the HTML. It runs in the build like every other test, inside the transaction that rolls back, so rows it inserts are gone when it ends.
Render the template
A template is a class. Construct it with the attributes the route would pass,
and toHtml() returns the page as the server sends it:
app/pages/home/test.tsimport { test, assert } from "@elements/app"; import html from "./template"; test("home", () => { test("shows the title", () => { let page = new html().toHtml(); assert(page.includes("Welcome"), "no title"); }); });
Check for text, a class or an attribute with includes, and order with
indexOf. Do not compare the whole string: the HTML also contains the markers the
browser runtime attaches to, and a test should not depend on them.
A page over the database
Insert the rows the test needs with sql, then build the attributes the way
the route does. A LiveTable is declared at the top of the file, assigned to
a name: the compiler takes the table name from that variable name, so a LiveTable
constructed inline has no table name and throws.
app/pages/home/test.tsimport { test, assert, sql, LiveTable } from "@elements/app"; import { Todo } from "./models"; import html from "./template"; const todos = new LiveTable<Todo>(); test("home", () => { test("lists what is in the table", () => { sql(`insert into todos (title) values ('Water the plants')`); let page = new html({ todos: todos.view(), view: { filter: "all" }, }).toHtml(); assert(page.includes("Water the plants"), "row not rendered"); }); });
What a page test covers
It covers what the server renders: which rows show, in what order, the empty
states, and what each attribute changes in the HTML. It does not run the browser, so
it does not cover handlers, clicks or live updates. Keep the logic a handler runs
in plain functions (a models.ts beside the template, as the todo recipe
does) and test those by calling them. Check handlers, clicks and live updates in the browser.
Related
elements man recipes/todo-app: a page test for a live list.elements man html: templates and their attributes.