Every chapter so far ended with a test, and every one of those tests used the same type. In this chapter we'll look at that type directly: how it reads a response, which assertions it offers, and the two checks it makes that are easy to miss.
This chapter assumes Chapters 1 to 4. It does not cover reloading a page or loading deferred props from a test.
Reading a page from a response
The test asserted the component name Invoices/Show and passed for a month. The file on disk was Invoice/Show.tsx. The handler agreed with the test, the test agreed with the handler, and neither had been introduced to the frontend.
Sometimes you want to assert on what a handler sent without starting a browser. Thankfully, the crate ships a type for that. Take a look at Example 5-1.
Example 5-1. Component, values and absences
AssertablePage::from_body(&reply.body)
.component("Invoices/Show")
.equals("invoiceId", 7)
.equals("auth.user", "Mary")
.missing("note");
Let's walk through it. from_body reads the page from a response body, whether that body is the JSON of an Inertia visit or the HTML document of a first visit. component checks the page's component name. equals and missing take a prop path in dot notation, so auth.user reaches inside the auth prop. Each assertion panics with a message when it fails, which is what fails the test.
Two checks that are easy to miss
You can also ask a scope to account for every key. Inside scope, the closure must assert on each key of the object or call etc() to allow the rest (see Example 5-2).
Example 5-2. A scope that tolerates nothing extra
page.scope("invoice", |invoice| invoice.equals("id", 7));
Run against a page whose invoice prop is {"id":7,"secret":"x"}, Example 5-2 panics and names secret. That is the check that notices a field you didn't mean to send.
The second check is for the file. Give the page your pages directory with pages_in, and component also asserts that a page file exists for that name, with one of six extensions: js, jsx, svelte, ts, tsx or vue (see Example 5-3).
Example 5-3. The component must exist on disk
AssertablePage::from_body(&reply.body)
.pages_in("resources/js/Pages")
.component("Invoices/Show");
If you're unsure where to start, I'd put pages_in in the one helper every test uses to build its page, so no test can skip it.
What this chapter leaves open
So the handler, the test and the file now agree on a name. What none of them checks is whether Show.tsx reads invoiceId or invoice_id. Chapter 1 generated a type for that. Is the type imported by the component? What fails, and when, if it isn't?
Testing
This chapter's listings are themselves tests. Examples 5-2 and 5-3 are run as tests that are expected to panic, one on the unasserted key and one on an empty pages directory.
TL;DR
AssertablePage::from_body reads a page from JSON or from a first visit's HTML. component, equals, missing, flash and missing_flash assert on it, with dot paths into props. A scope must assert on every key unless etc() is called. With pages_in, component also requires a page file on disk.