|
| 1 | +defmodule DevRoundWeb.EventLive.IndexTest do |
| 2 | + use DevRoundWeb.ConnCase |
| 3 | + import Phoenix.LiveViewTest |
| 4 | + import DevRound.EventsFixtures |
| 5 | + import DevRound.AccountsFixtures |
| 6 | + |
| 7 | + setup %{conn: conn} do |
| 8 | + user = user_fixture() |
| 9 | + lang = lang_fixture() |
| 10 | + now = NaiveDateTime.truncate(NaiveDateTime.local_now(), :second) |
| 11 | + |
| 12 | + # Current published event |
| 13 | + current_event = |
| 14 | + event_fixture(%{ |
| 15 | + title: "Current Event", |
| 16 | + put_langs: [lang], |
| 17 | + begin_local: NaiveDateTime.shift(now, day: 2), |
| 18 | + end_local: NaiveDateTime.shift(now, day: 2, hour: 2) |
| 19 | + }) |
| 20 | + |
| 21 | + # Archived published event |
| 22 | + archived_event = |
| 23 | + event_fixture(%{ |
| 24 | + title: "Archived Event", |
| 25 | + put_langs: [lang], |
| 26 | + begin_local: NaiveDateTime.shift(now, day: -5), |
| 27 | + end_local: NaiveDateTime.shift(now, day: -5, hour: 2), |
| 28 | + registration_deadline_local: NaiveDateTime.shift(now, day: -6) |
| 29 | + }) |
| 30 | + |
| 31 | + # Unpublished event (should never appear) |
| 32 | + unpublished_event = |
| 33 | + event_fixture(%{ |
| 34 | + title: "Hidden Event", |
| 35 | + put_langs: [lang], |
| 36 | + published: false, |
| 37 | + begin_local: NaiveDateTime.shift(now, day: 3) |
| 38 | + }) |
| 39 | + |
| 40 | + # Matching published event for search |
| 41 | + matching_event = |
| 42 | + event_fixture(%{ |
| 43 | + title: "Elixir Summit", |
| 44 | + put_langs: [lang] |
| 45 | + }) |
| 46 | + |
| 47 | + # Non‑matching published event for search |
| 48 | + other_event = |
| 49 | + event_fixture(%{ |
| 50 | + title: "Phoenix Conference", |
| 51 | + put_langs: [lang] |
| 52 | + }) |
| 53 | + |
| 54 | + conn = log_in_user(conn, user) |
| 55 | + |
| 56 | + %{ |
| 57 | + conn: conn, |
| 58 | + current_event: current_event, |
| 59 | + archived_event: archived_event, |
| 60 | + unpublished_event: unpublished_event, |
| 61 | + matching_event: matching_event, |
| 62 | + other_event: other_event, |
| 63 | + user: user, |
| 64 | + lang: lang |
| 65 | + } |
| 66 | + end |
| 67 | + |
| 68 | + test "redirects if user is not logged in", %{} do |
| 69 | + conn = build_conn() |
| 70 | + assert {:error, {:redirect, %{to: "/users/log_in"}}} = live(conn, ~p"/events") |
| 71 | + end |
| 72 | + |
| 73 | + test "search query in URL shows only matching published results", %{ |
| 74 | + conn: conn, |
| 75 | + matching_event: matching_event, |
| 76 | + other_event: other_event, |
| 77 | + unpublished_event: unpublished_event |
| 78 | + } do |
| 79 | + {:ok, _view, html} = live(conn, ~p"/events?query=Elixir") |
| 80 | + assert html =~ "Search Results" |
| 81 | + assert html =~ matching_event.title |
| 82 | + refute html =~ other_event.title |
| 83 | + refute html =~ unpublished_event.title |
| 84 | + end |
| 85 | + |
| 86 | + test "search form changes update URL and display only matching published results", %{ |
| 87 | + conn: conn, |
| 88 | + matching_event: matching_event, |
| 89 | + other_event: other_event, |
| 90 | + unpublished_event: unpublished_event |
| 91 | + } do |
| 92 | + {:ok, view, _html} = live(conn, ~p"/events") |
| 93 | + |
| 94 | + view |
| 95 | + |> form("#search-from", %{"query" => "Elixir"}) |
| 96 | + |> render_change() |
| 97 | + |
| 98 | + assert_patch(view, "/events?query=Elixir") |
| 99 | + html = render(view) |
| 100 | + assert html =~ matching_event.title |
| 101 | + refute html =~ other_event.title |
| 102 | + refute html =~ unpublished_event.title |
| 103 | + end |
| 104 | + |
| 105 | + test "page shows current and archived events, excludes unpublished", %{ |
| 106 | + conn: conn, |
| 107 | + current_event: current_event, |
| 108 | + archived_event: archived_event, |
| 109 | + unpublished_event: unpublished_event |
| 110 | + } do |
| 111 | + {:ok, _view, html} = live(conn, ~p"/events") |
| 112 | + assert html =~ current_event.title |
| 113 | + assert html =~ archived_event.title |
| 114 | + refute html =~ unpublished_event.title |
| 115 | + end |
| 116 | +end |
0 commit comments