Skip to content

Commit 6c43090

Browse files
committed
Add Tests for the Events Index Page
... and also add the current search terms to the query string. Now one can link to searches.
1 parent 14f3af1 commit 6c43090

2 files changed

Lines changed: 129 additions & 5 deletions

File tree

lib/dev_round_web/live/event_live/index.ex

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ defmodule DevRoundWeb.EventLive.Index do
1818
end
1919

2020
@impl true
21-
def handle_params(_params, _url, socket) do
22-
{:noreply, socket}
21+
def handle_params(params, _url, socket) do
22+
query = Map.get(params, "query", "")
23+
search_form = to_form(%{"query" => query})
24+
search_results = search_for_events(query)
25+
26+
{:noreply,
27+
assign(socket,
28+
search_form: search_form,
29+
search_results: search_results
30+
)}
2331
end
2432

2533
@impl true
26-
def handle_event("search", %{"query" => query} = search_params, socket) do
27-
{:noreply,
28-
assign(socket, form: to_form(search_params), search_results: search_for_events(query))}
34+
def handle_event("search", %{"query" => query}, socket) do
35+
socket = push_patch(socket, to: "/events?#{URI.encode_query(%{query: query})}")
36+
{:noreply, socket}
2937
end
3038

3139
defp search_for_events("" = _query), do: nil
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)