Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller Testing Documentation #63

Open
garlandcrow opened this issue Sep 4, 2020 · 1 comment
Open

Controller Testing Documentation #63

garlandcrow opened this issue Sep 4, 2020 · 1 comment

Comments

@garlandcrow
Copy link

I've starting using this in a new project I am working on and during controller testing I made a helper that I add to my conn_case.ex that maybe other people would find useful? I can add some documentation as a PR if this is something anyone would want. If not, just putting this here in case it helps someone.

def inertia_response(conn, status \\ 200) do
    response = Phoenix.ConnTest.html_response(conn, status)

    case Regex.run(~r/data-page=\"(.+?)\"/, response) do
      [_, injected_data] ->
        injected_data |> String.replace(""", "\"") |> Jason.decode!()

      _ ->
        nil
    end
  end

Then you can use it a test like:

conn = get(conn, Routes.customer_quotations_path(conn, :index))
response = inertia_response(conn, 200)

Which gives you a map of the inertia injected data on the page:

%{
  "component" => "QuotationList",
  "props" => %{
    "quotations" => [
      %{
        "completed" => false,
        "id" => 1,
        "status" => "awaiting_quotation",
        "updated_at" => "2020-09-04T02:02:14"
      }
    ]
  },
  "url" => "/quotations",
  "version" => "1"
}

This can be useful to assert the correct component is being rendered, or assert about the props being injected into the component.

@tmartin8080
Copy link
Collaborator

@garlandcrow This is great. Would you mind doing a PR with some docs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants