Skip to content

Commit 83a162c

Browse files
committed
Added tests for autoconfigure modal
1 parent ec26b82 commit 83a162c

3 files changed

Lines changed: 122 additions & 57 deletions

File tree

lib/plausible_web/live/goal_settings/form.ex

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
3030
assigns[:goal_type] || "custom_events"
3131
end
3232

33-
event_name_options_count = length(assigns.event_name_options)
33+
event_name_options_count = length(assigns.event_name_options || [])
3434

3535
show_autoconfigure_modal =
36-
if Map.has_key?(socket.assigns, :show_autoconfigure_modal) do
37-
socket.assigns.show_autoconfigure_modal
36+
if event_name_options_count == 0 do
37+
false
3838
else
39-
is_nil(assigns.goal) &&
40-
form_type == "custom_events" &&
41-
event_name_options_count > 0
39+
if Map.has_key?(socket.assigns, :show_autoconfigure_modal) do
40+
socket.assigns.show_autoconfigure_modal
41+
else
42+
is_nil(assigns.goal) &&
43+
form_type == "custom_events" &&
44+
event_name_options_count > 0
45+
end
4246
end
4347

4448
socket =
@@ -122,7 +126,9 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
122126
~H"""
123127
<div>
124128
<.title>
125-
We detected {@event_name_options_count} custom {if @event_name_options_count == 1, do: "event", else: "events"}.
129+
We detected {@event_name_options_count} custom {if @event_name_options_count == 1,
130+
do: "event",
131+
else: "events"}.
126132
</.title>
127133
128134
<p class="mt-2 py-2 text-sm text-gray-600 dark:text-gray-400 text-pretty">
@@ -142,7 +148,9 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
142148
phx-target={@myself}
143149
>
144150
<Heroicons.plus class="size-4" />
145-
Add {@event_name_options_count} {if @event_name_options_count == 1, do: "event", else: "events"}
151+
Add {@event_name_options_count} {if @event_name_options_count == 1,
152+
do: "event",
153+
else: "events"}
146154
</.button>
147155
</div>
148156
</div>
@@ -197,7 +205,8 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
197205
~H"""
198206
<div id="pageviews-form" class="py-2" {@rest}>
199207
<div class="text-sm pb-6 text-gray-600 dark:text-gray-400 text-pretty">
200-
Pageview goals allow you to measure how many people visit a specific page or section of your site. <.styled_link
208+
Pageview goals allow you to measure how many people visit a specific page or section of your site.
209+
<.styled_link
201210
href="https://plausible.io/docs/pageview-goals"
202211
new_tab={true}
203212
>
@@ -275,7 +284,8 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
275284
~H"""
276285
<div id="scroll-form" class="py-2" x-data={@js} {@rest}>
277286
<div class="text-sm pb-6 text-gray-500 dark:text-gray-400 text-justify rounded-md">
278-
Scroll Depth goals allow you to see how many people scroll beyond your desired scroll depth percentage threshold. <.styled_link
287+
Scroll Depth goals allow you to see how many people scroll beyond your desired scroll depth percentage threshold.
288+
<.styled_link
279289
href="https://plausible.io/docs/scroll-depth"
280290
new_tab={true}
281291
>

test/plausible_web/live/goal_settings/form_test.exs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,15 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
405405

406406
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
407407

408+
# If autoconfigure modal shows, click "Add manually" to get to the regular form
409+
html = render(lv)
410+
411+
if html =~ "We detected" do
412+
lv
413+
|> element("button[phx-click='add-manually']")
414+
|> render_click()
415+
end
416+
408417
type_into_combo(lv, "event_name_input_modalseq0", "One")
409418
html = render(lv)
410419

@@ -436,6 +445,99 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
436445
end
437446
end
438447

448+
describe "Autoconfigure modal" do
449+
setup [:create_user, :log_in, :create_site]
450+
451+
test "shows autoconfigure modal when opening custom events modal with available events", %{
452+
conn: conn,
453+
site: site
454+
} do
455+
populate_stats(site, [
456+
build(:event, name: "Signup"),
457+
build(:event, name: "Newsletter Signup"),
458+
build(:event, name: "Purchase")
459+
])
460+
461+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
462+
463+
html = render(lv)
464+
assert html =~ "We detected 3 custom"
465+
assert html =~ "These events have been sent from your site in the past 6 months"
466+
end
467+
468+
test "clicking 'Add manually' shows the regular form", %{conn: conn, site: site} do
469+
populate_stats(site, [
470+
build(:event, name: "Signup"),
471+
build(:event, name: "Newsletter Signup"),
472+
build(:event, name: "Purchase")
473+
])
474+
475+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
476+
477+
html = render(lv)
478+
assert html =~ "We detected 3 custom"
479+
480+
lv
481+
|> element("button[phx-click='add-manually']")
482+
|> render_click()
483+
484+
html = render(lv)
485+
refute html =~ "We detected"
486+
assert html =~ "Add goal for"
487+
end
488+
489+
test "autoconfigure button adds all events", %{conn: conn, site: site} do
490+
populate_stats(site, [
491+
build(:event, name: "Signup"),
492+
build(:event, name: "Newsletter Signup"),
493+
build(:event, name: "Purchase")
494+
])
495+
496+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
497+
498+
lv
499+
|> element("button[phx-click='autoconfigure']")
500+
|> render_click()
501+
502+
# Render again to process the async :autoconfigure message
503+
_html = render(lv)
504+
505+
goals = Plausible.Goals.for_site(site)
506+
assert length(goals) == 3
507+
assert Enum.any?(goals, &(&1.event_name == "Signup"))
508+
assert Enum.any?(goals, &(&1.event_name == "Newsletter Signup"))
509+
assert Enum.any?(goals, &(&1.event_name == "Purchase"))
510+
end
511+
512+
test "autoconfigure modal does not show when all events are already goals", %{
513+
conn: conn,
514+
site: site
515+
} do
516+
populate_stats(site, [
517+
build(:event, name: "Signup"),
518+
build(:event, name: "Newsletter Signup"),
519+
build(:event, name: "Purchase")
520+
])
521+
522+
lv = get_liveview(conn, site)
523+
524+
lv = open_modal_with_goal_type(lv, "custom_events")
525+
526+
lv
527+
|> element("button[phx-click='autoconfigure']")
528+
|> render_click()
529+
530+
# Render again to process the async :autoconfigure message and modal close event
531+
_html = render(lv)
532+
533+
lv = open_modal_with_goal_type(lv, "custom_events")
534+
535+
html = render(lv)
536+
refute html =~ "We detected"
537+
refute html =~ "from the last 6 months"
538+
end
539+
end
540+
439541
defp type_into_combo(lv, id, text) do
440542
lv
441543
|> element("input##{id}")

test/plausible_web/live/goal_settings_test.exs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -246,53 +246,6 @@ defmodule PlausibleWeb.Live.GoalSettingsTest do
246246

247247
refute html =~ "No goals found for this site. Please refine or"
248248
end
249-
250-
test "auto-configuring custom event goals", %{conn: conn, site: site} do
251-
populate_stats(site, [
252-
build(:event, name: "Signup"),
253-
build(:event, name: "Newsletter Signup"),
254-
build(:event, name: "Purchase")
255-
])
256-
257-
autoconfigure_button_selector = ~s/button[phx-click="autoconfigure"]/
258-
259-
assert_suggested_event_name_count = fn html, number ->
260-
assert text_of_element(html, autoconfigure_button_selector) =~
261-
"found #{number} custom events from the last 6 months that are not yet configured as goals"
262-
end
263-
264-
{lv, html} = get_liveview(conn, site, with_html?: true)
265-
266-
# At first, 3 event names are suggested
267-
assert_suggested_event_name_count.(html, 3)
268-
269-
# Add one goal
270-
lv
271-
|> element("#goals-form-modal form")
272-
|> render_submit(%{goal: %{event_name: "Signup"}})
273-
274-
html = render(lv)
275-
276-
# Now two goals are suggested because one is already added
277-
assert_suggested_event_name_count.(html, 2)
278-
279-
# Delete the goal
280-
goal = Plausible.Repo.get_by(Plausible.Goal, site_id: site.id, event_name: "Signup")
281-
html = lv |> element(~s/button#delete-goal-#{goal.id}/) |> render_click()
282-
283-
# Suggested event name count should be 3 again
284-
assert_suggested_event_name_count.(html, 3)
285-
286-
# Autoconfigure all custom event goals
287-
lv
288-
|> element(autoconfigure_button_selector)
289-
|> render_click()
290-
291-
html = render(lv)
292-
293-
# All possible goals exist - no suggestions anymore
294-
refute html =~ "from the last 6 months"
295-
end
296249
end
297250

298251
on_ee do

0 commit comments

Comments
 (0)