debian-mirror-gitlab/spec/components/pajamas/checkbox_component_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
3.3 KiB
Ruby
Raw Normal View History

2022-07-23 23:45:48 +05:30
# frozen_string_literal: true
require "spec_helper"
RSpec.describe Pajamas::CheckboxComponent, :aggregate_failures, type: :component do
include FormBuilderHelpers
let_it_be(:method) { :view_diffs_file_by_file }
let_it_be(:label) { "Show one file at a time on merge request's Changes tab" }
let_it_be(:help_text) { 'Instead of all the files changed, show only one file at a time.' }
context 'with default options' do
before do
fake_form_for do |form|
render_inline(
described_class.new(
form: form,
method: method,
label: label
)
)
end
end
include_examples 'it renders unchecked checkbox with value of `1`'
include_examples 'it does not render help text'
it 'renders hidden input with value of `0`' do
2022-08-13 15:12:31 +05:30
expect(page).to have_field('user[view_diffs_file_by_file]', type: 'hidden', with: '0')
2022-07-23 23:45:48 +05:30
end
end
context 'with custom options' do
let_it_be(:checked_value) { 'yes' }
let_it_be(:unchecked_value) { 'no' }
let_it_be(:checkbox_options) { { class: 'checkbox-foo-bar', checked: true } }
let_it_be(:label_options) { { class: 'label-foo-bar' } }
before do
fake_form_for do |form|
render_inline(
described_class.new(
form: form,
method: method,
label: label,
help_text: help_text,
checked_value: checked_value,
unchecked_value: unchecked_value,
checkbox_options: checkbox_options,
label_options: label_options
)
)
end
end
include_examples 'it renders help text'
it 'renders checked checkbox with value of `yes`' do
2022-08-13 15:12:31 +05:30
expect(page).to have_checked_field(label, with: checked_value, class: checkbox_options[:class])
2022-07-23 23:45:48 +05:30
end
it 'adds CSS class to label' do
2022-08-13 15:12:31 +05:30
expect(page).to have_selector('label.label-foo-bar')
2022-07-23 23:45:48 +05:30
end
it 'renders hidden input with value of `no`' do
2022-08-13 15:12:31 +05:30
expect(page).to have_field('user[view_diffs_file_by_file]', type: 'hidden', with: unchecked_value)
2022-07-23 23:45:48 +05:30
end
end
context 'with `label` slot' do
before do
fake_form_for do |form|
render_inline(
described_class.new(
form: form,
method: method
)
) do |c|
2023-07-09 08:55:56 +05:30
c.with_label { label }
2022-07-23 23:45:48 +05:30
end
end
end
include_examples 'it renders unchecked checkbox with value of `1`'
end
context 'with `help_text` slot' do
before do
fake_form_for do |form|
render_inline(
described_class.new(
form: form,
method: method,
label: label
)
) do |c|
2023-07-09 08:55:56 +05:30
c.with_help_text { help_text }
2022-07-23 23:45:48 +05:30
end
end
end
include_examples 'it renders unchecked checkbox with value of `1`'
include_examples 'it renders help text'
end
context 'with `label` and `help_text` slots' do
before do
fake_form_for do |form|
render_inline(
described_class.new(
form: form,
method: method
)
) do |c|
2023-07-09 08:55:56 +05:30
c.with_label { label }
c.with_help_text { help_text }
2022-07-23 23:45:48 +05:30
end
end
end
include_examples 'it renders unchecked checkbox with value of `1`'
include_examples 'it renders help text'
end
end