Skip to content

Commit 7b12931

Browse files
alice-carrchao-xian
authored andcommitted
Include page heading from guidance in the read only file upload question when the form is live
When a file upload question has guidance, we want to include the guidance page header in the title of the card so that it provides context to the question text
1 parent b738941 commit 7b12931

6 files changed

Lines changed: 80 additions & 3 deletions

File tree

app/frontend/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/services/page_options_service.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ def initialize(page:, pages:, read_only: true)
1616
def all_options_for_answer_type
1717
options = []
1818

19-
options.concat(page_heading_options) if @page.respond_to?(:page_heading) && @page.page_heading.present?
19+
# Prioritize guidance markdown for file uploads
2020
options.concat(guidance_markdown_options) if @page.respond_to?(:guidance_markdown) && @page.guidance_markdown.present?
2121

22+
# Page heading for non-file types
23+
options.concat(page_heading_options) if @page.respond_to?(:page_heading) && @page.page_heading.present? && @page.answer_type != "file"
24+
25+
# File upload: If a page heading/guidance is present, show the question text in the body of the summary card
26+
# as we're using the heading in the title rather than the question text.
27+
options.concat(question_text_options) if @page.answer_type == "file" && @page.page_heading.present?
28+
29+
# Other answer types
2230
options.concat(hint_options) if @page.hint_text?.present?
2331
options.concat(generic_options) if @read_only && %w[address date text selection name].exclude?(@page.answer_type)
2432

@@ -40,6 +48,13 @@ def page_heading_options
4048
}]
4149
end
4250

51+
def question_text_options
52+
[{
53+
key: { text: I18n.t("reports.form_or_questions_list_table.headings.question_text") },
54+
value: { text: @page.question_text },
55+
}]
56+
end
57+
4358
def markdown_content
4459
safe_join(['<pre class="app-markdown-editor__markdown-example-block">'.html_safe, @page.guidance_markdown, "</pre>".html_safe])
4560
end

app/services/page_summary_card_data_service.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,20 @@ def build_data
2424
private
2525

2626
def build_title
27-
return "#{page_number(@page)}. #{@page.question_text}" unless @page.is_optional? && @page.answer_type != "selection"
27+
page_number = page_number(@page)
28+
heading = @page.page_heading
29+
question = @page.question_text
2830

29-
"#{page_number(@page)}. #{@page.question_text} (optional)"
31+
# Use the actual heading instead of the question text for the title on file upload type
32+
title = if @page.answer_type == "file" && heading.present?
33+
"#{page_number}. #{heading}"
34+
else
35+
"#{page_number}. #{question}"
36+
end
37+
38+
title += " (optional)" if @page.is_optional? && @page.answer_type != "selection"
39+
40+
title
3041
end
3142

3243
def page_number(page)

spec/factories/models/pages.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def as_json(*args)
3737
answer_type { Page::ANSWER_TYPES_WITHOUT_SETTINGS.sample }
3838
end
3939

40+
trait :with_file_upload_answer_type do
41+
answer_type { "file" }
42+
end
43+
4044
trait :with_selection_settings do
4145
transient do
4246
only_one_option { "true" }

spec/services/page_options_service_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,35 @@
348348
end
349349
end
350350
end
351+
352+
context "with answer type of file" do
353+
context "when file upload question does not have a page heading" do
354+
let(:page) { build :page, :with_file_upload_answer_type }
355+
356+
it "does not include the question text" do
357+
expect(page_options_service.all_options_for_answer_type).not_to include(
358+
{ key: { text: I18n.t("reports.form_or_questions_list_table.headings.question_text") },
359+
value: { text: page.question_text } },
360+
)
361+
end
362+
end
363+
364+
context "when file upload question contains guidance text" do
365+
let(:page) { build :page, :with_guidance, :with_file_upload_answer_type }
366+
367+
it "returns question text" do
368+
expect(page_options_service.all_options_for_answer_type).to include(
369+
{ key: { text: I18n.t("reports.form_or_questions_list_table.headings.question_text") },
370+
value: { text: page.question_text } },
371+
)
372+
end
373+
374+
it "does not return page heading" do
375+
expect(page_options_service.all_options_for_answer_type).not_to include(
376+
{ key: { text: I18n.t("page_options_service.page_heading") } },
377+
)
378+
end
379+
end
380+
end
351381
end
352382
end

spec/services/page_summary_card_data_service_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,21 @@
6060
end
6161
end
6262
end
63+
64+
context "when file upload question contains guidance text" do
65+
let(:page) { build :page, :with_guidance, :with_file_upload_answer_type, is_optional: }
66+
67+
it "includes the guidance text page heading as title" do
68+
expect(service.build_data[:card][:title]).to eq "1. #{page.page_heading}"
69+
end
70+
71+
context "when the question is optional" do
72+
let(:is_optional) { "true" }
73+
74+
it "includes a title with (optional) added to it" do
75+
expect(service.build_data[:card][:title]).to eq "1. #{page.page_heading} (optional)"
76+
end
77+
end
78+
end
6379
end
6480
end

0 commit comments

Comments
 (0)