From 6c5d6ce04667cc3be4d463794ee17c7519d9693d Mon Sep 17 00:00:00 2001 From: Adam Piontek Date: Thu, 4 Mar 2021 23:15:20 -0500 Subject: [PATCH] fixed errors found by tests; fixed tests --- .../property_live/form_component.html.leex | 1 + lib/bones73k_web/live/property_live/show.ex | 27 ++++-- .../live/property_live/show.html.leex | 3 +- test/bones73k_web/live/property_live_test.exs | 97 ++++++++++++++----- test/support/fixtures/accounts_fixtures.ex | 2 +- 5 files changed, 93 insertions(+), 37 deletions(-) diff --git a/lib/bones73k_web/live/property_live/form_component.html.leex b/lib/bones73k_web/live/property_live/form_component.html.leex index a25b1f82..582aa6f3 100644 --- a/lib/bones73k_web/live/property_live/form_component.html.leex +++ b/lib/bones73k_web/live/property_live/form_component.html.leex @@ -1,4 +1,5 @@ <%= form_for @changeset, "#", [ + id: "property-form", phx_target: @myself, phx_change: "validate", phx_submit: "save" diff --git a/lib/bones73k_web/live/property_live/show.ex b/lib/bones73k_web/live/property_live/show.ex index b611312c..d5809cde 100644 --- a/lib/bones73k_web/live/property_live/show.ex +++ b/lib/bones73k_web/live/property_live/show.ex @@ -17,18 +17,29 @@ defmodule Bones73kWeb.PropertyLive.Show do property = Properties.get_property!(id) if Roles.can?(current_user, property, live_action) do - {:noreply, - socket - |> assign(:property, property) - |> assign(:page_title, page_title(live_action))} + socket + |> assign(:property, property) + |> assign(:page_title, page_title(live_action)) + |> assign(:modal_return_to, Routes.property_show_path(socket, :show, property)) + |> live_noreply() else - {:noreply, - socket - |> put_flash(:error, "Unauthorised") - |> redirect(to: "/")} + socket + |> put_flash(:error, "Unauthorised") + |> redirect(to: "/") + |> live_noreply() end end + @impl true + def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do + socket |> copy_flash() |> push_patch(to: to) |> live_noreply() + end + + @impl true + def handle_info({:put_flash_message, {flash_type, msg}}, socket) do + socket |> put_flash(flash_type, msg) |> live_noreply() + end + defp page_title(:show), do: "Show Property" defp page_title(:edit), do: "Edit Property" end diff --git a/lib/bones73k_web/live/property_live/show.html.leex b/lib/bones73k_web/live/property_live/show.html.leex index e178fcc6..18a61458 100644 --- a/lib/bones73k_web/live/property_live/show.html.leex +++ b/lib/bones73k_web/live/property_live/show.html.leex @@ -5,8 +5,7 @@ id: @property.id, title: @page_title, action: @live_action, - property: @property, - return_to: Routes.property_show_path(@socket, :show, @property) %> + property: @property %> <% end %> diff --git a/test/bones73k_web/live/property_live_test.exs b/test/bones73k_web/live/property_live_test.exs index 64a4fc7d..f9dc9660 100644 --- a/test/bones73k_web/live/property_live_test.exs +++ b/test/bones73k_web/live/property_live_test.exs @@ -54,14 +54,23 @@ defmodule Bones73kWeb.PropertyLiveTest do |> form("#property-form", property: @invalid_attrs) |> render_change() =~ "can't be blank" - {:ok, _, html} = - index_live - |> form("#property-form", property: @create_attrs) - |> render_submit() - |> follow_redirect(conn, Routes.property_index_path(conn, :index)) + # update form attrs + index_live + |> form("#property-form", property: @update_attrs) + |> render_change() + # submit new form attrs + index_live + |> form("#property-form", property: @update_attrs) + |> render_submit() + + # send modal close event & observe results + send(index_live.pid, {:close_modal, true}) + html = render(index_live) + + assert_patched(index_live, Routes.property_index_path(conn, :index)) assert html =~ "Property created successfully" - assert html =~ "some description" + assert html =~ "some updated description" end test "updates property in listing", %{conn: conn, property: property} do @@ -76,12 +85,21 @@ defmodule Bones73kWeb.PropertyLiveTest do |> form("#property-form", property: @invalid_attrs) |> render_change() =~ "can't be blank" - {:ok, _, html} = - index_live - |> form("#property-form", property: @update_attrs) - |> render_submit() - |> follow_redirect(conn, Routes.property_index_path(conn, :index)) + # update form attrs + index_live + |> form("#property-form", property: @update_attrs) + |> render_change() + # submit new form attrs + index_live + |> form("#property-form", property: @update_attrs) + |> render_submit() + + # send modal close event & observe results + send(index_live.pid, {:close_modal, true}) + html = render(index_live) + + assert_patched(index_live, Routes.property_index_path(conn, :index)) assert html =~ "Property updated successfully" assert html =~ "some updated description" end @@ -130,12 +148,21 @@ defmodule Bones73kWeb.PropertyLiveTest do |> form("#property-form", property: @invalid_attrs) |> render_change() =~ "can't be blank" - {:ok, _, html} = - index_live - |> form("#property-form", property: @update_attrs) - |> render_submit() - |> follow_redirect(conn, Routes.property_index_path(conn, :index)) + # update form attrs + index_live + |> form("#property-form", property: @update_attrs) + |> render_change() + # submit new form attrs + index_live + |> form("#property-form", property: @update_attrs) + |> render_submit() + + # send modal close event & observe results + send(index_live.pid, {:close_modal, true}) + html = render(index_live) + + assert_patched(index_live, Routes.property_index_path(conn, :index)) assert html =~ "Property updated successfully" assert html =~ "some updated description" end @@ -257,12 +284,21 @@ defmodule Bones73kWeb.PropertyLiveTest do |> form("#property-form", property: @invalid_attrs) |> render_change() =~ "can't be blank" - {:ok, _, html} = - show_live - |> form("#property-form", property: @update_attrs) - |> render_submit() - |> follow_redirect(conn, Routes.property_show_path(conn, :show, property)) + # update form attrs + show_live + |> form("#property-form", property: @update_attrs) + |> render_change() + # submit new form attrs + show_live + |> form("#property-form", property: @update_attrs) + |> render_submit() + + # send modal close event & observe results + send(show_live.pid, {:close_modal, true}) + html = render(show_live) + + assert_patched(show_live, Routes.property_show_path(conn, :show, property)) assert html =~ "Property updated successfully" assert html =~ "some updated description" end @@ -293,12 +329,21 @@ defmodule Bones73kWeb.PropertyLiveTest do |> form("#property-form", property: @invalid_attrs) |> render_change() =~ "can't be blank" - {:ok, _, html} = - show_live - |> form("#property-form", property: @update_attrs) - |> render_submit() - |> follow_redirect(conn, Routes.property_show_path(conn, :show, property)) + # update form attrs + show_live + |> form("#property-form", property: @update_attrs) + |> render_change() + # submit new form attrs + show_live + |> form("#property-form", property: @update_attrs) + |> render_submit() + + # send modal close event & observe results + send(show_live.pid, {:close_modal, true}) + html = render(show_live) + + assert_patched(show_live, Routes.property_show_path(conn, :show, property)) assert html =~ "Property updated successfully" assert html =~ "some updated description" end diff --git a/test/support/fixtures/accounts_fixtures.ex b/test/support/fixtures/accounts_fixtures.ex index 7ba9082d..1983ee7f 100644 --- a/test/support/fixtures/accounts_fixtures.ex +++ b/test/support/fixtures/accounts_fixtures.ex @@ -36,7 +36,7 @@ defmodule Bones73k.AccountsFixtures do # {:ok, captured} = fun.(&"[TOKEN]#{&1}[TOKEN]") # [_, token, _] = String.split(captured.body, "[TOKEN]") # token - %Bamboo.Email{} = email = fun.(&"[TOKEN]#{&1}[TOKEN]") + {:ok, %Bamboo.Email{} = email} = fun.(&"[TOKEN]#{&1}[TOKEN]") [_, token, _] = String.split(email.text_body, "[TOKEN]") token end