|
| 1 | +defmodule DemoWeb.Live.PreferencesPersistenceTest do |
| 2 | + @moduledoc """ |
| 3 | + End-to-end coverage for the `persist: [:order, :filters, :columns]` option on |
| 4 | + `Backpex.LiveResource`. Mounts `DemoWeb.PostLive` (configured with all three |
| 5 | + persistence kinds) and asserts that sort, filter, and column-toggle |
| 6 | + interactions emit a `push_event` with the expected preference key and value |
| 7 | + shape. |
| 8 | +
|
| 9 | + The wire event name comes from `Backpex.Preferences.LiveView.event_name/0` |
| 10 | + and the keys come from `Backpex.Preferences.Keys.{order,filters,columns}/1`, |
| 11 | + so the test reflects the same contract the emitter uses. |
| 12 | + """ |
| 13 | + |
| 14 | + use DemoWeb.ConnCase, async: false |
| 15 | + |
| 16 | + import Demo.EctoFactory |
| 17 | + import Phoenix.LiveViewTest |
| 18 | + |
| 19 | + alias Backpex.Preferences.Keys, as: PrefKeys |
| 20 | + alias Backpex.Preferences.LiveView, as: PrefLiveView |
| 21 | + |
| 22 | + @resource_mod DemoWeb.PostLive |
| 23 | + |
| 24 | + # assert_push_event expands to assert_receive, which pattern-matches the |
| 25 | + # arguments. Bind the event name and key to module-level constants or local |
| 26 | + # variables before the macro call so the pattern is literal-shaped. |
| 27 | + @event_name PrefLiveView.event_name() |
| 28 | + |
| 29 | + describe "persist: [:order]" do |
| 30 | + test "sort change via column-header click emits push_event with order key", %{conn: conn} do |
| 31 | + insert(:post, title: "Alpha", published: true) |
| 32 | + insert(:post, title: "Beta", published: true) |
| 33 | + |
| 34 | + {:ok, view, _html} = live(conn, ~p"/admin/posts?filters[published][]=published") |
| 35 | + |
| 36 | + # Click the Title column header — triggers a sort and routes through |
| 37 | + # maybe_persist_order/2 which fires the push_event. |
| 38 | + view |
| 39 | + |> element("a", "Title") |
| 40 | + |> render_click() |
| 41 | + |
| 42 | + expected_key = PrefKeys.order(@resource_mod) |
| 43 | + |
| 44 | + assert_push_event(view, @event_name, %{ |
| 45 | + key: ^expected_key, |
| 46 | + value: %{"by" => "title", "direction" => "asc"} |
| 47 | + }) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + describe "persist: [:filters]" do |
| 52 | + test "filter change emits push_event with filters key", %{conn: conn} do |
| 53 | + insert(:post, title: "Published", published: true) |
| 54 | + insert(:post, title: "Draft", published: false) |
| 55 | + |
| 56 | + # Mount with the default published-only filter applied. |
| 57 | + {:ok, view, _html} = live(conn, ~p"/admin/posts?filters[published][]=published") |
| 58 | + |
| 59 | + # Toggle the filter to include not_published too — routes through |
| 60 | + # handle_params → maybe_persist_filters/2 → push_event. |
| 61 | + view |
| 62 | + |> form("form[phx-change='change-filter']", |
| 63 | + filters: %{published: ["published", "not_published"]} |
| 64 | + ) |
| 65 | + |> render_change() |
| 66 | + |
| 67 | + expected_key = PrefKeys.filters(@resource_mod) |
| 68 | + |
| 69 | + # The LiveResource emits several filter-persistence events over the mount |
| 70 | + # + change cycle. We care that at least one of them reflects the new |
| 71 | + # two-value set and carries the filters key. |
| 72 | + assert_push_event(view, @event_name, %{ |
| 73 | + key: ^expected_key, |
| 74 | + value: %{"published" => ["published", "not_published"]} |
| 75 | + }) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + describe "persist: [:columns]" do |
| 80 | + test "column toggle emits push_event with columns key", %{conn: conn} do |
| 81 | + insert(:post, title: "Alpha", published: true) |
| 82 | + |
| 83 | + {:ok, view, _html} = live(conn, ~p"/admin/posts?filters[published][]=published") |
| 84 | + |
| 85 | + # Toggle the "title" column off. maybe_push_columns/3 emits the |
| 86 | + # push_event with the full active-fields map. |
| 87 | + view |
| 88 | + |> element("input[phx-click='toggle_column'][phx-value-field='title']") |
| 89 | + |> render_click() |
| 90 | + |
| 91 | + expected_key = PrefKeys.columns(@resource_mod) |
| 92 | + |
| 93 | + assert_push_event(view, @event_name, %{key: ^expected_key, value: value}) |
| 94 | + |
| 95 | + # title was just toggled, so it must now be false; other fields remain true. |
| 96 | + assert is_map(value) |
| 97 | + assert value["title"] == false |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments