Skip to content
This repository

Refactor input_checkboxes_input_spec to use subject #705

Closed
wants to merge 3 commits into from

2 participants

Dmytrii Nagirniak Justin French
This page is out of date. Refresh to see the latest.
10 lib/formtastic/inputs/check_boxes_input.rb
@@ -109,7 +109,7 @@ def choice_html(choice)
109 109 end
110 110
111 111 def hidden_field_for_all
112   - if hidden_fields?
  112 + unless hidden_collection?
113 113 ""
114 114 else
115 115 options = {}
@@ -120,7 +120,13 @@ def hidden_field_for_all
120 120 end
121 121
122 122 def hidden_fields?
123   - options[:hidden_fields]
  123 + render_hidden = options[:hidden_fields]
  124 + return false if render_hidden == :never
  125 + render_hidden
  126 + end
  127 +
  128 + def hidden_collection?
  129 + options[:hidden_fields] != :never
124 130 end
125 131
126 132 def check_box_with_hidden_input(choice)
141 spec/inputs/check_boxes_input_spec.rb
@@ -5,6 +5,8 @@
5 5
6 6 include FormtasticSpecHelper
7 7
  8 + subject { output_buffer }
  9 +
8 10 describe 'for a has_many association' do
9 11 before do
10 12 @output_buffer = ''
@@ -26,75 +28,75 @@
26 28 it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]')
27 29
28 30 it 'should generate a legend containing a label with text for the input' do
29   - output_buffer.should have_tag('form li fieldset legend.label label')
30   - output_buffer.should have_tag('form li fieldset legend.label label', /Posts/)
  31 + should have_tag('form li fieldset legend.label label')
  32 + should have_tag('form li fieldset legend.label label', /Posts/)
31 33 end
32 34
33 35 it 'should not link the label within the legend to any input' do
34   - output_buffer.should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
  36 + should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
35 37 end
36 38
37 39 it 'should generate an ordered list with an li.choice for each choice' do
38   - output_buffer.should have_tag('form li fieldset ol')
39   - output_buffer.should have_tag('form li fieldset ol li.choice input[@type=checkbox]', :count => ::Post.all.size)
  40 + should have_tag('form li fieldset ol')
  41 + should have_tag('form li fieldset ol li.choice input[@type=checkbox]', :count => ::Post.all.size)
40 42 end
41 43
42 44 it 'should have one option with a "checked" attribute' do
43   - output_buffer.should have_tag('form li input[@checked]', :count => 1)
  45 + should have_tag('form li input[@checked]', :count => 1)
44 46 end
45 47
46 48 it 'should not generate hidden inputs with default value blank' do
47   - output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']")
  49 + should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']")
48 50 end
49 51
50 52 it 'should not render hidden inputs inside the ol' do
51   - output_buffer.should_not have_tag("form li fieldset ol li input[@type='hidden']")
  53 + should_not have_tag("form li fieldset ol li input[@type='hidden']")
52 54 end
53 55
54 56 it 'should render one hidden input for each choice outside the ol' do
55   - output_buffer.should have_tag("form li fieldset > input[@type='hidden']", :count => 1)
  57 + should have_tag("form li fieldset > input[@type='hidden']", :count => 1)
56 58 end
57 59
58 60 describe "each choice" do
59 61
60 62 it 'should not give the choice label the .label class' do
61   - output_buffer.should_not have_tag('li.choice label.label')
  63 + should_not have_tag('li.choice label.label')
62 64 end
63 65
64 66 it 'should not be marked as required' do
65   - output_buffer.should_not have_tag('li.choice input[@required]')
  67 + should_not have_tag('li.choice input[@required]')
66 68 end
67 69
68 70 it 'should contain a label for the radio input with a nested input and label text' do
69 71 ::Post.all.each do |post|
70   - output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/)
71   - output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
  72 + should have_tag('form li fieldset ol li label', /#{post.to_label}/)
  73 + should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
72 74 end
73 75 end
74 76
75 77 it 'should use values as li.class when value_as_class is true' do
76 78 ::Post.all.each do |post|
77   - output_buffer.should have_tag("form li fieldset ol li.post_#{post.id} label")
  79 + should have_tag("form li fieldset ol li.post_#{post.id} label")
78 80 end
79 81 end
80 82
81 83 it 'should have a checkbox input but no hidden field for each post' do
82 84 ::Post.all.each do |post|
83   - output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
84   - output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 1)
  85 + should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
  86 + should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 1)
85 87 end
86 88 end
87 89
88 90 it 'should have a hidden field with an empty array value for the collection to allow clearing of all checkboxes' do
89   - output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value='']", :count => 1)
  91 + should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value='']", :count => 1)
