View Source Inertia.Testing (Inertia v3.0.0-rc2)

Helpers for testing Inertia responses.

Summary

Functions

Fetches the Inertia component (if applicable) for the current request.

Fetches the deferred prop groups for the current request.

Fetches the Inertia errors (if applicable) for the current request.

Fetches the Inertia flash data (if applicable) for the current request.

Fetches the merge prop keys for the current request.

Fetches the once prop metadata for the current request.

Fetches the full Inertia page object map for the current request.

Fetches the Inertia props (if applicable) for the current request.

Fetches the scroll prop metadata for the current request.

Fetches the shared prop keys (if applicable) for the current request.

Functions

inertia_component(conn)

@spec inertia_component(Plug.Conn.t()) :: String.t() | nil

Fetches the Inertia component (if applicable) for the current request.

Example

use MyAppWeb.ConnCase

import Inertia.Testing

describe "GET /" do
  test "renders the home page", %{conn: conn} do
    conn = get("/")
    assert inertia_component(conn) == "Home"
  end
end

inertia_deferred_props(conn)

(since 3.0.0)
@spec inertia_deferred_props(Plug.Conn.t()) :: map()

Fetches the deferred prop groups for the current request.

Returns a map of group name to list of deferred prop paths.

inertia_errors(conn)

(since 2.4.0)
@spec inertia_errors(Plug.Conn.t()) :: map()

Fetches the Inertia errors (if applicable) for the current request.

If there are errors available in the current page props, they will be returned. Otherwise, errors that have been stored in the session will be retrieved.

Example

use MyAppWeb.ConnCase

import Inertia.Testing

describe "POST /users" do
  test "fails when name empty", %{conn: conn} do
    conn = post("/users", %{"name" => ""})

    assert %{user: %{id: 1}} = inertia_props(conn)
    assert redirected_to(conn) == ~p"/users"
    assert inertia_errors(conn) == %{"name" => "can't be blank"}
  end
end

inertia_flash(conn)

(since 3.0.0)
@spec inertia_flash(Plug.Conn.t()) :: map()

Fetches the Inertia flash data (if applicable) for the current request.

Returns the flash map from the top-level page object, or falls back to the conn flash assigns.

Example

use MyAppWeb.ConnCase

import Inertia.Testing

describe "PUT /" do
  test "flashes a success message", %{conn: conn} do
    conn = put("/")
    assert %{"info" => "Updated"} = inertia_flash(conn)
  end
end

inertia_merge_props(conn)

(since 3.0.0)
@spec inertia_merge_props(Plug.Conn.t()) :: [String.t()]

Fetches the merge prop keys for the current request.

Returns the list of prop paths that are configured for client-side merging.

inertia_once_props(conn)

(since 3.0.0)
@spec inertia_once_props(Plug.Conn.t()) :: map()

Fetches the once prop metadata for the current request.

Returns a map of once-prop keys to their metadata (prop path and expiration).

inertia_page(conn)

(since 3.0.0)
@spec inertia_page(Plug.Conn.t()) :: map() | nil

Fetches the full Inertia page object map for the current request.

Returns the complete page data including component, props, url, version, and any metadata like mergeProps, deferredProps, etc.

inertia_props(conn)

@spec inertia_props(Plug.Conn.t()) :: map() | nil

Fetches the Inertia props (if applicable) for the current request.

Example

use MyAppWeb.ConnCase

import Inertia.Testing

describe "GET /" do
  test "renders the home page", %{conn: conn} do
    conn = get("/")
    assert %{user: %{id: 1}} = inertia_props(conn)
  end
end

inertia_scroll_props(conn)

(since 3.0.0)
@spec inertia_scroll_props(Plug.Conn.t()) :: map()

Fetches the scroll prop metadata for the current request.

Returns a map of prop paths to their scroll pagination metadata.

inertia_shared_props(conn)

(since 3.0.0)
@spec inertia_shared_props(Plug.Conn.t()) :: [String.t()]

Fetches the shared prop keys (if applicable) for the current request.

Returns the list of string keys that were marked as shared props.

Example

use MyAppWeb.ConnCase

import Inertia.Testing

describe "GET /" do
  test "includes shared props", %{conn: conn} do
    conn = get("/")
    assert "currentUser" in inertia_shared_props(conn)
  end
end