Skip to content

Commit

Permalink
Add specs ensuring helpers with plain ruby objects
Browse files Browse the repository at this point in the history
  • Loading branch information
peteryates committed Jan 7, 2020
1 parent f50cc5a commit 3370995
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 0 deletions.
Expand Up @@ -38,6 +38,10 @@
it_behaves_like 'a field that supports setting the legend via localisation'
it_behaves_like 'a field that supports setting the hint via localisation'

it_behaves_like 'a field that accepts a plain ruby object' do
let(:described_element) { ['input', { with: { type: 'checkbox' }, count: projects.size }] }
end

describe 'check boxes' do
specify 'output should contain the correct number of check boxes' do
expect(subject).to have_tag('div', with: { 'data-module' => 'govuk-checkboxes' }) do |cb|
Expand Down
Expand Up @@ -28,6 +28,10 @@
it_behaves_like 'a field that supports setting the hint via localisation'
it_behaves_like 'a field that supports setting the legend via localisation'

it_behaves_like 'a field that accepts a plain ruby object' do
let(:described_element) { 'fieldset' }
end

context 'when no block is supplied' do
subject { builder.send(*args) }
specify { expect { subject }.to raise_error(NoMethodError, /undefined method.*call/) }
Expand Down
4 changes: 4 additions & 0 deletions spec/govuk_design_system_formbuilder/builder/date_spec.rb
Expand Up @@ -62,6 +62,10 @@
it_behaves_like 'a field that supports setting the legend via localisation'
it_behaves_like 'a field that supports setting the hint via localisation'

it_behaves_like 'a field that accepts a plain ruby object' do
let(:described_element) { ['input', { count: 3 }] }
end

specify 'should output a form group with fieldset, date group and 3 inputs and labels' do
expect(subject).to have_tag('div', with: { class: 'govuk-form-group' }) do |fg|
expect(fg).to have_tag('fieldset', with: { class: 'govuk-fieldset' }) do |fs|
Expand Down
4 changes: 4 additions & 0 deletions spec/govuk_design_system_formbuilder/builder/file_spec.rb
Expand Up @@ -40,6 +40,10 @@
it_behaves_like 'a field that supports setting the label via localisation'
it_behaves_like 'a field that supports setting the hint via localisation'

it_behaves_like 'a field that accepts a plain ruby object' do
let(:described_element) { ['input', with: { type: 'file' }] }
end

describe 'additional attributes' do
subject { builder.send(method, attribute, accept: 'image/*', multiple: true) }

Expand Down
Expand Up @@ -44,6 +44,10 @@
it_behaves_like 'a field that supports setting the hint via localisation'
it_behaves_like 'a field that supports setting the legend via localisation'

it_behaves_like 'a field that accepts a plain ruby object' do
let(:described_element) { ['input', { with: { type: 'radio' }, count: colours.size }] }
end

context 'radio buttons' do
specify 'each radio button should have the correct classes' do
expect(subject).to have_tag('input', with: { class: %w(govuk-radios__input) }, count: colours.size)
Expand Down
Expand Up @@ -32,6 +32,12 @@
it_behaves_like 'a field that supports setting the hint via localisation'
it_behaves_like 'a field that supports setting the legend via localisation'

it_behaves_like 'a field that accepts a plain ruby object' do
subject { builder.send(*args) {} }

let(:described_element) { 'fieldset' }
end

context 'when a block containing radio buttons is supplied' do
specify 'output should be a form group containing a form group and fieldset' do
expect(subject).to have_tag('div', with: { class: 'govuk-form-group' }) do |fg|
Expand Down
4 changes: 4 additions & 0 deletions spec/govuk_design_system_formbuilder/builder/select_spec.rb
Expand Up @@ -30,6 +30,10 @@
it_behaves_like 'a field that supports setting the label via localisation'
it_behaves_like 'a field that supports setting the hint via localisation'

it_behaves_like 'a field that accepts a plain ruby object' do
let(:described_element) { 'select' }
end

specify 'output should be a form group containing a label and select box' do
expect(subject).to have_tag('div', with: { class: 'govuk-form-group' }) do |fg|
expect(fg).to have_tag('select')
Expand Down
Expand Up @@ -59,6 +59,10 @@
it_behaves_like 'a field that supports setting the label via localisation'
it_behaves_like 'a field that supports setting the hint via localisation'

it_behaves_like 'a field that accepts a plain ruby object' do
let(:described_element) { 'textarea' }
end

specify 'should have the correct classes' do
expect(subject).to have_tag('textarea', with: { class: 'govuk-textarea' })
end
Expand Down
19 changes: 19 additions & 0 deletions spec/support/shared/shared_plain_ruby_object_examples.rb
@@ -0,0 +1,19 @@
class Biscuit
attr_accessor :name, :weight, :calories

def initialize(name, weight, calories)
self.name = name
self.weight = weight
self.calories = calories
end
end

shared_examples 'a field that accepts a plain ruby object' do
let(:object) { Biscuit.new('Pick-up!', 28, 143) }
let(:object_name) { :biscuit }
let(:attribute) { :calories }

specify 'should render the element successfully' do
expect(subject).to have_tag(*described_element)
end
end
4 changes: 4 additions & 0 deletions spec/support/shared/shared_text_field_examples.rb
Expand Up @@ -40,6 +40,10 @@
it_behaves_like 'a field that supports setting the label via localisation'
it_behaves_like 'a field that supports setting the hint via localisation'

it_behaves_like 'a field that accepts a plain ruby object' do
let(:described_element) { 'input' }
end

context 'extra attributes' do
let(:regular_args) { { label: { text: 'What should we call you?' } } }

Expand Down

0 comments on commit 3370995

Please sign in to comment.