Browse Source

write helper method to support having the correct rent period checkboxes checked

pull/2389/head
Arthur Campbell 2 years ago
parent
commit
cce984908c
  1. 10
      app/helpers/organisations_helper.rb
  2. 35
      spec/helpers/organisations_helper_spec.rb

10
app/helpers/organisations_helper.rb

@ -37,4 +37,14 @@ module OrganisationsHelper
end end
end end
end end
def rent_periods_with_checked_attr(checked_periods: nil)
all_rent_periods = RentPeriod.rent_period_mappings
rent_periods = all_rent_periods.map do |code, period|
period_copy = period.clone
period_copy[:checked] = true if checked_periods.nil? || checked_periods.include?(code)
[code, period_copy]
end
rent_periods.to_h
end
end end

35
spec/helpers/organisations_helper_spec.rb

@ -20,4 +20,39 @@ RSpec.describe OrganisationsHelper do
) )
end end
end end
describe "rent_periods_with_checked_attr" do
let(:fake_rent_periods) do
{
"1" => { "value" => "Every minute" },
"2" => { "value" => "Every decade" },
}
end
before do
allow(RentPeriod).to receive(:rent_period_mappings).and_return fake_rent_periods
end
it "returns rent_period_mappings" do
actual = rent_periods_with_checked_attr
expect(actual.keys).to eq RentPeriod.rent_period_mappings.keys
end
context "when checked_periods is nil" do
it "returns all rent periods with checked true" do
actual = rent_periods_with_checked_attr
checked_attrs = actual.values.map { |p| p[:checked] }
expect(checked_attrs).to all be true
end
end
context "when checked_periods is not nil" do
it "returns the rent_periods with the correct values checked" do
checked_rent_period = "1"
actual = rent_periods_with_checked_attr(checked_periods: [checked_rent_period])
expect(actual[checked_rent_period][:checked]).to be true
expect(actual["2"][:checked]).to be_falsey
end
end
end
end end

Loading…
Cancel
Save