1
+ require_relative "../../../../support/utils"
2
+ require_relative "../../../../support/test_controller"
3
+ require_relative "support/form_test_controller"
4
+ require_relative "support/model_form_test_controller"
5
+ include Utils
6
+
7
+ describe "Form Component" , type : :feature , js : true do
8
+
9
+ before :all do
10
+ class BasePage < Matestack ::Ui ::Page
11
+ def response
12
+ form form_config , :include do
13
+ form_input id : "my-test-input" , key : :foo , type : :text
14
+ form_submit do
15
+ button text : "Submit me!"
16
+ end
17
+ end
18
+ end
19
+
20
+ def form_config
21
+ return {
22
+ for : :my_object ,
23
+ method : :post ,
24
+ path : :async_request_success_form_test_path ,
25
+ params : {
26
+ id : 42
27
+ }
28
+ }
29
+ end
30
+ end
31
+
32
+ Rails . application . routes . append do
33
+ post '/success_form_test/:id' , to : 'form_test#success_submit' , as : 'async_request_success_form_test'
34
+ post '/success_form_test_with_transition/:id' , to : 'form_test#success_submit_with_transition' , as : 'async_request_success_form_test_with_transition'
35
+ post '/failure_form_test_with_transition/:id' , to : 'form_test#failure_submit_with_transition' , as : 'async_request_failure_form_test_with_transition'
36
+ post '/success_form_test_with_redirect/:id' , to : 'form_test#success_submit_with_redirect' , as : 'async_request_success_form_test_with_redirect'
37
+ post '/failure_form_test_with_redirect/:id' , to : 'form_test#failure_submit_with_redirect' , as : 'async_request_failure_form_test_with_redirect'
38
+ post '/failure_form_test/:id' , to : 'form_test#failure_submit' , as : 'async_request_failure_form_test'
39
+ post '/model_form_test' , to : 'model_form_test#model_submit' , as : 'async_request_model_form_test'
40
+ end
41
+ Rails . application . reload_routes!
42
+ end
43
+
44
+ before :each do
45
+ allow_any_instance_of ( FormTestController ) . to receive ( :expect_params )
46
+ end
47
+
48
+ describe 'async submit' do
49
+ before :all do
50
+ class BaseExamplePage < BasePage
51
+ end
52
+ end
53
+
54
+ it "Example 1 - Async submit request with clientside payload" do
55
+ visit "/base_example"
56
+ fill_in "my-test-input" , with : "bar"
57
+ expect_any_instance_of ( FormTestController ) . to receive ( :expect_params )
58
+ . with ( hash_including ( my_object : { foo : "bar" } ) )
59
+ click_button "Submit me!"
60
+ end
61
+
62
+ it "Example 2 - Async submit request and clears inputs on success" do
63
+ visit "/base_example"
64
+ fill_in "my-test-input" , with : "bar"
65
+ expect ( find_field ( "my-test-input" ) . value ) . to eq "bar"
66
+ click_button "Submit me!"
67
+ expect ( find_field ( "my-test-input" ) . value ) . to eq ""
68
+ end
69
+
70
+ end
71
+
72
+ describe 'async emit' do
73
+
74
+ it "Example 1 - Async submit request with success event" do
75
+ class BaseExamplePage < BasePage
76
+ def response
77
+ super
78
+ async show_on : "my_form_success" do
79
+ plain "{{event.data.message}}"
80
+ end
81
+ end
82
+
83
+ def form_config
84
+ super . merge ( success : { emit : "my_form_success" } )
85
+ end
86
+ end
87
+
88
+ visit "/base_example"
89
+ fill_in "my-test-input" , with : "bar"
90
+ click_button "Submit me!"
91
+ expect ( page ) . to have_content ( "server says: form submitted successfully" )
92
+ end
93
+
94
+ it "Example 3 - Async submit request with failure event" do
95
+ class BaseExamplePage < BasePage
96
+ def response
97
+ super
98
+ async show_on : "my_form_failure" do
99
+ plain "{{event.data.message}}"
100
+ plain "{{event.data.errors}}"
101
+ end
102
+ end
103
+
104
+ def form_config
105
+ super . merge ( path : :async_request_failure_form_test_path , failure : { emit : "my_form_failure" } )
106
+ end
107
+ end
108
+
109
+ visit "/base_example"
110
+ fill_in "my-test-input" , with : "bar"
111
+ click_button "Submit me!"
112
+ expect ( page ) . to have_content ( "server says: form had errors" )
113
+ expect ( page ) . to have_content ( "\" foo\" : [ \" seems to be invalid\" ]" )
114
+ end
115
+
116
+ end
117
+
118
+ end
0 commit comments