From c503857c8080fa589f9a7e11f6c49574ca6986a5 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Tue, 22 Oct 2024 11:21:18 +0100 Subject: [PATCH] Use Rails validation to check minimal configuration --- .../govuk_publishing_components/components/_chart.html.erb | 7 ++++--- spec/components/chart_spec.rb | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/views/govuk_publishing_components/components/_chart.html.erb b/app/views/govuk_publishing_components/components/_chart.html.erb index 81112c6e28..d58adbe3e1 100644 --- a/app/views/govuk_publishing_components/components/_chart.html.erb +++ b/app/views/govuk_publishing_components/components/_chart.html.erb @@ -42,8 +42,9 @@ enableInteractivity = false if minimal textPosition = nil textPosition = 'none' if minimal - valid_minimal = true - valid_minimal = false if minimal && !minimal_link # minimal version must include a link + if minimal && !minimal_link + raise ArgumentError, "Minimal version must include a link" + end chart_library_options = { chartArea: { width: '80%', height: '60%' }, @@ -80,7 +81,7 @@ end end %> -<% if rows.length > 0 && keys.length > 0 && valid_minimal %> +<% if rows.length > 0 && keys.length > 0 %> <%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" if @external_script[:loaded] == 1 %> <%= tag.div(**component_helper.all_attributes) do %> <% if chart_heading && !minimal %> diff --git a/spec/components/chart_spec.rb b/spec/components/chart_spec.rb index 34d14937c8..3ca62ebc13 100644 --- a/spec/components/chart_spec.rb +++ b/spec/components/chart_spec.rb @@ -122,7 +122,9 @@ def component_name it "does not render a minimal version if a link is not supplied" do data[:minimal] = true - assert_empty render_component(data) + expect { + render_component(data) + }.to raise_error("Minimal version must include a link") end it "only calls an external script once" do