|
520 | 520 | end |
521 | 521 |
|
522 | 522 | describe "#answer_value_groups" do |
523 | | - let(:form) { build_stubbed :form, :ready_for_routing } |
| 523 | + let(:form) { create :form } |
524 | 524 |
|
525 | | - let(:pages) do |
| 525 | + let!(:pages) do |
526 | 526 | [ |
527 | | - build_stubbed( |
| 527 | + create( |
528 | 528 | :page, |
529 | 529 | :with_selection_settings, |
530 | | - id: 101, |
| 530 | + form:, |
531 | 531 | selection_options:, |
532 | | - routing_conditions: conditions, |
533 | 532 | ), |
534 | | - build_stubbed(:page, id: 102), |
535 | | - build_stubbed(:page, id: 103), |
536 | | - build_stubbed(:page, id: 104), |
| 533 | + create(:page, form:), |
| 534 | + create(:page, form:), |
| 535 | + create(:page, form:), |
537 | 536 | ] |
538 | 537 | end |
539 | 538 |
|
|
547 | 546 |
|
548 | 547 | let(:conditions) do |
549 | 548 | [ |
550 | | - build_stubbed(:condition, routing_page_id: 101, answer_value: "Option 1", goto_page_id: 103), |
551 | | - build_stubbed(:condition, routing_page_id: 101, answer_value: "Option 2", goto_page_id: 103), |
552 | | - build_stubbed(:condition, routing_page_id: 101, answer_value: "Option 3", goto_page_id: 104), |
553 | | - build_stubbed(:condition, routing_page_id: 101, answer_value: nil, goto_page_id: nil, skip_to_end: true), |
| 549 | + create(:condition, routing_page_id: pages[0].id, answer_value: "Option 1", goto_page_id: pages[2].id), |
| 550 | + create(:condition, routing_page_id: pages[0].id, answer_value: "Option 2", goto_page_id: pages[2].id), |
| 551 | + create(:condition, routing_page_id: pages[0].id, answer_value: "Option 3", goto_page_id: pages[3].id), |
| 552 | + create(:condition, routing_page_id: pages[0].id, answer_value: nil, goto_page_id: nil, skip_to_end: true), |
554 | 553 | ] |
555 | 554 | end |
556 | 555 |
|
| 556 | + let(:page_list_component) { described_class.new(pages: form.reload.pages, form:) } |
| 557 | + |
| 558 | + before do |
| 559 | + conditions |
| 560 | + end |
| 561 | + |
557 | 562 | it "groups conditions by goto_page_id" do |
558 | 563 | expected = [ |
559 | | - [103, [conditions.first, conditions.second]], |
560 | | - [104, [conditions.third]], |
561 | | - [nil, [conditions.fourth]], |
| 564 | + [pages[2].id, [conditions[0], conditions[1]]], |
| 565 | + [pages[3].id, [conditions[2]]], |
| 566 | + [nil, [conditions[3]]], |
562 | 567 | ] |
563 | | - result = page_list_component.answer_value_groups(pages.first) |
| 568 | + result = page_list_component.answer_value_groups(form.pages.first) |
564 | 569 | expect(result).to eq expected |
565 | 570 | end |
566 | 571 |
|
|
575 | 580 |
|
576 | 581 | it "returns the conditions in the same order" do |
577 | 582 | expected = [ |
578 | | - [103, [conditions.second, conditions.first]], |
579 | | - [104, [conditions.third]], |
580 | | - [nil, [conditions.fourth]], |
| 583 | + [pages[2].id, [conditions[1], conditions[0]]], |
| 584 | + [pages[3].id, [conditions[2]]], |
| 585 | + [nil, [conditions[3]]], |
| 586 | + ] |
| 587 | + |
| 588 | + result = page_list_component.answer_value_groups(form.pages.first) |
| 589 | + expect(result).to eq expected |
| 590 | + end |
| 591 | + end |
| 592 | + |
| 593 | + context "when page positions are not in ID order" do |
| 594 | + let!(:pages) do |
| 595 | + [ |
| 596 | + create(:page, :with_selection_settings, form:, selection_options:, position: 1), |
| 597 | + create(:page, form:, position: 4), |
| 598 | + create(:page, form:, position: 2), |
| 599 | + create(:page, form:, position: 3), |
| 600 | + ] |
| 601 | + end |
| 602 | + |
| 603 | + it "sorts groups by page position, not page id" do |
| 604 | + expected = [ |
| 605 | + [pages[2].id, [conditions[0], conditions[1]]], |
| 606 | + [pages[3].id, [conditions[2]]], |
| 607 | + [nil, [conditions[3]]], |
581 | 608 | ] |
582 | 609 |
|
583 | | - result = page_list_component.answer_value_groups(pages.first) |
| 610 | + result = page_list_component.answer_value_groups(form.pages.first) |
584 | 611 | expect(result).to eq expected |
585 | 612 | end |
586 | 613 | end |
|
0 commit comments