90 92 end
91 93
92 94 it 'the hidden field with an empty array value should be followed by the ol' do
93   - output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value=''] + ol", :count => 1)
  95 + should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value=''] + ol", :count => 1)
94 96 end
95 97
96 98 it 'should not have a hidden field with an empty string value for the collection' do
97   - output_buffer.should_not have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
  99 + should_not have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
98 100 end
99 101
100 102 it 'should have a checkbox and a hidden field for each post with :hidden_field => true' do
@@ -105,15 +107,15 @@
105 107 end)
106 108
107 109 ::Post.all.each do |post|
108   - output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
109   - output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
  110 + should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
  111 + should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
110 112 end
111 113
112 114 end
113 115
114 116 it "should mark input as checked if it's the the existing choice" do
115 117 ::Post.all.include?(@fred.posts.first).should be_true
116   - output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
  118 + should have_tag("form li fieldset ol li label input[@checked='checked']")
117 119 end
118 120 end
119 121
@@ -126,26 +128,26 @@
126 128 end
127 129
128 130 it 'should generate a fieldset with legend' do
129   - output_buffer.should have_tag('form li fieldset legend', /Author/)
  131 + should have_tag('form li fieldset legend', /Author/)
130 132 end
131 133
132 134 it 'shold generate an li tag for each item in the collection' do
133   - output_buffer.should have_tag('form li fieldset ol li input[@type=checkbox]', :count => ::Author.all.size)
  135 + should have_tag('form li fieldset ol li input[@type=checkbox]', :count => ::Author.all.size)
134 136 end
135 137
136 138 it 'should generate labels for each item' do
137 139 ::Author.all.each do |author|
138   - output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
139   - output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
  140 + should have_tag('form li fieldset ol li label', /#{author.to_label}/)
  141 + should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
140 142 end
141 143 end
142 144
143 145 it 'should generate inputs for each item' do
144 146 ::Author.all.each do |author|
145   - output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
146   - output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
147   - output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
148   - output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
  147 + should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
  148 + should have_tag("form li fieldset ol li label input[@type='checkbox']")
  149 + should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
  150 + should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
149 151 end
150 152 end
151 153
@@ -154,7 +156,7 @@
154 156 concat(builder.input(:author_id, :as => :check_boxes, :collection => [["<b>Item 1</b>", 1], ["<b>Item 2</b>", 2]]))
155 157 end)
156 158
157   - output_buffer.should have_tag('form li fieldset ol li label') do |label|
  159 + should have_tag('form li fieldset ol li label') do |label|
158 160 label.body.should match /&lt;b&gt;Item [12]&lt;\/b&gt;$/
159 161 end
160 162 end
@@ -172,18 +174,37 @@
172 174
173 175 it 'should have a checkbox input for each post' do
174 176 ::Post.all.each do |post|
175   - output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
176   - output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => ::Post.all.length)
  177 + should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
  178 + should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => ::Post.all.length)
177 179 end
178 180 end
179 181
180 182 it "should mark input as checked if it's the the existing choice" do
181 183 ::Post.all.include?(@fred.posts.first).should be_true
182   - output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
  184 + should have_tag("form li fieldset ol li label input[@checked='checked']")
183 185 end
184 186
185 187 it 'should not generate empty hidden inputs' do
186   - output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.all.length)
  188 + should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.all.length)
  189 + end
  190 +
  191 + it 'should generate collection hidden input' do
  192 + should have_tag("form input[@type='hidden'][@name='author[post_ids][]']", :count => 1)
  193 + end
  194 + end
  195 +
  196 + describe 'when :hidden_fields is set to :never' do
  197 + before do
  198 + @output_buffer = ''
  199 + mock_everything
  200 +
  201 + concat(semantic_form_for(@fred) do |builder|
  202 + concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true, :hidden_fields => :never))
  203 + end)
  204 + end
  205 +
  206 + it 'should not generate any hidden inputs' do
  207 + should_not have_tag("form li input[@type='hidden']")
187 208 end
188 209 end
189 210
@@ -202,7 +223,7 @@
202 223 end
203 224
204 225 it 'should not have any disabled item(s)' do
205   - output_buffer.should_not have_tag("form li fieldset ol li label input[@disabled='disabled']")
  226 + should_not have_tag("form li fieldset ol li label input[@disabled='disabled']")
206 227 end
207 228 end
208 229
@@ -216,9 +237,9 @@
216 237 end
217 238
218 239 it "should have one item disabled; the specified one" do
219   - output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
220   - output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
221   - output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
  240 + should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
  241 + should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
  242 + should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
