Skip to content

Commit 4eeca5a

Browse files
committed
Add sequence tests for check/uncheck on array named checkboxes
Since we just introduced more logic for unchecked boxes, I wanted to a few more tests that verify that the check->uncheck and uncheck->check sequences correctly track cumulative state.
1 parent dd79690 commit 4eeca5a

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

test/phoenix_test/live_test.exs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,31 @@ defmodule PhoenixTest.LiveTest do
674674
|> assert_has("#form-data", text: "three")
675675
end
676676

677+
test "can re-check an array named checkbox after unchecking it on change", %{conn: conn} do
678+
conn
679+
|> visit("/live/index")
680+
|> within("#array-checkbox-form", fn session ->
681+
session
682+
|> uncheck("One")
683+
|> check("One")
684+
end)
685+
|> assert_has("#form-data", text: "one")
686+
|> assert_has("#form-data", text: "two")
687+
end
688+
689+
test "can re-check an array named checkbox after unchecking it on submit", %{conn: conn} do
690+
conn
691+
|> visit("/live/index")
692+
|> within("#array-checkbox-form", fn session ->
693+
session
694+
|> uncheck("One")
695+
|> check("One")
696+
|> submit()
697+
end)
698+
|> assert_has("#form-data", text: "one")
699+
|> assert_has("#form-data", text: "two")
700+
end
701+
677702
test "check triggers phx-change on the input if it is defined", %{conn: conn} do
678703
conn
679704
|> visit("/live/index")
@@ -817,6 +842,33 @@ defmodule PhoenixTest.LiveTest do
817842
|> assert_has("#form-data", text: "two")
818843
end
819844

845+
test "can uncheck an array named checkbox after checking it on change", %{conn: conn} do
846+
conn
847+
|> visit("/live/index")
848+
|> within("#array-checkbox-form", fn session ->
849+
session
850+
|> check("Three")
851+
|> uncheck("Three")
852+
end)
853+
|> refute_has("#form-data", text: "three")
854+
|> assert_has("#form-data", text: "one")
855+
|> assert_has("#form-data", text: "two")
856+
end
857+
858+
test "can uncheck an array named checkbox after checking it on submit", %{conn: conn} do
859+
conn
860+
|> visit("/live/index")
861+
|> within("#array-checkbox-form", fn session ->
862+
session
863+
|> check("Three")
864+
|> uncheck("Three")
865+
|> submit()
866+
end)
867+
|> refute_has("#form-data", text: "three")
868+
|> assert_has("#form-data", text: "one")
869+
|> assert_has("#form-data", text: "two")
870+
end
871+
820872
test "raises error if checkbox doesn't have phx-click or belong to form", %{conn: conn} do
821873
session = visit(conn, "/live/index")
822874

test/phoenix_test/static_test.exs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,19 @@ defmodule PhoenixTest.StaticTest do
558558
|> assert_has("#form-data", text: "three")
559559
end
560560

561+
test "can re-check an array named checkbox after unchecking it", %{conn: conn} do
562+
conn
563+
|> visit("/page/index")
564+
|> within("#array-checkbox-form", fn session ->
565+
session
566+
|> uncheck("One")
567+
|> check("One")
568+
end)
569+
|> submit()
570+
|> assert_has("#form-data", text: "one")
571+
|> assert_has("#form-data", text: "two")
572+
end
573+
561574
test "handle checkbox name with '?'", %{conn: conn} do
562575
conn
563576
|> visit("/page/index")
@@ -640,6 +653,20 @@ defmodule PhoenixTest.StaticTest do
640653
|> refute_has("#form-data", text: "one")
641654
|> assert_has("#form-data", text: "two")
642655
end
656+
657+
test "can uncheck an array named checkbox after checking it", %{conn: conn} do
658+
conn
659+
|> visit("/page/index")
660+
|> within("#array-checkbox-form", fn session ->
661+
session
662+
|> check("Three")
663+
|> uncheck("Three")
664+
end)
665+
|> submit()
666+
|> refute_has("#form-data", text: "three")
667+
|> assert_has("#form-data", text: "one")
668+
|> assert_has("#form-data", text: "two")
669+
end
643670
end
644671

645672
describe "choose/3" do

0 commit comments

Comments
 (0)