Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cannot_start_yet argument to the task list status component #488

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module GovukComponent
class TaskListComponent::ItemComponent < GovukComponent::Base
renders_one :status, ->(text: nil, classes: [], html_attributes: {}, &block) do
renders_one :status, ->(text: nil, cannot_start_yet: false, classes: [], html_attributes: {}, &block) do
GovukComponent::TaskListComponent::StatusComponent.new(
id_prefix: @id_prefix,
count: @count,
text: text,
cannot_start_yet: cannot_start_yet,
classes: classes,
html_attributes: html_attributes,
&block
Expand Down Expand Up @@ -39,6 +40,10 @@ def initialize(title: nil, href: nil, hint: nil, count: nil, id_prefix: nil, sta
end

def call
if href.presence && status_content.cannot_start_yet
fail(ArgumentError, "item cannot have a href with status where cannot_start_yet: true")
end

adjusted_html_attributes = if href.present? || title&.href.present?
html_attributes_with_link_class
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module GovukComponent
class TaskListComponent::StatusComponent < GovukComponent::Base
attr_reader :id_prefix, :text, :count
attr_reader :id_prefix, :text, :cannot_start_yet, :count

def initialize(text: nil, id_prefix: nil, count: nil, classes: [], html_attributes: {})
@text = text
@count = count
@id_prefix = id_prefix
def initialize(text: nil, id_prefix: nil, count: nil, cannot_start_yet: false, classes: [], html_attributes: {})
@text = text
@count = count
@id_prefix = id_prefix
@cannot_start_yet = cannot_start_yet

super(classes: classes, html_attributes: html_attributes)
end
Expand All @@ -21,7 +22,13 @@ def render?
private

def default_attributes
{ class: %w(govuk-task-list__status), id: [id_prefix, count, "status"].compact.join("-") }
{
class: class_names(
"govuk-task-list__status",
"govuk-task-list__status--cannot-start-yet" => cannot_start_yet,
),
id: [id_prefix, count, "status"].compact.join("-"),
}
end

def status_text
Expand Down
2 changes: 1 addition & 1 deletion guide/content/components/task-list.slim
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ markdown:
code: task_list_with_cannot_start_yet) do

markdown:
Tasks can omit the `href` keyword if they cannot be started yet. When doing this you should add the `govuk-task-list__status--cannot-start-yet` to the status, and a hint to explain why the task cannot be started yet.
Tasks can omit the `href` keyword if they cannot be started yet. When doing this you should add `cannot_start_yet: true` to the status, and a hint to explain why the task cannot be started yet.

== render('/partials/example.*',
caption: "Task list with custom classes",
Expand Down
2 changes: 1 addition & 1 deletion guide/lib/examples/task_list_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def task_list_with_cannot_start_yet
- task_list.with_item(title: "Contact details", href: '#', status: "Completed")
- task_list.with_item(title: "Project details", href: '#', status: "Completed")
- task_list.with_item(title: "Funding", hint: "The funds will be announced on 1 April 2022") do |item|
- item.with_status(text: "Cannot start yet", classes: "govuk-task-list__status--cannot-start-yet")
- item.with_status(text: "Cannot start yet", cannot_start_yet: true)
SNIPPET
end

Expand Down
82 changes: 82 additions & 0 deletions spec/components/govuk_component/task_list_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@
end
end
end

context "when items have an element that cannot start yet" do
let(:list_item_two_kwargs) { { title: "Two", status: { text: "todo", cannot_start_yet: true } } }

specify "the status has an extra class marking that it cannot yet be started" do
expect(rendered_content).to have_tag(
"div",
with: { class: %w(govuk-task-list__status govuk-task-list__status--cannot-start-yet) },
text: "todo",
)
end
end

context "when an item has an element that cannot start yet and a href" do
let(:failing_component) do
render_inline(GovukComponent::TaskListComponent.new(**kwargs)) do |task_list|
task_list.with_item(title: "Two", href: "#", status: { text: "todo", cannot_start_yet: true })
end
end

specify "the component raises an error because an item has both a href and cannot_start_yet: true" do
expect { failing_component }.to raise_error(ArgumentError, /item cannot have a href with status where cannot_start_yet: true/)
end
end
end

describe "when rendered with blocks" do
Expand Down Expand Up @@ -130,6 +154,64 @@
end
end

describe "status cannot_start_yet" do
let(:title_text) { "Choose a course" }
let(:hint_text) { "Determine eligibility" }
let(:status_text) { "Not done" }
let(:cannot_start_yet) { false }
let(:item_kwargs) { {} }

subject! do
render_inline(GovukComponent::TaskListComponent.new(**kwargs)) do |task_list|
task_list.with_item(**item_kwargs) do |item|
helper.safe_join(
[
item.with_title(text: title_text, hint: hint_text),
item.with_status(text: status_text, cannot_start_yet: cannot_start_yet),
]
)
end
end
end

context "when false (default)" do
specify "there is no govuk-task-list__status--cannot-start-yet class" do
expect(rendered_content).not_to have_tag(".govuk-task-list__status--cannot-start-yet")
end
end

context "when true" do
let(:cannot_start_yet) { true }

specify "the status has an extra class marking that it cannot yet be started" do
expect(rendered_content).to have_tag(
"div",
with: { class: %w(govuk-task-list__status govuk-task-list__status--cannot-start-yet) },
text: status_text,
)
end
end

context "when an item has an element that cannot start yet and a href" do
let(:failing_component) do
render_inline(GovukComponent::TaskListComponent.new(**kwargs)) do |task_list|
task_list.with_item(href: "https://www.gov.uk") do |item|
helper.safe_join(
[
item.with_title(text: title_text, hint: hint_text),
item.with_status(text: status_text, cannot_start_yet: true),
]
)
end
end
end

specify "the component raises an error because an item has both a href and cannot_start_yet: true" do
expect { failing_component }.to raise_error(ArgumentError, /item cannot have a href with status where cannot_start_yet: true/)
end
end
end

describe "status presence" do
let(:status_class) { "govuk-task-list__status" }

Expand Down