1+ require_relative "../../../../support/utils"
2+ include Utils
3+
4+ class TestController < ActionController ::Base
5+
6+ before_action :check_params
7+
8+ def check_params
9+ expect_params ( params . permit! . to_h )
10+ end
11+
12+ def expect_params ( params )
13+ end
14+
15+ end
16+
17+ describe "Form Component" , type : :feature , js : true do
18+
19+ before :all do
20+ class FormTestController < TestController
21+ def success_submit
22+ render json : { message : "server says: form submitted successfully" } , status : 200
23+ end
24+
25+ def success_submit_with_transition
26+ render json : {
27+ message : "server says: form submitted successfully" ,
28+ transition_to : form_test_page_4_path ( id : 42 )
29+ } , status : 200
30+ end
31+
32+ def failure_submit
33+ render json : {
34+ message : "server says: form had errors" ,
35+ errors : { foo : [ "seems to be invalid" ] }
36+ } , status : 400
37+ end
38+ end
39+
40+ Rails . application . routes . append do
41+ post '/success_form_test/:id' , to : 'form_test#success_submit' , as : 'success_form_test'
42+ post '/success_form_test_with_transition/:id' , to : 'form_test#success_submit_with_transition' , as : 'success_form_test_with_transition'
43+ post '/failure_form_test/:id' , to : 'form_test#failure_submit' , as : 'failure_form_test'
44+ end
45+ Rails . application . reload_routes!
46+
47+ class ModelFormTestController < TestController
48+ def model_submit
49+ @test_model = TestModel . create ( model_params )
50+ if @test_model . errors . any?
51+ render json : {
52+ message : "server says: something went wrong!" ,
53+ errors : @test_model . errors
54+ } , status : :unprocessable_entity
55+ else
56+ render json : {
57+ message : "server says: form submitted successfully!"
58+ } , status : :ok
59+ end
60+ end
61+
62+ protected
63+
64+ def model_params
65+ params . require ( :test_model ) . permit ( :title , :description , :status , some_data : [ ] , more_data : [ ] )
66+ end
67+ end
68+
69+ Rails . application . routes . append do
70+ post '/model_form_test' , to : 'model_form_test#model_submit' , as : 'model_form_test'
71+ end
72+ Rails . application . reload_routes!
73+ end
74+
75+ before :each do
76+ allow_any_instance_of ( FormTestController ) . to receive ( :expect_params )
77+ end
78+
79+
80+ describe "Radio Button" do
81+
82+ it "generates unique names for each radio button group and value" do
83+
84+ class ExamplePage < Matestack ::Ui ::Page
85+
86+ def response
87+ components {
88+ form form_config , :include do
89+ form_select id : 'group-one-radio' , key : :array_input_one , type : :radio , options : [ 'foo' , 'bar' ]
90+ form_select id : 'group-two-radio' , key : :array_input_two , type : :radio , options : [ 'foo' , 'bar' ]
91+ form_submit do
92+ button text : 'Submit me!'
93+ end
94+ end
95+ }
96+ end
97+
98+ def form_config
99+ return {
100+ for : :my_object ,
101+ method : :post ,
102+ path : :success_form_test_path ,
103+ params : {
104+ id : 42
105+ }
106+ }
107+ end
108+
109+ end
110+
111+ visit '/example'
112+
113+ expect ( page ) . to have_selector ( '#group-one-radio[name="array_input_one_foo"]' )
114+ expect ( page ) . to have_selector ( '#group-one-radio[name="array_input_one_bar"]' )
115+
116+ expect ( page ) . to have_selector ( '#group-two-radio[name="array_input_two_foo"]' )
117+ expect ( page ) . to have_selector ( '#group-two-radio[name="array_input_two_bar"]' )
118+ end
119+
120+ end
121+
122+ end
0 commit comments