Skip to content

Commit c4448b6

Browse files
committed
Generate passing live tests when schema and table names are equal
When the live generator is used with a schema name that is the same as the table name, the generated tests should not fail. This change makes sure that the generated tests correctly target the collection item elements by using `schema.collection` instead of `schema.table`. The following generator calling variations should therefore work as intended: - same names, default pk name: `mix phx.gen.live Books Series series name:string` - different names, default pk name: `mix phx.gen.live Books Author authors name:string` - same names, custom pk name: `mix phx.gen.live Books Covers covers --primary-key covers_id front:text back:text` - different names, custom pk name: `mix phx.gen.live Books Reviewer reviewers --primary-key reviewer_id name:string` For context, before this change, calling the generator with the same word for schema name and table name (e.g. `mix phx.gen.live Books Series series name:string`) resulted in failing tests with errors like: ``` 1) test Index updates series in listing (ExampleWeb.SeriesLiveTest) test/example_web/live/series_live_test.exs:52 ** (ArgumentError) selector "#series_collection-c9129c62-f6b4-4749-854d-6d56f6a3b4b2 a" did not return any element within: <div id="phx-GGSpIya1xsOZigmM" data-phx-main="" data-phx-session="SFMyNTY.g2gDaAJhBnQAAAAIdwJpZG0AAAAUcGh4LUdHU3BJeWExeHNPWmlnbU13B3Nlc3Npb250AAAAAHcKcGFyZW50X3BpZHcDbmlsdwZ... ... ``` Reason being, that the stream used to generate the listing was named after the `schema.collection` which was in this case suffixed with `_collection` while the tests used the `schema.plural` value. For reference see: - <https://github.com/phoenixframework/phoenix/blob/v1.8.1/lib/mix/phoenix/schema.ex#L102> - <https://github.com/phoenixframework/phoenix/blob/v1.8.1/priv/templates/phx.gen.live/index.ex#L54>
1 parent 6bc3edc commit c4448b6

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

priv/templates/phx.gen.live/index.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
5050
{:ok,
5151
socket
5252
|> assign(:page_title, "Listing <%= schema.human_plural %>")<%= if primary_key != :id do %>
53-
|> stream_configure(:<%= schema.collection %>, dom_id: &"<%= schema.table %>-#{&1.<%= primary_key %>}")<% end %>
53+
|> stream_configure(:<%= schema.collection %>, dom_id: &"<%= schema.collection %>-#{&1.<%= primary_key %>}")<% end %>
5454
|> stream(:<%= schema.collection %>, list_<%= schema.plural %>(<%= socket_scope %>))}
5555
end
5656

priv/templates/phx.gen.live/live_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
6060

6161
assert {:ok, form_live, _html} =
6262
index_live
63-
|> element("#<%= schema.plural %>-#{<%= schema.singular %>.<%= primary_key %>} a", "Edit")
63+
|> element("#<%= schema.collection %>-#{<%= schema.singular %>.<%= primary_key %>} a", "Edit")
6464
|> render_click()
6565
|> follow_redirect(conn, ~p"<%= scope_param_route_prefix %><%= schema.route_prefix %>/#{<%= schema.singular %>}/edit")
6666
@@ -84,7 +84,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
8484
test "deletes <%= schema.singular %> in listing", %{conn: conn, <%= schema.singular %>: <%= schema.singular %><%= test_context_scope %>} do
8585
{:ok, index_live, _html} = live(conn, ~p"<%= scope_param_route_prefix %><%= schema.route_prefix %>")
8686

87-
assert index_live |> element("#<%= schema.plural %>-#{<%= schema.singular %>.<%= primary_key %>} a", "Delete") |> render_click()
87+
assert index_live |> element("#<%= schema.collection %>-#{<%= schema.singular %>.<%= primary_key %>} a", "Delete") |> render_click()
8888
refute has_element?(index_live, "#<%= schema.plural %>-#{<%= schema.singular %>.<%= primary_key %>}")
8989
end
9090
end

0 commit comments

Comments
 (0)