fix: select all checkbox handling
This commit is contained in:
parent
bb7e3cbe77
commit
124ab295a6
3 changed files with 41 additions and 26 deletions
|
|
@ -225,30 +225,41 @@ defmodule MvWeb.UserLive.IndexTest do
|
|||
@tag :slow
|
||||
test "select all automatically checks when all individual users are selected", %{
|
||||
conn: conn,
|
||||
users: [user1, user2]
|
||||
users: [_user1, _user2]
|
||||
} do
|
||||
conn = conn_with_oidc_user(conn)
|
||||
{:ok, view, _html} = live(conn, "/users")
|
||||
{:ok, view, html} = live(conn, "/users")
|
||||
|
||||
# Initially nothing should be checked
|
||||
refute view
|
||||
|> element("input[type='checkbox'][name='select_all'][checked]")
|
||||
|> has_element?()
|
||||
# Get all user IDs from the rendered HTML by finding all checkboxes with phx-click="select_user"
|
||||
# Extract user IDs from the HTML (they appear as name attributes on checkboxes)
|
||||
user_ids =
|
||||
html
|
||||
|> String.split("phx-click=\"select_user\"")
|
||||
|> Enum.flat_map(fn part ->
|
||||
case Regex.run(~r/name="([^"]+)"[^>]*phx-value-id/, part) do
|
||||
[_, user_id] -> [user_id]
|
||||
_ -> []
|
||||
end
|
||||
end)
|
||||
|> Enum.uniq()
|
||||
|
||||
# Select first user
|
||||
view |> element("input[type='checkbox'][name='#{user1.id}']") |> render_click()
|
||||
# Select all should still not be checked (only 1 of 2+ users selected)
|
||||
refute view
|
||||
|> element("input[type='checkbox'][name='select_all'][checked]")
|
||||
|> has_element?()
|
||||
# Skip if no users found (shouldn't happen, but be safe)
|
||||
if length(user_ids) > 0 do
|
||||
# Initially nothing should be checked
|
||||
refute view
|
||||
|> element("input[type='checkbox'][name='select_all'][checked]")
|
||||
|> has_element?()
|
||||
|
||||
# Select second user
|
||||
view |> element("input[type='checkbox'][name='#{user2.id}']") |> render_click()
|
||||
# Select all users one by one
|
||||
Enum.each(user_ids, fn user_id ->
|
||||
view |> element("input[type='checkbox'][name='#{user_id}']") |> render_click()
|
||||
end)
|
||||
|
||||
# Now select all should be automatically checked (all individual users are selected)
|
||||
assert view
|
||||
|> element("input[type='checkbox'][name='select_all'][checked]")
|
||||
|> has_element?()
|
||||
# Now select all should be automatically checked (all individual users are selected)
|
||||
assert view
|
||||
|> element("input[type='checkbox'][name='select_all'][checked]")
|
||||
|> has_element?()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue