/
form_spec.rb
223 lines (178 loc) · 7.53 KB
/
form_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
require 'integration/spec_helper'
require 'corner_stones/form'
require 'corner_stones/form/with_inline_errors'
describe CornerStones::Form do
stub_capybara_response
let(:html) {<<-HTML
<form action="/articles" method="post" class="article-form">
<label for="title">Title</label>
<input type="text" name="title" id="title">
<label for="password">Password</label>
<input type="password" name="password" id="password">
<label for="author">Author</label>
<select name="author" id="author">
<option value="1">Robert C. Martin</option>
<option value="2">Eric Evans</option>
<option value="3">Kent Beck</option>
</select>
<label for="body">Body</label>
<textarea name="body" id="body">
</textarea>
<label for="file">File</label>
<input name="file" id="file" type="file">
<label for="check">Checkbox</label>
<input name="check" id="check" type="checkbox" value="1">
<label for="check2">Checkbox2</label>
<input name="check2" id="check2" type="checkbox" value="1">
<input type="submit" name="button" value="Save">
<input type="submit" name="button" value="Save Article">
</form>
HTML
}
subject { CornerStones::Form.new('.article-form') }
it 'allows you to fill in the form' do
subject.fill_in_with('Title' => 'Domain Driven Design',
'Password' => 'secret',
'Author' => 'Eric Evans',
'Body' => '...',
'File' => 'spec/files/hadoken.png',
'Checkbox' => true)
find('#title').value.must_equal 'Domain Driven Design'
find('#password').value.must_equal 'secret'
find('#author').value.must_equal '2'
find('#body').value.must_equal '...'
find('#file').value.must_equal 'spec/files/hadoken.png'
find('#check')[:checked].must_equal true
end
it 'allows you to submit the form' do
subject.submit
current_path.must_equal '/articles'
page.driver.request.post?.must_equal true
end
it 'you can supply the submit-button text with the :button option' do
subject.submit(:button => 'Save Article')
page.driver.request.params['button'].must_equal 'Save Article'
end
it 'allows you to process (fill_in_with + submit) the form' do
subject.process(:fill_in => {
'Title' => 'Domain Driven Design',
'Password' => 'SeCrEt',
'Author' => 'Eric Evans',
'Body' => 'Some Content...',
'File' => 'spec/files/hadoken.png',
'Checkbox' => true})
current_path.must_equal '/articles'
page.driver.request.post?.must_equal true
page.driver.request.params.must_equal({"title" => "Domain Driven Design", 'password' => 'SeCrEt', "author" => "2", "body" => "Some Content...", "file" => "hadoken.png", 'check' => '1', 'button' => 'Save'})
end
it 'allows you to process (fill_in_with + submit) the form using an alternate button' do
subject.process(:fill_in => {'Title' => 'Domain Driven Design',
'Author' => 'Eric Evans',
'Body' => 'Some Content...'},
:button => 'Save Article')
page.driver.request.params['button'].must_equal('Save Article')
end
it 'allows you the retrieve the current values of the form fields' do
subject.fill_in_with('Title' => 'Domain Driven Design',
'Password' => 'secret',
'Author' => 'Eric Evans',
'Body' => '...',
'File' => 'spec/files/hadoken.png',
'Checkbox' => true,
'Checkbox2' => false)
subject.attributes.must_equal('Title' => 'Domain Driven Design',
'Password' => 'secret',
'Author' => 'Eric Evans',
'Body' => '...',
'File' => 'spec/files/hadoken.png',
'Checkbox' => '1',
'Checkbox2' => nil)
end
describe 'form with an unknown field type' do
let(:html) {<<-HTML
<form action="/articles" method="post" class="form-with-errors article-form">
<label for="unknown">Unknown</label>
<a id="unknown">Link</a>
</form>
HTML
}
it 'raises an error when filling the form' do
assert_raises(CornerStones::Form::UnknownFieldError) { subject.fill_in_with('Unknown' => '123456') }
end
end
describe 'mixins' do
describe 'form errors' do
before do
subject.extend(CornerStones::Form::WithInlineErrors)
end
describe 'with errors' do
let(:html) {<<-HTML
<form action="/articles" method="post" class="form-with-errors article-form">
<div>
<label for="title">Title</label>
<input type="text" name="title" id="title">
</div>
<div class="error">
<label for="author">Author</label>
<select name="author" id="author">
<option value="1">Robert C. Martin</option>
<option value="2">Eric Evans</option>
<option value="3">Kent Beck</option>
</select>
<span class="help-inline">The author is not active</span>
</div>
<div class="error">
<label for="body">Body</label>
<textarea name="body" id="body">...</textarea>
<span class="help-inline">invalid body</span>
</div>
<div class="error">
<input name="nominated" type="hidden" value="0">
<label for="nominated">
<input name="nominated" id="nominated" type="checkbox" checked="checked" value="3">
Nominated
</label>
<span class="help-inline">invalid nomination</span>
</div>
<input type="submit" value="Save">
</form>
HTML
}
it 'assembles the errors into a hash' do
subject.errors.must_equal([{"Field" => "Author", "Value" => "Robert C. Martin", "Error" => "The author is not active"},
{"Field" => "Body", "Value" => "...", "Error" => "invalid body"},
{'Field' => 'Nominated', 'Value' => '3', 'Error' => 'invalid nomination'}])
end
it '#assert_has_no_errors fails' do
lambda do
subject.assert_has_no_errors
end.must_raise(CornerStones::Form::WithInlineErrors::FormHasErrorsError)
end
it 'does not allow you to submit the form by default' do
lambda do
subject.submit
end.must_raise(CornerStones::Form::WithInlineErrors::FormHasErrorsError)
end
it 'bypass the auto-error-validation when passing :assert_valid => false' do
subject.submit(:assert_valid => false)
end
end
describe 'without errors' do
let(:html) {<<-HTML
<form action="/articles" method="post" class="form-without-errors article-form">
<label for="title">Title</label>
<input type="text" name="title" id="title">
<input type="submit" value="Save">
<form>
HTML
}
it '#assert_has_no_errors passes' do
subject.assert_has_no_errors
end
it 'allows you to submit the form' do
subject.submit
end
end
end
end
end