From ddae42ab765e4d32ec22f4d5b12db52b4b1648f3 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Mon, 8 Sep 2025 11:49:04 +0100 Subject: [PATCH] Ensure programme tags are ordered This ensures that when displaying a list of programme tags they are always ordered alphabetically, even if they're not ordered in that way when they're passed in. Generally we order the results of programmes when querying from the database, but there seems to be an issue in QA currently where preloading the programmes means they are not always ordered. This change guarantees that users will see the programmes in the correct order. --- .../app_programme_tags_component.rb | 13 +++++------- .../app_programme_tags_component_spec.rb | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 spec/components/app_programme_tags_component_spec.rb diff --git a/app/components/app_programme_tags_component.rb b/app/components/app_programme_tags_component.rb index 29586613e3..d7485d38da 100644 --- a/app/components/app_programme_tags_component.rb +++ b/app/components/app_programme_tags_component.rb @@ -5,16 +5,13 @@ def initialize(programmes) @programmes = programmes end - def call - safe_join( - programmes.map do - tag.strong(it.name, class: "nhsuk-tag nhsuk-tag--white") - end, - " " - ) - end + def call = safe_join(tags, " ") private attr_reader :programmes + + def names = programmes.map(&:name).sort + + def tags = names.map { tag.strong(it, class: "nhsuk-tag nhsuk-tag--white") } end diff --git a/spec/components/app_programme_tags_component_spec.rb b/spec/components/app_programme_tags_component_spec.rb new file mode 100644 index 0000000000..49fbea1f37 --- /dev/null +++ b/spec/components/app_programme_tags_component_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe AppProgrammeTagsComponent do + subject { render_inline(component) } + + let(:component) { described_class.new(programmes) } + + let(:programmes) do + [create(:programme, :menacwy), create(:programme, :td_ipv)] + end + + it { should have_content("MenACWY Td/IPV") } + + context "with unordered programmes" do + let(:programmes) do + [create(:programme, :td_ipv), create(:programme, :menacwy)] + end + + it { should have_content("MenACWY Td/IPV") } + end +end