222 243 end
223 244 end
224 245
@@ -232,11 +253,11 @@
232 253 end
233 254
234 255 it "should have multiple items disabled; the specified ones" do
235   - output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
236   - output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
237   - output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
238   - output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
239   - output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
  256 + should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
  257 + should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
  258 + should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
  259 + should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
  260 + should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
240 261 end
241 262 end
242 263
@@ -259,7 +280,7 @@
259 280 end
260 281
261 282 it "should do foo" do
262   - output_buffer.should have_tag("legend.label label", /Translated/)
  283 + should have_tag("legend.label label", /Translated/)
263 284 end
264 285
265 286 end
@@ -273,7 +294,7 @@
273 294 end
274 295
275 296 it "should output the correct label title" do
276   - output_buffer.should have_tag("legend.label label", /The authors/)
  297 + should have_tag("legend.label label", /The authors/)
277 298 end
278 299 end
279 300
@@ -287,11 +308,11 @@
287 308 end
288 309
289 310 it "should not output the legend" do
290   - output_buffer.should_not have_tag("legend.label")
  311 + should_not have_tag("legend.label")
291 312 end
292 313
293 314 it "should not cause escaped HTML" do
294   - output_buffer.should_not include("&gt;")
  315 + should_not include("&gt;")
295 316 end
296 317
297 318 end
@@ -305,7 +326,7 @@
305 326 end
306 327
307 328 it "should output the correct label title" do
308   - output_buffer.should have_tag("legend.label label abbr")
  329 + should have_tag("legend.label label abbr")
309 330 end
310 331 end
311 332
@@ -324,14 +345,14 @@
324 345
325 346 it 'should render checkboxes' do
326 347 # I'm aware these two lines test the same thing
327   - output_buffer.should have_tag('input[type="checkbox"]', :count => 2)
328   - output_buffer.should have_tag('input[type="checkbox"]', :count => ::Author.all.size)
  348 + should have_tag('input[type="checkbox"]', :count => 2)
  349 + should have_tag('input[type="checkbox"]', :count => ::Author.all.size)
329 350 end
330 351
331 352 it 'should only select checkboxes that are present in the association' do
332 353 # I'm aware these two lines test the same thing
333   - output_buffer.should have_tag('input[checked="checked"]', :count => 1)
334   - output_buffer.should have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
  354 + should have_tag('input[checked="checked"]', :count => 1)
  355 + should have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
335 356 end
336 357
337 358 end
@@ -352,7 +373,7 @@
352 373 concat(semantic_form_for(@new_post) do |builder|
353 374 concat(builder.input(:author, :as => :check_boxes, :member_value => :custom_value, :collection => [item, item, item]))
354 375 end)
355   - output_buffer.should have_tag('input[@type=checkbox][@value="custom_value"]', :count => 3)
  376 + should have_tag('input[@type=checkbox][@value="custom_value"]', :count => 3)
356 377 end
357 378 end
358 379 end
@@ -369,8 +390,8 @@
369 390 end
370 391
371 392 it 'should check the correct checkboxes' do
372   - output_buffer.should have_tag("form li fieldset ol li label input[@value='fiction'][@checked='checked']")
373   - output_buffer.should have_tag("form li fieldset ol li label input[@value='biography'][@checked='checked']")
  393 + should have_tag("form li fieldset ol li label input[@value='fiction'][@checked='checked']")
  394 + should have_tag("form li fieldset ol li label input[@value='biography'][@checked='checked']")
374 395 end
375 396 end
376 397
@@ -386,7 +407,7 @@
386 407 end
387 408
388 409 it "should have a label for #context2_author_post_ids_19" do
389   - output_buffer.should have_tag("form li label[@for='context2_author_post_ids_19']")
  410 + should have_tag("form li label[@for='context2_author_post_ids_19']")
390 411 end
391 412
392 413 it_should_have_input_with_id('context2_author_post_ids_19')
@@ -406,13 +427,13 @@
406 427
407 428 it "should use array items for labels and values" do
408 429 @_collection.each do |post|
409   - output_buffer.should have_tag('form li fieldset ol li label', /#{post.first}/)
410   - output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.last}']")
  430 + should have_tag('form li fieldset ol li label', /#{post.first}/)
  431 + should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.last}']")
411 432 end
412 433 end
413 434
414 435 it "should not check any items" do
415   - output_buffer.should have_tag('form li input[@checked]', :count => 0)
  436 + should have_tag('form li input[@checked]', :count => 0)
416 437 end
417 438 end
418 439

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.