Skip to content

Commit

Permalink
Fix block syntax for buttons
Browse files Browse the repository at this point in the history
If you use the block syntax, then the href is actually confusingly the variable named `name` as it is the first argument, rather than the one named `href`.

Unfortunately the tests didn't pick this up as the mock for `url_for` always returned the right variable, even if passed `nil`. Swapping this out for a minimal implementation of `url_for` avoids these false positives.
  • Loading branch information
frankieroberto committed Nov 29, 2023
1 parent 3151c54 commit b1a98c2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/helpers/govuk_link_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def govuk_button_to(name, href = nil, disabled: false, inverse: false, secondary
button_text = build_text(name, visually_hidden_prefix: visually_hidden_prefix, visually_hidden_suffix: visually_hidden_suffix)

if block_given?
button_to(href, **button_args, &block)
button_to(name, **button_args, &block)
else
button_to(button_text, href, **button_args)
end
Expand All @@ -37,7 +37,7 @@ def govuk_button_link_to(name, href = nil, new_tab: false, disabled: false, inve
button_text = build_text(name, visually_hidden_prefix: visually_hidden_prefix, visually_hidden_suffix: visually_hidden_suffix)

if block_given?
link_to(href, **button_args, &block)
link_to(name, **button_args, &block)
else
link_to(button_text, href, **button_args)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/helpers/govuk_link_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
include ActionView::Helpers::UrlHelper

before do
allow(self).to receive(:url_for).with(any_args).and_return("/world")
def url_for(path)
return path
end
end

describe "govuk_link_to" do
Expand Down

0 comments on commit b1a98c2

Please sign in to comment.