/
select_input_spec.rb
437 lines (360 loc) · 15.9 KB
/
select_input_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# coding: utf-8
require File.dirname(__FILE__) + '/../spec_helper'
describe 'select input' do
include FormtasticSpecHelper
before do
@output_buffer = ''
mock_everything
end
describe 'explicit values' do
describe 'using an array of values' do
before do
@array_with_values = ["Title A", "Title B", "Title C"]
@array_with_keys_and_values = [["Title D", 1], ["Title E", 2], ["Title F", 3]]
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :as => :select, :collection => @array_with_values))
concat(builder.input(:title, :as => :select, :collection => @array_with_keys_and_values))
end
end
it 'should have a option for each key and/or value' do
@array_with_values.each do |v|
output_buffer.should have_tag("form li select option[@value='#{v}']", /^#{v}$/)
end
@array_with_keys_and_values.each do |v|
output_buffer.should have_tag("form li select option[@value='#{v.second}']", /^#{v.first}$/)
end
end
end
describe 'using a range' do
before do
@range_with_values = 1..5
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :as => :select, :collection => @range_with_values))
end
end
it 'should have an option for each value' do
@range_with_values.each do |v|
output_buffer.should have_tag("form li select option[@value='#{v}']", /^#{v}$/)
end
end
end
end
describe 'for a belongs_to association' do
before do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :select))
end
end
it_should_have_input_wrapper_with_class("select")
it_should_have_input_wrapper_with_id("post_author_input")
it_should_have_label_with_text(/Author/)
it_should_have_label_for('post_author_id')
it_should_apply_error_logic_for_input_type(:select)
it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
it_should_use_the_collection_when_provided(:select, 'option')
it 'should have a select inside the wrapper' do
output_buffer.should have_tag('form li select')
output_buffer.should have_tag('form li select#post_author_id')
end
it 'should not create a multi-select' do
output_buffer.should_not have_tag('form li select[@multiple]')
end
it 'should create a select without size' do
output_buffer.should_not have_tag('form li select[@size]')
end
it 'should have a blank option' do
output_buffer.should have_tag("form li select option[@value='']")
end
it 'should have a select option for each Author' do
output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1)
::Author.find(:all).each do |author|
output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
end
end
it 'should have one option with a "selected" attribute' do
output_buffer.should have_tag('form li select option[@selected]', :count => 1)
end
it 'should not singularize the association name' do
@new_post.stub!(:author_status).and_return(@bob)
@new_post.stub!(:author_status_id).and_return(@bob.id)
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author_status, :as => :select))
end
output_buffer.should have_tag('form li select#post_author_status_id')
end
end
describe 'for a belongs_to association with :group_by => :continent' do
before do
@authors = [@bob, @fred, @fred, @fred]
::Author.stub!(:find).and_return(@authors)
@continent_names = %w(Europe Africa)
@continents = (0..1).map { |i| mock("continent", :id => (100 - i) ) }
@authors[0..1].each_with_index { |author, i| author.stub!(:continent).and_return(@continents[i]) }
@continents.each_with_index do |continent, i|
continent.stub!(:to_label).and_return(@continent_names[i])
continent.stub!(:authors).and_return([@authors[i]])
end
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :select, :group_by => :continent ) )
concat(builder.input(:author, :as => :select, :group_by => :continent, :group_label_method => :id ) )
end
end
it_should_have_input_wrapper_with_class("select")
it_should_have_input_wrapper_with_id("post_author_input")
it_should_have_label_with_text(/Author/)
it_should_have_label_for('post_author_id')
# TODO, need to find a way to repeat some of the specs and logic from the belongs_to specs without grouping
0.upto(1) do |i|
it 'should have all option groups and the right values' do
output_buffer.should have_tag("form li select optgroup[@label='#{@continent_names[i]}']", @authors[i].to_label)
end
it 'should have custom group labels' do
output_buffer.should have_tag("form li select optgroup[@label='#{@continents[i].id}']", @authors[i].to_label)
end
end
it 'should have no duplicate groups' do
output_buffer.should have_tag('form li select optgroup', :count => 4)
end
it 'should sort the groups on the label method' do
output_buffer.should have_tag("form li select optgroup[@label='Africa']")
output_buffer.should have_tag("form li select optgroup[@label='99']")
end
it 'should call find with :include for more optimized queries' do
Author.should_receive(:find).with(:all, :include => :continent)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :select, :group_by => :continent ) )
end
end
end
describe 'for a has_many association' do
before do
semantic_form_for(@fred) do |builder|
concat(builder.input(:posts, :as => :select))
end
end
it_should_have_input_wrapper_with_class("select")
it_should_have_input_wrapper_with_id("author_posts_input")
it_should_have_label_with_text(/Post/)
it_should_have_label_for('author_post_ids')
it_should_apply_error_logic_for_input_type(:select)
it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
it_should_use_the_collection_when_provided(:select, 'option')
it 'should have a select inside the wrapper' do
output_buffer.should have_tag('form li select')
output_buffer.should have_tag('form li select#author_post_ids')
end
it 'should have a multi-select select' do
output_buffer.should have_tag('form li select[@multiple="multiple"]')
end
it 'should have a select option for each Post' do
output_buffer.should have_tag('form li select option', :count => ::Post.find(:all).size)
::Post.find(:all).each do |post|
output_buffer.should have_tag("form li select option[@value='#{post.id}']", /#{post.to_label}/)
end
end
it 'should not have a blank option' do
output_buffer.should_not have_tag("form li select option[@value='']")
end
it 'should have one option with a "selected" attribute' do
output_buffer.should have_tag('form li select option[@selected]', :count => 1)
end
end
describe 'for a has_and_belongs_to_many association' do
before do
semantic_form_for(@freds_post) do |builder|
concat(builder.input(:authors, :as => :select))
end
end
it_should_have_input_wrapper_with_class("select")
it_should_have_input_wrapper_with_id("post_authors_input")
it_should_have_label_with_text(/Author/)
it_should_have_label_for('post_author_ids')
it_should_apply_error_logic_for_input_type(:select)
it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
it_should_use_the_collection_when_provided(:select, 'option')
it 'should have a select inside the wrapper' do
output_buffer.should have_tag('form li select')
output_buffer.should have_tag('form li select#post_author_ids')
end
it 'should have a multi-select select' do
output_buffer.should have_tag('form li select[@multiple="multiple"]')
end
it 'should have a select option for each Author' do
output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size)
::Author.find(:all).each do |author|
output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
end
end
it 'should not have a blank option' do
output_buffer.should_not have_tag("form li select option[@value='']")
end
it 'should have one option with a "selected" attribute' do
output_buffer.should have_tag('form li select option[@selected]', :count => 1)
end
end
describe 'when :prompt => "choose something" is set' do
before do
@new_post.stub!(:author_id).and_return(nil)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :select, :prompt => "choose author"))
end
end
it 'should have a select with prompt' do
output_buffer.should have_tag("form li select option[@value='']", /choose author/)
end
it 'should not have a blank select option' do
output_buffer.should_not have_tag("form li select option[@value='']", "")
end
end
describe 'when no object is given' do
before(:each) do
semantic_form_for(:project, :url => 'http://test.host') do |builder|
concat(builder.input(:author, :as => :select, :collection => ::Author.find(:all)))
end
end
it 'should generate label' do
output_buffer.should have_tag('form li label', /Author/)
output_buffer.should have_tag("form li label[@for='project_author']")
end
it 'should generate select inputs' do
output_buffer.should have_tag('form li select#project_author')
output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1)
end
it 'should generate an option to each item' do
::Author.find(:all).each do |author|
output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
end
end
end
describe 'when :selected is set' do
before do
@output_buffer = ''
end
describe "no selected items" do
before do
@new_post.stub!(:author_id).and_return(nil)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :select, :selected => nil))
end
end
it 'should not have any selected item(s)' do
output_buffer.should_not have_tag("form li select option[@selected='selected']")
end
end
describe "single selected item" do
before do
@new_post.stub!(:author_id).and_return(nil)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :select, :selected => @bob.id))
end
end
it 'should have a selected item; the specified one' do
output_buffer.should have_tag("form li select option[@selected='selected']", :count => 1)
output_buffer.should have_tag("form li select option[@selected='selected']", /bob/i)
output_buffer.should have_tag("form li select option[@selected='selected'][@value='#{@bob.id}']")
end
end
describe "multiple selected items" do
describe "when :multiple => false" do
before do
@new_post.stub!(:author_ids).and_return(nil)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:authors, :as => :select, :selected => [@bob.id, @fred.id], :multiple => false))
end
end
it "should only select the first value" do
output_buffer.should have_tag("form li select option[@selected='selected']", :count => 1)
output_buffer.should have_tag("form li select[^@multiple] option[@selected='selected']", /bob/i)
output_buffer.should have_tag("form li select[^@multiple] option[@selected='selected'][@value='#{@bob.id}']")
end
end
describe "when :multiple => true" do
before do
@new_post.stub!(:author_ids).and_return(nil)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:authors, :as => :select, :selected => [@bob.id, @fred.id]))
end
end
it "should have multiple items selected; the specified ones" do
output_buffer.should have_tag("form li select option[@selected='selected']", :count => 2)
output_buffer.should have_tag("form li select[@multiple] option[@selected='selected']", /bob/i)
output_buffer.should have_tag("form li select[@multiple] option[@selected='selected'][@value='#{@bob.id}']")
output_buffer.should have_tag("form li select[@multiple] option[@selected='selected']", /fred/i)
output_buffer.should have_tag("form li select[@multiple] option[@selected='selected'][@value='#{@fred.id}']")
end
end
end
end
describe 'boolean select' do
describe 'default formtastic locale' do
before do
# Note: Works, but something like Formtastic.root.join(...) would probably be "safer".
::I18n.load_path = [File.join(File.dirname(__FILE__), *%w[.. .. lib locale en.yml])]
::I18n.backend.send(:init_translations)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:published, :as => :select))
end
end
after do
::I18n.backend.store_translations :en, {}
end
it 'should render a select with at least options: true/false' do
output_buffer.should have_tag("form li select option[@value='true']", /^Yes$/)
output_buffer.should have_tag("form li select option[@value='false']", /^No$/)
end
end
describe 'custom locale' do
before do
@boolean_select_labels = {:yes => 'Yep', :no => 'Nope'}
::I18n.backend.store_translations :en, :formtastic => @boolean_select_labels
semantic_form_for(@new_post) do |builder|
concat(builder.input(:published, :as => :select))
end
end
after do
::I18n.backend.store_translations :en, {}
end
it 'should render a select with at least options: true/false' do
output_buffer.should have_tag("form li select option[@value='true']", /#{@boolean_select_labels[:yes]}/)
output_buffer.should have_tag("form li select option[@value='false']", /#{@boolean_select_labels[:no]}/)
end
end
end
describe "enums" do
describe ":collection is set" do
before do
@output_buffer = ''
@some_meta_descriptions = ["One", "Two", "Three"]
@new_post.stub!(:meta_description).any_number_of_times
end
describe ":as is not set" do
before do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:meta_description, :collection => @some_meta_descriptions))
end
semantic_form_for(:project, :url => 'http://test.host') do |builder|
concat(builder.input(:meta_description, :collection => @some_meta_descriptions))
end
end
it "should render a select field" do
output_buffer.should have_tag("form li select", :count => 2)
end
end
describe ":as is set" do
before do
# Should not be a case, but just checking :as got highest priority in setting input type.
semantic_form_for(@new_post) do |builder|
concat(builder.input(:meta_description, :as => :string, :collection => @some_meta_descriptions))
end
semantic_form_for(:project, :url => 'http://test.host') do |builder|
concat(builder.input(:meta_description, :as => :string, :collection => @some_meta_descriptions))
end
end
it "should render a text field" do
output_buffer.should have_tag("form li input[@type='text']", :count => 2)
end
end
end
end
end