Skip to content

Commit

Permalink
add values from params to date filter
Browse files Browse the repository at this point in the history
passing the filters to the date component so it
can check what its values should be.
  • Loading branch information
pezholio committed Dec 10, 2024
1 parent caf727f commit 34e6109
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
name: "last_updated_from[1i]",
label: "Year",
width: 4,
value: date_value(:last_updated_from, "1i"),
},
month: {
id: "last_updated_from_2i",
name: "last_updated_from[2i]",
label: "Month",
width: 2,
value: date_value(:last_updated_from, "2i"),
},
day: {
id: "last_updated_from_3i",
name: "last_updated_from[3i]",
label: "Day",
width: 2,
value: date_value(:last_updated_from, "3i"),
},
}) %>
<%= render("components/datetime_fields", {
Expand All @@ -34,17 +37,20 @@
name: "last_updated_to[1i]",
label: "Year",
width: 4,
value: date_value(:last_updated_to, "1i"),
},
month: {
id: "last_updated_to_2i",
name: "last_updated_to[2i]",
label: "Month",
width: 2,
value: date_value(:last_updated_to, "2i"),
},
day: {
id: "last_updated_to_3i",
name: "last_updated_to[3i]",
label: "Day",
width: 2,
value: date_value(:last_updated_to, "3i"),
},
}) %>
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
class ContentBlockManager::ContentBlock::Document::Index::DateFilterComponent < ViewComponent::Base
def initialize; end
def initialize(filters: nil)
@filters = filters
end

private

attr_reader :filters

def date_value(date, date_part)
filters&.dig(date, date_part)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
},
content: {
html: (
render(ContentBlockManager::ContentBlock::Document::Index::DateFilterComponent.new)
render(ContentBlockManager::ContentBlock::Document::Index::DateFilterComponent.new(filters: @filters))
),
},
expanded: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,36 @@ class ContentBlockManager::ContentBlock::Document::Index::DateFilterComponentTes

it "renders from and to dates" do
render_inline(ContentBlockManager::ContentBlock::Document::Index::DateFilterComponent.new)
assert_selector "input[name='last_updated[from(1i)]']"
assert_selector "input[name='last_updated[from(2i)]']"
assert_selector "input[name='last_updated[from(3i)]']"
assert_selector "input[name='last_updated_from[1i]']"
assert_selector "input[name='last_updated_from[2i]']"
assert_selector "input[name='last_updated_from[3i]']"

assert_selector "input[name='last_updated[to(1i)]']"
assert_selector "input[name='last_updated[to(2i)]']"
assert_selector "input[name='last_updated[to(3i)]']"
assert_selector "input[name='last_updated_to[1i]']"
assert_selector "input[name='last_updated_to[2i]']"
assert_selector "input[name='last_updated_to[3i]']"
end

it "keeps the values from the filter params" do
filters = {
last_updated_from: {
"3i" => "1",
"2i" => "2",
"1i" => "2025",
},
last_updated_to: {
"3i" => "3",
"2i" => "4",
"1i" => "2026",
},
}
render_inline(ContentBlockManager::ContentBlock::Document::Index::DateFilterComponent.new(filters:))

assert_selector "input[name='last_updated_from[3i]'][value=1]"
assert_selector "input[name='last_updated_from[2i]'][value='2']"
assert_selector "input[name='last_updated_from[1i]'][value='2025']"

assert_selector "input[name='last_updated_to[3i]'][value='3']"
assert_selector "input[name='last_updated_to[2i]'][value='4']"
assert_selector "input[name='last_updated_to[1i]'][value='2026']"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,17 @@ class ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent
assert_selector "option[selected='selected'][value=2]"
end

it "filters by last updated date" do
it "passes filters to Date component" do
filters = { lead_organisation: "2" }
date_component = ContentBlockManager::ContentBlock::Document::Index::DateFilterComponent.new(filters:)
ContentBlockManager::ContentBlock::Document::Index::DateFilterComponent.expects(:new).with(filters:).returns(date_component)

render_inline(
ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new(
filters: { lead_organisation: "2" },
filters:,
),
)

assert_selector ".govuk-accordion__section--expanded", text: "Last updated date"
assert_selector "h3", text: "From"
assert_selector "h3", text: "To"
end
end

0 comments on commit 34e6109

Please sign in to comment.