phoenixframework / phoenix_live_view

Rich, real-time user experiences with server-rendered HTML
https://hex.pm/packages/phoenix_live_view
MIT License
6.12k stars 917 forks source link

Support for `assert_flash`? #642

Closed cadebward closed 4 years ago

cadebward commented 4 years ago

I know this is technically a feature request 🙈 , but would it make sense to have an assert_flash or maybe support for get_flash for live flash?

I've been looking through the code and I'm not 100% sure if its even possible in its current form. If it is, I'd be happy to submit a PR. Just thought I'd toss this out for discussion first.

Examples:

test "shows a flash when email is bad", %{conn: conn} do
  {:ok, view, _html} = live(conn, Routes.live_path(@endpoint, SomeModule))
  params = %{"form" => %{"email" => "bad"}}
  render_submit(view, "save", params)
  assert_flash(view_or_conn_or_something, %{"error" => "That email is soooo bad!"})
end

The alternative would be something like:

assert html =~ "That email is sooo bad!"

Which isn't the worst thing in the world. I think a helper like assert_flash or get_flash would really help the LiveView testing story (which is already amazing).

chrismccord commented 4 years ago

asserting on the HTML content is the way to go, like anything other content you care about asserting on the page. You could look into a library like Floki to use selector-based tests if you wanted to specifically test the content of say #flash-info.