@@ -65,7 +65,6 @@ defmodule LiveSelectTest do
6565 stub_options ( [ { "A" , 1 } , { "B" , 2 } , { "C" , 3 } ] )
6666
6767 { :ok , live , _html } = live ( conn , "/" )
68-
6968 type ( live , "ABC" )
7069
7170 assert_options ( live , [ "A" , "B" , "C" ] )
@@ -990,4 +989,88 @@ defmodule LiveSelectTest do
990989 end
991990 end
992991 end
992+
993+ describe "Disabled option tests" do
994+ test "Disabled Options are skipped by keydown events" , % { conn: conn } do
995+ stub_options ( [ { "A" , 1 , false } , { "B" , 2 , true } , { "C" , 3 , false } ] )
996+
997+ { :ok , live , _html } = live ( conn , "/" )
998+
999+ type ( live , "ABC" )
1000+ assert_options ( live , [ "A" , "B" , "C" ] )
1001+
1002+ select_nth_option ( live , 2 )
1003+ assert_selected ( live , "C" , 3 )
1004+ end
1005+
1006+ test "Disabled Options are skipped by key up events" , % { conn: conn } do
1007+ stub_options ( [ { "A" , 1 , false } , { "B" , 2 , true } , { "C" , 3 , false } ] )
1008+
1009+ { :ok , live , _html } = live ( conn , "/" )
1010+
1011+ type ( live , "ABC" )
1012+ assert_options ( live , [ "A" , "B" , "C" ] )
1013+
1014+ # Navigate to C by going two down
1015+ navigate ( live , 2 , :down , [ ] )
1016+
1017+ # Navigate back to A by going 1 up as we skip B because it's disabled.
1018+ # Then select the item we're on. It should be "A"
1019+ navigate ( live , 1 , :up , [ ] )
1020+ keydown ( live , "Enter" , [ ] )
1021+
1022+ assert_selected ( live , "A" , 1 )
1023+ end
1024+
1025+ test "Supports disabling options filled from an enumerable of maps" , % { conn: conn } do
1026+ stub_options ( [
1027+ % { label: "A" , value: 1 , disabled: false } ,
1028+ % { label: "B" , value: 2 , disabled: true } ,
1029+ % { label: "C" , value: 3 , disabled: false }
1030+ ] )
1031+
1032+ { :ok , live , _html } = live ( conn , "/" )
1033+
1034+ type ( live , "ABC" )
1035+ assert_options ( live , [ "A" , "B" , "C" ] )
1036+
1037+ select_nth_option ( live , 1 )
1038+ assert_selected ( live , "A" , 1 )
1039+
1040+ type ( live , "ABC" )
1041+ assert_options ( live , [ "A" , "B" , "C" ] )
1042+ # The maps are sorted on their key value pairings on the showcase page
1043+ # before being sent to the LiveSelect component. This results in the
1044+ # disabled option "B" being sorted last.
1045+ select_nth_option ( live , 3 , method: :click )
1046+ assert_selected_static ( live , "A" , 1 )
1047+ end
1048+
1049+ test "Disabled options can't be selected with mouseclick" , % { conn: conn } do
1050+ stub_options ( [ { "A" , 1 , false } , { "B" , 2 , true } , { "C" , 3 , false } ] )
1051+
1052+ { :ok , live , _html } = live ( conn , "/" )
1053+ type ( live , "ABC" )
1054+
1055+ assert_options ( live , [ "A" , "B" , "C" ] )
1056+ select_nth_option ( live , 1 )
1057+ assert_selected ( live , "A" , 1 )
1058+
1059+ # Mouse clicks won't change the selected option
1060+ type ( live , "ABC" )
1061+ select_nth_option ( live , 2 , method: :click )
1062+ assert_selected_static ( live , "A" , 1 )
1063+ end
1064+
1065+ test "If everything is disabled, nothing is saved" , % { conn: conn } do
1066+ stub_options ( [ { "A" , 1 , true } , { "B" , 2 , true } ] )
1067+
1068+ { :ok , live , _html } = live ( conn , "/" )
1069+ type ( live , "ABC" )
1070+
1071+ assert_options ( live , [ "A" , "B" ] )
1072+ select_nth_option ( live , 1 )
1073+ refute_selected ( live )
1074+ end
1075+ end
9931076end
0 commit comments