Skip to content

Commit

Permalink
Investigate adding fixture-based specs
Browse files Browse the repository at this point in the history
This is an initial attempt at using GOV.UK Frontend's recent fixtures[0]
addition to ensure the HTML we're generating is in accordance with the
Design System.

After flipping a few default values it's not a million miles off, but
there are some snags:

1) Rails insists on adding a `data-disable-with` attribute to the
   submit[1], even when we attempt to disable it by passing `false`

2) Because in all cases (other than `#govuk_date_field`) we are merely
   wrapping the built-in Rails form helpers, we're not in control of the
   order of attributes. Comparing them directly will fail.

Both of these problems are demonstrated in the RSpec output:

  expected: `<input value="Start now" type="submit" name="start-now" class="govuk-button" data-module="govuk-button">`
       got: `<input type="submit" name="start-now" value="Start now" class="govuk-button" data-module="govuk-button" data-disable-with="Start now" />`

[0] alphagov/govuk-frontend#1830
[1] https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-submit_tag
  • Loading branch information
peteryates committed Jul 29, 2020
1 parent bf9577b commit 663c1ae
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
5 changes: 2 additions & 3 deletions guide/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion guide/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"govuk-frontend": "^3.7.0"
"govuk-frontend": "github:alphagov/govuk-frontend#634eeca76ee6f4399cba54cc13a11bd38f5c450a"
}
}
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ def govuk_check_box(attribute_name, value, hint_text: nil, label: {}, link_error
# = f.govuk_submit "Proceed", prevent_double_click: true do
# = link_to 'Cancel', some_other_path, class: 'govuk-button__secondary'
#
def govuk_submit(text = config.default_submit_button_text, warning: false, secondary: false, classes: nil, prevent_double_click: true, validate: false, disabled: false, &block)
def govuk_submit(text = config.default_submit_button_text, warning: false, secondary: false, classes: nil, prevent_double_click: false, validate: true, disabled: false, &block)
Elements::Submit.new(self, text, warning: warning, secondary: secondary, classes: classes, prevent_double_click: prevent_double_click, validate: validate, disabled: disabled, &block).html
end

Expand Down
9 changes: 8 additions & 1 deletion lib/govuk_design_system_formbuilder/elements/submit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,16 @@ def options
{
formnovalidate: !@validate,
disabled: @disabled,
name: @text.parameterize,
data: {
module: %(#{brand}-button),
'prevent-double-click': @prevent_double_click
'prevent-double-click': @prevent_double_click,

# this is an attempt to prevent Rails from adding a
# data-disable-with attr to the input element.
#
# it doesn't work (╯°□°)╯︵ ┻━┻
disable_with: false
}.select { |_k, v| v.present? }
}
end
Expand Down
32 changes: 32 additions & 0 deletions spec/fixtures/button_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
describe GOVUKDesignSystemFormBuilder::FormBuilder do
include_context 'setup builder'

describe 'Button' do
# we only generate `<input type=submit...` so ignore everything else like `<button>`
applicable = ['input']

fixtures = JSON
.parse(File.read('guide/node_modules/govuk-frontend/govuk/components/button/fixtures.json'))
.select { |f| f['name'].in?(applicable) }

fixtures.each do |fixture|
describe fixture['name'] do
let(:expected) { fixture['html'] }

let(:button_text) { fixture.dig('options', 'text') }
subject { builder.govuk_submit(button_text) }

# name: input
# options:
# element: input
# name: start-now
# text: Start now
# html: <input value="Start now" type="submit" name="start-now" class="govuk-button" data-module="govuk-button">

specify 'should match the HTML' do
expect(subject).to eql(expected)
end
end
end
end
end

0 comments on commit 663c1ae

Please sign in to comment.