Skip to content

Commit 5a9b057

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. We use the `schema.collection` by default and use `schema.table` when the primary key is not named `:id`. The following generator calling variations should therefore work as intended: - same names, default pk name: `mix phx.gen.live Bibliography Series series name:string` - different names, default pk name: `mix phx.gen.live Bibliography Author authors name:string` - same names, custom pk name: `mix phx.gen.live Bibliography Buyer buyer name:string --primary-key buyer_id` - different names, custom pk name: `mix phx.gen.live Bibliography Seller sellers name:string --primary-key seller_id` For context, before this change, calling the generator with the same word for schema name and table name (e.g. `mix phx.gen.live Bibliography Series series name:string`) resulted in failing tests with errors like: ``` 1) test Index updates buyer in listing (ExampleWeb.BuyerLiveTest) test/example_web/live/buyer_live_test.exs:52 ** (ArgumentError) selector "#buyer_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 5a9b057

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

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("#<%= if(primary_key != :id, do: schema.table, else: 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("#<%= if(primary_key != :id, do: schema.table, else: 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)