Skip to content

Commit

Permalink
Merge pull request #313 from DFE-Digital/improve-error-presenter-test…
Browse files Browse the repository at this point in the history
…ing-and-setup

Improve the setup of error presenters
  • Loading branch information
peteryates authored Sep 1, 2021
2 parents 40e319c + 02965ff commit c9add91
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
9 changes: 3 additions & 6 deletions lib/govuk_design_system_formbuilder/elements/error_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ def list
# error_messages from our object. Otherwise (if it's any other object),
# treat it like a presenter
def presenter
return @presenter.new(error_messages) if @presenter.is_a?(Class)

unless @presenter.respond_to?(:formatted_error_messages)
fail(ArgumentError, "error summary presenter doesn't implement #formatted_error_messages")
(@presenter.is_a?(Class) ? @presenter.new(error_messages) : @presenter).tap do |p|
fail(ArgumentError, "error summary presenter doesn't implement #formatted_error_messages") unless
p.respond_to?(:formatted_error_messages)
end

@presenter
end

def error_messages
Expand Down
21 changes: 15 additions & 6 deletions spec/govuk_design_system_formbuilder/builder/error_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,15 @@ def formatted_error_messages
end
end
end

context "when the custom presenter is a class that doesn't implement #formatted_error_messages" do
let(:non_presenter) { OpenStruct }
subject { builder.send(*args, presenter: non_presenter) }

specify "fails with an appropriate error message" do
expect { subject }.to raise_error(ArgumentError, "error summary presenter doesn't implement #formatted_error_messages")
end
end
end

context "as an instance" do
Expand All @@ -484,14 +493,14 @@ def formatted_error_messages
end
end
end
end

context "when the custom presenter doesn't implement #formatted_error_messages" do
let(:non_presenter) { "totally not a presenter" }
subject { builder.send(*args, presenter: non_presenter) }
context "when the custom presenter is an instance that doesn't implement #formatted_error_messages" do
let(:non_presenter) { "totally not a presenter" }
subject { builder.send(*args, presenter: non_presenter) }

specify "fails with an appropriate error message" do
expect { subject }.to raise_error(ArgumentError, "error summary presenter doesn't implement #formatted_error_messages")
specify "fails with an appropriate error message" do
expect { subject }.to raise_error(ArgumentError, "error summary presenter doesn't implement #formatted_error_messages")
end
end
end
end
Expand Down

0 comments on commit c9add91

Please sign in to comment.