CAITSGet started

Pages and routes

src/routes/ holds the pages of the app and its HTTP API. Marko Run, the router, finds them by their file names. A route only places what the contexts give: no logic, no test. The end-to-end tests check the pages in a browser.

The files

The routes of the example app

src/routes/
├── _frontend/                         the pages
│   ├── +layout.marko                  the frame of every page: the menu, the title, the app's stylesheet
│   ├── +page.marko                    the page /
│   ├── messages/+page.marko           the page /messages
│   ├── component/                     the components that only the pages use
│   └── style/                         the app's stylesheet, and its partials
└── _backend/                          the HTTP API
    └── api/messages/
        ├── +handler.ts                GET and POST /api/messages
        └── $id/+handler.ts            PUT and DELETE /api/messages/{id}
  • _frontend and _backend don't show in the addresses: Marko Run leaves out a folder whose name starts with _.
  • $id is a parameter of the address: the controller reads it from the request.
  • A frontend route imports only the frontend, with #frontend/; a backend route only the backend, with #backend/.

A page

A +page.marko places views of the contexts, with its own HTML around them, and imports its stylesheet. A page can place the views of several contexts:

src/routes/_frontend/messages/+page.marko

import MessageBoard from "#frontend/messages/presentation/view/message-board/MessageBoard.marko";
import "./messages-page.scss";

<main class="messages-page">
  <p class="eyebrow">The messages context</p>
  <h1>Example</h1>
  <p class="lead">Add, edit and remove messages. The backend keeps them in <code>tmp/messages.json</code>.</p>
  <MessageBoard/>
</main>
  • A page imports only views, and never calls frontendOf: each view takes the frontend of its own context.
  • A +layout.marko is the frame of the pages of its folder and below. The example's layout holds the menu and the title of each page, and imports the app's stylesheet: see Styles.
  • A component that only the pages use, such as the logo, lives in component/. Its logic lives in a context, with its test: the copy button of the example takes its copy from src/frontend/site/.

A route of the API

A +handler.ts exports one function per method, which hands the request to the controller of a use case. Run comes from Marko Run: no import needed.

src/routes/_backend/api/messages/$id/+handler.ts

import { controller } from "@ingenioz-it/caits/di/backend";
import { EditMessageHttpController } from "#backend/messages/command/edit-message/edit-message.http.controller.js";
import { RemoveMessageHttpController } from "#backend/messages/command/remove-message/remove-message.http.controller.js";

export const PUT = Run.PUT(async (ctx): Promise<Response> => controller(EditMessageHttpController).handle(ctx.request));

export const DELETE = Run.DELETE(async (ctx): Promise<Response> => controller(RemoveMessageHttpController).handle(ctx.request));

The tests of the controller send their requests through this route: see Test the controller.

A document

A document is a page in another format than HTML: a sitemap, a feed, llms.txt. Its route is a folder named after its file, between backticks, with a +handler.ts that gives the GET of a view:

```ts src/routes/_frontend/messages.txt/+handler.ts export { messagesText as GET } from "#frontend/messages/presentation/view/messages-text/messagesText.js";

The view makes the document from what its context knows, as a [loader](/docs/frontend-use-case#the-view) does:

```ts src/frontend/messages/presentation/view/messages-text/messagesText.ts
import { frontendOf } from "@ingenioz-it/caits/di/frontend";
import { settled } from "@ingenioz-it/caits/state/Loadable";
import { request } from "@ingenioz-it/caits/state/request";
import { MessagesRequested } from "#context/event/MessagesRequested.js";

/** GET /messages.txt: the messages, one a line. */
export async function messagesText($global: object): Promise<Response> {
  const messages = await request(frontendOf($global, "messages"), new MessagesRequested(), state => settled(state.messages));
  if (messages.status === "failed") return new Response(messages.message, { status: 503 });
  return new Response(messages.data.map(message => message.text).join("\n"), { headers: { "content-type": "text/plain; charset=utf-8" } });
}

Its test sits next to it, as for any view: it publishes the answer of the handler, then checks the DTO that the store sent, and the document.

src/frontend/messages/presentation/view/messages-text/messagesText.test.ts

test("asks for the messages, then gives them as text, one a line", async () => {
  const presentation = testPresentation("messages");
  const response = messagesText(globalWith({ messages: presentation }));

  presentation.events.publish(new MessagesLoaded([new Message("m1", "Hello"), new Message("m2", "World")]));

  expect(presentation.sent).toStrictEqual([new ListMessagesQuery()]);
  expect(await (await response).text()).toBe("Hello\nWorld");
});

Test the pages

The end-to-end tests run the pages in a real browser, with Playwright, on the production build. npm run test:e2e builds the app, empties its DATA_DIR, starts it on port 4310, then runs test/e2e/*.spec.ts.

  • Name each test Given …, When …, Then …: the report of the tests shows it as three steps.
  • The tests run one after another, on the same server: the data of a test stays for the next. Start each test from a known state.

test/e2e/messages.spec.ts

// Each test starts with no message: the server keeps the messages of the tests before.
test.beforeEach(async ({ request }) => {
  const { messages: kept } = (await (await request.get("/api/messages")).json()) as { messages: { id: string }[] };
  for (const { id } of kept) await request.delete(`/api/messages/${encodeURIComponent(id)}`);
});

test("Given a visitor on the messages page, When they add, edit and remove messages, Then the list shows each change, also after a reload", async ({ page }) => {
  await page.goto("/messages");
  await expect(page.getByText("No message yet.")).toBeVisible();
  // …
});

The rules of the routes check this page.