2022-04-04 11:22:00 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe ListboxHelper do
|
|
|
|
subject do
|
|
|
|
tag = helper.gl_redirect_listbox_tag(items, selected, html_options)
|
|
|
|
Nokogiri::HTML.fragment(tag).children.first
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:sprite_icon).with(
|
|
|
|
'chevron-down',
|
|
|
|
css_class: 'gl-button-icon dropdown-chevron gl-icon'
|
|
|
|
).and_return('<span class="icon"></span>'.html_safe)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:selected) { 'bar' }
|
|
|
|
let(:html_options) { {} }
|
|
|
|
let(:items) do
|
|
|
|
[
|
|
|
|
{ value: 'foo', text: 'Foo' },
|
|
|
|
{ value: 'bar', text: 'Bar' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#gl_redirect_listbox_tag' do
|
|
|
|
it 'creates root element with expected classes' do
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(subject.classes).to include(
|
|
|
|
*%w[
|
|
|
|
dropdown
|
|
|
|
b-dropdown
|
|
|
|
gl-new-dropdown
|
|
|
|
btn-group
|
|
|
|
js-redirect-listbox
|
|
|
|
])
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets data attributes for items and selected' do
|
|
|
|
expect(subject.attributes['data-items'].value).to eq(items.to_json)
|
|
|
|
expect(subject.attributes['data-selected'].value).to eq(selected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds styled button' do
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(subject.at_css('button').classes).to include(
|
|
|
|
*%w[
|
|
|
|
btn
|
|
|
|
dropdown-toggle
|
|
|
|
btn-default
|
|
|
|
btn-md
|
|
|
|
gl-button
|
|
|
|
gl-dropdown-toggle
|
|
|
|
])
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets button text to selected item' do
|
|
|
|
expect(subject.at_css('button').content).to eq('Bar')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'given html_options' do
|
|
|
|
let(:html_options) { { class: 'test-class', data: { qux: 'qux' } } }
|
|
|
|
|
|
|
|
it 'applies them to the root element' do
|
|
|
|
expect(subject.attributes['data-qux'].value).to eq('qux')
|
|
|
|
expect(subject.classes).to include('test-class')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when selected does not match any item' do
|
2022-05-07 20:08:51 +05:30
|
|
|
where(selected: [nil, 'qux'])
|
2022-04-04 11:22:00 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
with_them do
|
|
|
|
it 'selects first item' do
|
|
|
|
expect(subject.at_css('button').content).to eq('Foo')
|
|
|
|
expect(subject.attributes['data-selected'].value).to eq('foo')
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|