|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe TimeParamsNormalizer do |
| 4 | + describe ".call" do |
| 5 | + subject(:call_normalizer) { described_class.call!(params:, field_name:) } |
| 6 | + |
| 7 | + let(:field_name) { :performed_at_time } |
| 8 | + |
| 9 | + context "when hour and minute are blank but seconds is present" do |
| 10 | + let(:params) do |
| 11 | + { |
| 12 | + "performed_at_time(4i)" => "", |
| 13 | + "performed_at_time(5i)" => "", |
| 14 | + "performed_at_time(6i)" => "0" |
| 15 | + } |
| 16 | + end |
| 17 | + |
| 18 | + it "blanks the seconds field" do |
| 19 | + call_normalizer |
| 20 | + expect(params["performed_at_time(6i)"]).to eq("") |
| 21 | + end |
| 22 | + |
| 23 | + it "returns the params hash" do |
| 24 | + expect(call_normalizer).to eq(params) |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + context "when hour and minute are nil but seconds is present" do |
| 29 | + let(:params) do |
| 30 | + { |
| 31 | + "performed_at_time(4i)" => nil, |
| 32 | + "performed_at_time(5i)" => nil, |
| 33 | + "performed_at_time(6i)" => "0" |
| 34 | + } |
| 35 | + end |
| 36 | + |
| 37 | + it "blanks the seconds field" do |
| 38 | + call_normalizer |
| 39 | + expect(params["performed_at_time(6i)"]).to eq("") |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context "when all time fields are blank" do |
| 44 | + let(:params) do |
| 45 | + { |
| 46 | + "performed_at_time(4i)" => "", |
| 47 | + "performed_at_time(5i)" => "", |
| 48 | + "performed_at_time(6i)" => "" |
| 49 | + } |
| 50 | + end |
| 51 | + |
| 52 | + it "leaves seconds blank" do |
| 53 | + call_normalizer |
| 54 | + expect(params["performed_at_time(6i)"]).to eq("") |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context "when hour and minute are blank and seconds is not present" do |
| 59 | + let(:params) do |
| 60 | + { "performed_at_time(4i)" => "", "performed_at_time(5i)" => "" } |
| 61 | + end |
| 62 | + |
| 63 | + it "does not add a seconds field" do |
| 64 | + call_normalizer |
| 65 | + expect(params).not_to have_key("performed_at_time(6i)") |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context "when hour is present but minute is blank" do |
| 70 | + let(:params) do |
| 71 | + { |
| 72 | + "performed_at_time(4i)" => "12", |
| 73 | + "performed_at_time(5i)" => "", |
| 74 | + "performed_at_time(6i)" => "0" |
| 75 | + } |
| 76 | + end |
| 77 | + |
| 78 | + it "does not modify seconds" do |
| 79 | + call_normalizer |
| 80 | + expect(params["performed_at_time(6i)"]).to eq("0") |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context "when minute is present but hour is blank" do |
| 85 | + let(:params) do |
| 86 | + { |
| 87 | + "performed_at_time(4i)" => "", |
| 88 | + "performed_at_time(5i)" => "30", |
| 89 | + "performed_at_time(6i)" => "0" |
| 90 | + } |
| 91 | + end |
| 92 | + |
| 93 | + it "does not modify seconds" do |
| 94 | + call_normalizer |
| 95 | + expect(params["performed_at_time(6i)"]).to eq("0") |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context "when hour and minute are both present" do |
| 100 | + let(:params) do |
| 101 | + { |
| 102 | + "performed_at_time(4i)" => "12", |
| 103 | + "performed_at_time(5i)" => "30", |
| 104 | + "performed_at_time(6i)" => "0" |
| 105 | + } |
| 106 | + end |
| 107 | + |
| 108 | + it "does not modify seconds" do |
| 109 | + call_normalizer |
| 110 | + expect(params["performed_at_time(6i)"]).to eq("0") |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + context "with a different field name" do |
| 115 | + let(:field_name) { :another_time } |
| 116 | + |
| 117 | + let(:params) do |
| 118 | + { |
| 119 | + "another_time(4i)" => "", |
| 120 | + "another_time(5i)" => "", |
| 121 | + "another_time(6i)" => "0" |
| 122 | + } |
| 123 | + end |
| 124 | + |
| 125 | + it "normalizes the correct field" do |
| 126 | + call_normalizer |
| 127 | + expect(params["another_time(6i)"]).to eq("") |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + context "when params contain other fields" do |
| 132 | + let(:params) do |
| 133 | + { |
| 134 | + "performed_at_date(1i)" => "2025", |
| 135 | + "performed_at_date(2i)" => "7", |
| 136 | + "performed_at_date(3i)" => "31", |
| 137 | + "performed_at_time(4i)" => "", |
| 138 | + "performed_at_time(5i)" => "", |
| 139 | + "performed_at_time(6i)" => "0", |
| 140 | + "notes" => "Some notes" |
| 141 | + } |
| 142 | + end |
| 143 | + |
| 144 | + it "only modifies the time seconds field" do |
| 145 | + call_normalizer |
| 146 | + expect(params["performed_at_date(1i)"]).to eq("2025") |
| 147 | + expect(params["performed_at_date(2i)"]).to eq("7") |
| 148 | + expect(params["performed_at_date(3i)"]).to eq("31") |
| 149 | + expect(params["performed_at_time(6i)"]).to eq("") |
| 150 | + expect(params["notes"]).to eq("Some notes") |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | +end |
0 commit comments