|
9 | 9 | end |
10 | 10 | let(:options) { [[1, 'Admin'], [2, 'Editor'], [3, 'Viewer']] } |
11 | 11 | let(:component) do |
12 | | - described_class.new(field, attributes: attributes, options: options) |
| 12 | + described_class.new(field, attributes: attributes, options:) |
13 | 13 | end |
14 | 14 | let(:attributes) { {} } |
15 | 15 |
|
|
81 | 81 | described_class.new( |
82 | 82 | field, |
83 | 83 | attributes: attributes, |
84 | | - options: options, |
| 84 | + options:, |
85 | 85 | multiple: true |
86 | 86 | ) |
87 | 87 | end |
|
124 | 124 | described_class.new( |
125 | 125 | field, |
126 | 126 | attributes: attributes, |
127 | | - options: options, |
| 127 | + options:, |
128 | 128 | multiple: true |
129 | 129 | ) |
130 | 130 | end |
|
158 | 158 | end |
159 | 159 | end |
160 | 160 |
|
161 | | - describe 'with include_blank: true' do |
| 161 | + describe 'with nil as first option (blank option)' do |
| 162 | + let(:options_with_blank) { [nil, *options] } |
162 | 163 | let(:component) do |
163 | 164 | described_class.new( |
164 | 165 | field, |
165 | 166 | attributes: attributes, |
166 | | - options: options, |
167 | | - include_blank: true |
| 167 | + options: options_with_blank |
168 | 168 | ) |
169 | 169 | end |
170 | 170 |
|
|
212 | 212 | described_class.new( |
213 | 213 | role_ids_field, |
214 | 214 | attributes: attributes, |
215 | | - options: options, |
| 215 | + options:, |
216 | 216 | multiple: true |
217 | 217 | ) |
218 | 218 | end |
|
246 | 246 | end |
247 | 247 | end |
248 | 248 |
|
249 | | - describe 'with both multiple and include_blank' do |
| 249 | + describe 'with both multiple and nil first option (blank)' do |
| 250 | + let(:options_with_blank) { [nil, *options] } |
250 | 251 | let(:component) do |
251 | 252 | described_class.new( |
252 | 253 | field, |
253 | 254 | attributes: attributes, |
254 | | - options: options, |
255 | | - multiple: true, |
256 | | - include_blank: true |
| 255 | + options: options_with_blank, |
| 256 | + multiple: true |
257 | 257 | ) |
258 | 258 | end |
259 | 259 |
|
|
283 | 283 | end |
284 | 284 | end |
285 | 285 |
|
286 | | - context 'with options keyword argument' do |
| 286 | + context 'with positional arguments' do |
287 | 287 | subject do |
288 | 288 | render( |
289 | 289 | form_field.select( |
290 | | - options: [[1, 'Admin'], [2, 'Editor'], [3, 'Viewer']] |
| 290 | + [1, 'Admin'], [2, 'Editor'], [3, 'Viewer'] |
291 | 291 | ) |
292 | 292 | ) |
293 | 293 | end |
294 | 294 |
|
295 | | - it 'renders select with options from options kwarg' do |
| 295 | + it 'renders select with options from positional args' do |
296 | 296 | expect(subject).to include('>Admin</option>') |
297 | 297 | expect(subject).to include('>Editor</option>') |
298 | 298 | expect(subject).to include('>Viewer</option>') |
|
303 | 303 | subject do |
304 | 304 | render( |
305 | 305 | form_field.select( |
306 | | - options: [[1, 'Admin'], [2, 'Editor']], |
| 306 | + [1, 'Admin'], [2, 'Editor'], |
307 | 307 | multiple: true |
308 | 308 | ) |
309 | 309 | ) |
310 | 310 | end |
311 | 311 |
|
312 | | - it 'renders multiple select with options kwarg' do |
| 312 | + it 'renders multiple select with positional args' do |
313 | 313 | expect(subject).to include('multiple') |
314 | 314 | expect(subject).to include('name="role_ids[]"') |
315 | 315 | expect(subject).to include('>Admin</option>') |
|
365 | 365 | expect(subject).to match(/<option value="\d+">Bob<\/option>/) |
366 | 366 | end |
367 | 367 | end |
368 | | - |
369 | | - describe 'backwards compatibility with collection parameter' do |
370 | | - context 'using deprecated collection keyword in component' do |
371 | | - let(:component) do |
372 | | - described_class.new(field, attributes: attributes, collection: options) |
373 | | - end |
374 | | - |
375 | | - it 'shows deprecation warning' do |
376 | | - expect_any_instance_of(described_class).to receive(:warn).with( |
377 | | - "[DEPRECATION] Superform::Rails::Components::Select: " \ |
378 | | - "`collection:` parameter is deprecated. " \ |
379 | | - "Use `options:` instead." |
380 | | - ) |
381 | | - component |
382 | | - end |
383 | | - |
384 | | - it 'still renders select correctly' do |
385 | | - # Suppress deprecation warning for this test |
386 | | - allow_any_instance_of(described_class).to receive(:warn) |
387 | | - result = render(component) |
388 | | - expect(result).to include('>Admin</option>') |
389 | | - expect(result).to include('>Editor</option>') |
390 | | - expect(result).to include('>Viewer</option>') |
391 | | - end |
392 | | - end |
393 | | - |
394 | | - context 'using deprecated collection keyword in field helper' do |
395 | | - let(:form_field) do |
396 | | - Superform::Rails::Field.new(:role_ids, parent: nil, object: object) |
397 | | - end |
398 | | - |
399 | | - it 'shows deprecation warning' do |
400 | | - expect(form_field).to receive(:warn).with( |
401 | | - "[DEPRECATION] Superform::Rails::Field#select: " \ |
402 | | - "`collection:` parameter is deprecated. " \ |
403 | | - "Use `options:` instead." |
404 | | - ) |
405 | | - form_field.select(collection: [[1, 'Admin'], [2, 'Editor']]) |
406 | | - end |
407 | | - |
408 | | - it 'still renders select correctly' do |
409 | | - # Suppress deprecation warning for this test |
410 | | - allow(form_field).to receive(:warn) |
411 | | - result = render( |
412 | | - form_field.select(collection: [[1, 'Admin'], [2, 'Editor']]) |
413 | | - ) |
414 | | - expect(result).to include('>Admin</option>') |
415 | | - expect(result).to include('>Editor</option>') |
416 | | - end |
417 | | - end |
418 | | - |
419 | | - context 'when both options and collection are provided' do |
420 | | - let(:component) do |
421 | | - described_class.new( |
422 | | - field, |
423 | | - attributes: attributes, |
424 | | - options: [[1, 'Admin']], |
425 | | - collection: [[2, 'Editor']] |
426 | | - ) |
427 | | - end |
428 | | - |
429 | | - it 'does not show deprecation warning' do |
430 | | - expect_any_instance_of(described_class).not_to receive(:warn) |
431 | | - component |
432 | | - end |
433 | | - |
434 | | - it 'uses options parameter (takes precedence)' do |
435 | | - result = render(component) |
436 | | - expect(result).to include('>Admin</option>') |
437 | | - expect(result).not_to include('>Editor</option>') |
438 | | - end |
439 | | - end |
440 | | - end |
441 | 368 | end |
442 | 369 | # rubocop:enable Metrics/BlockLength |
0 commit comments