|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | describe SearchForm do
|
4 |
| - subject(:form) do |
5 |
| - described_class.new( |
6 |
| - consent_status:, |
7 |
| - date_of_birth_day:, |
8 |
| - date_of_birth_month:, |
9 |
| - date_of_birth_year:, |
10 |
| - missing_nhs_number:, |
11 |
| - programme_status:, |
12 |
| - q:, |
13 |
| - register_status:, |
14 |
| - session_status:, |
15 |
| - triage_status:, |
16 |
| - year_groups: |
17 |
| - ) |
18 |
| - end |
| 4 | + subject(:form) { described_class.new(**params, session:, request_path:) } |
19 | 5 |
|
| 6 | + let(:session) { {} } |
| 7 | + let(:empty_params) { {} } |
20 | 8 | let(:consent_status) { nil }
|
21 | 9 | let(:date_of_birth_day) { Date.current.day }
|
22 | 10 | let(:date_of_birth_month) { Date.current.month }
|
|
28 | 16 | let(:session_status) { nil }
|
29 | 17 | let(:triage_status) { nil }
|
30 | 18 | let(:year_groups) { %w[8 9 10 11] }
|
| 19 | + let(:request_path) { "/a-path" } |
| 20 | + |
| 21 | + let(:params) do |
| 22 | + { |
| 23 | + consent_status:, |
| 24 | + date_of_birth_day:, |
| 25 | + date_of_birth_month:, |
| 26 | + date_of_birth_year:, |
| 27 | + missing_nhs_number:, |
| 28 | + programme_status:, |
| 29 | + q:, |
| 30 | + register_status:, |
| 31 | + session_status:, |
| 32 | + triage_status:, |
| 33 | + year_groups: |
| 34 | + } |
| 35 | + end |
31 | 36 |
|
32 | 37 | context "for patients" do
|
33 | 38 | let(:scope) { Patient.all }
|
|
207 | 212 | end
|
208 | 213 | end
|
209 | 214 | end
|
| 215 | + |
| 216 | + describe "session filter persistence" do |
| 217 | + let(:another_path) { "/another-path" } |
| 218 | + |
| 219 | + context "when clear_filters param is present" do |
| 220 | + it "only clears filters for the current path" do |
| 221 | + described_class.new(**{ q: "John" }, session:, request_path:) |
| 222 | + described_class.new( |
| 223 | + **{ q: "Jane" }, |
| 224 | + session:, |
| 225 | + request_path: another_path |
| 226 | + ) |
| 227 | + |
| 228 | + described_class.new( |
| 229 | + **{ clear_filters: "true" }, |
| 230 | + session:, |
| 231 | + request_path: |
| 232 | + ) |
| 233 | + |
| 234 | + form1 = described_class.new(**empty_params, session:, request_path:) |
| 235 | + expect(form1.q).to be_nil |
| 236 | + |
| 237 | + form2 = |
| 238 | + described_class.new( |
| 239 | + **empty_params, |
| 240 | + session:, |
| 241 | + request_path: another_path |
| 242 | + ) |
| 243 | + expect(form2.q).to eq("Jane") |
| 244 | + end |
| 245 | + end |
| 246 | + |
| 247 | + context "when filters are present in params" do |
| 248 | + it "persists filters to be loaded in subsequent requests" do |
| 249 | + described_class.new(**{ q: "John" }, session:, request_path:) |
| 250 | + |
| 251 | + form = described_class.new(**empty_params, session:, request_path:) |
| 252 | + expect(form.q).to eq("John") |
| 253 | + end |
| 254 | + |
| 255 | + it "overwrites previously stored filters" do |
| 256 | + described_class.new(**{ q: "John" }, session:, request_path:) |
| 257 | + |
| 258 | + form1 = described_class.new(**{ q: "Jane" }, session:, request_path:) |
| 259 | + expect(form1.q).to eq("Jane") |
| 260 | + |
| 261 | + form2 = described_class.new(**empty_params, session:, request_path:) |
| 262 | + expect(form2.q).to eq("Jane") |
| 263 | + end |
| 264 | + |
| 265 | + it "overrides session filters when 'Any' option is selected (empty string)" do |
| 266 | + described_class.new( |
| 267 | + **{ consent_status: "given" }, |
| 268 | + session:, |
| 269 | + request_path: |
| 270 | + ) |
| 271 | + |
| 272 | + form1 = described_class.new(**empty_params, session:, request_path:) |
| 273 | + expect(form1.consent_status).to eq("given") |
| 274 | + |
| 275 | + form2 = |
| 276 | + described_class.new(**{ consent_status: "" }, session:, request_path:) |
| 277 | + expect(form2.consent_status).to eq("") |
| 278 | + |
| 279 | + form3 = described_class.new(**empty_params, session:, request_path:) |
| 280 | + expect(form3.consent_status).to eq("") |
| 281 | + end |
| 282 | + end |
| 283 | + |
| 284 | + context "when no filters are present in params but exist in session" do |
| 285 | + before do |
| 286 | + described_class.new( |
| 287 | + **{ q: "John", year_groups: %w[8 11], consent_status: "given" }, |
| 288 | + session:, |
| 289 | + request_path: |
| 290 | + ) |
| 291 | + end |
| 292 | + |
| 293 | + it "loads filters from the session" do |
| 294 | + form = described_class.new(**empty_params, session:, request_path:) |
| 295 | + |
| 296 | + expect(form.q).to eq("John") |
| 297 | + expect(form.year_groups).to eq([8, 11]) |
| 298 | + expect(form.consent_status).to eq("given") |
| 299 | + end |
| 300 | + end |
| 301 | + |
| 302 | + context "with path-specific filters" do |
| 303 | + it "maintains separate filters for different paths" do |
| 304 | + described_class.new(**{ q: "John" }, session:, request_path:) |
| 305 | + described_class.new( |
| 306 | + **{ q: "Jane" }, |
| 307 | + session:, |
| 308 | + request_path: another_path |
| 309 | + ) |
| 310 | + |
| 311 | + form1 = described_class.new(**empty_params, session:, request_path:) |
| 312 | + expect(form1.q).to eq("John") |
| 313 | + |
| 314 | + form2 = |
| 315 | + described_class.new( |
| 316 | + **empty_params, |
| 317 | + session:, |
| 318 | + request_path: another_path |
| 319 | + ) |
| 320 | + expect(form2.q).to eq("Jane") |
| 321 | + end |
| 322 | + end |
| 323 | + end |
210 | 324 | end
|
0 commit comments