|
| 1 | +FactoryBot.define do |
| 2 | + factory :form_resource, class: "Api::V1::FormResource" do |
| 3 | + sequence(:name) { |n| "Form #{n}" } |
| 4 | + sequence(:form_slug) { |n| "form-#{n}" } |
| 5 | + has_draft_version { true } |
| 6 | + submission_email { Faker::Internet.email(domain: "example.gov.uk") } |
| 7 | + submission_type { "email" } |
| 8 | + privacy_policy_url { Faker::Internet.url(host: "gov.uk") } |
| 9 | + language { "en" } |
| 10 | + live_at { nil } |
| 11 | + support_email { nil } |
| 12 | + support_phone { nil } |
| 13 | + support_url { nil } |
| 14 | + support_url_text { nil } |
| 15 | + what_happens_next_markdown { nil } |
| 16 | + declaration_text { nil } |
| 17 | + question_section_completed { false } |
| 18 | + declaration_section_completed { false } |
| 19 | + share_preview_completed { false } |
| 20 | + has_routing_errors { false } |
| 21 | + creator_id { nil } |
| 22 | + ready_for_live { false } |
| 23 | + incomplete_tasks { %i[missing_pages missing_privacy_policy_url missing_contact_details missing_what_happens_next share_preview_not_completed] } |
| 24 | + state { :draft } |
| 25 | + |
| 26 | + transient do |
| 27 | + statuses { { declaration_status: "not_started", make_live_status: "cannot_start", name_status: "completed", pages_status: "not_started", privacy_policy_status: "not_started", support_contact_details_status: "not_started", what_happens_next_status: "not_started" } } |
| 28 | + end |
| 29 | + task_statuses { OpenStruct.new(attributes: statuses) } |
| 30 | + |
| 31 | + trait :new_form do |
| 32 | + submission_email { "" } |
| 33 | + privacy_policy_url { "" } |
| 34 | + state { :draft } |
| 35 | + end |
| 36 | + |
| 37 | + trait :with_id do |
| 38 | + sequence(:id) { |n| n } |
| 39 | + end |
| 40 | + |
| 41 | + trait :ready_for_live do |
| 42 | + with_pages |
| 43 | + support_email { Faker::Internet.email(domain: "example.gov.uk") } |
| 44 | + what_happens_next_markdown { "We usually respond to applications within 10 working days." } |
| 45 | + question_section_completed { true } |
| 46 | + declaration_section_completed { true } |
| 47 | + share_preview_completed { true } |
| 48 | + ready_for_live { true } |
| 49 | + incomplete_tasks { [] } |
| 50 | + transient do |
| 51 | + statuses { { declaration_status: "completed", make_live_status: "not_started", name_status: "completed", pages_status: "completed", privacy_policy_status: "completed", support_contact_details_status: "completed", what_happens_next_status: "completed" } } |
| 52 | + end |
| 53 | + task_statuses { OpenStruct.new(attributes: statuses) } |
| 54 | + end |
| 55 | + |
| 56 | + trait :live do |
| 57 | + ready_for_live |
| 58 | + live_at { Time.zone.now } |
| 59 | + has_draft_version { false } |
| 60 | + state { :live } |
| 61 | + end |
| 62 | + |
| 63 | + trait :live_with_draft do |
| 64 | + live |
| 65 | + state { :live_with_draft } |
| 66 | + has_draft_version { true } |
| 67 | + end |
| 68 | + |
| 69 | + trait :archived do |
| 70 | + live |
| 71 | + state { :archived } |
| 72 | + end |
| 73 | + |
| 74 | + trait :archived_with_draft do |
| 75 | + live |
| 76 | + state { :archived_with_draft } |
| 77 | + end |
| 78 | + |
| 79 | + trait :with_active_resource do |
| 80 | + task_statuses { statuses } |
| 81 | + end |
| 82 | + |
| 83 | + trait :with_pages do |
| 84 | + transient do |
| 85 | + pages_count { 5 } |
| 86 | + end |
| 87 | + |
| 88 | + pages do |
| 89 | + Array.new(pages_count) { association(:page_resource) } |
| 90 | + end |
| 91 | + |
| 92 | + question_section_completed { true } |
| 93 | + end |
| 94 | + |
| 95 | + trait :with_support do |
| 96 | + support_email { Faker::Internet.email(domain: "example.gov.uk") } |
| 97 | + support_phone { Faker::Lorem.paragraph(sentence_count: 2, supplemental: true, random_sentences_to_add: 4) } |
| 98 | + support_url { Faker::Internet.url(host: "gov.uk") } |
| 99 | + support_url_text { Faker::Lorem.sentence(word_count: 1, random_words_to_add: 4) } |
| 100 | + end |
| 101 | + |
| 102 | + trait :ready_for_routing do |
| 103 | + transient do |
| 104 | + pages_count { 5 } |
| 105 | + end |
| 106 | + |
| 107 | + pages do |
| 108 | + Array.new(pages_count) { association(:page_resource, :with_selection_settings) } |
| 109 | + end |
| 110 | + |
| 111 | + after(:build) do |form| |
| 112 | + if form.pages.present? |
| 113 | + # assign position and next_page to each page |
| 114 | + form.pages.each.with_index(1).each_cons(2) do |page_with_index, next_page_with_index| |
| 115 | + page, page_index = page_with_index |
| 116 | + next_page, _next_page_index = next_page_with_index |
| 117 | + |
| 118 | + page.position = page_index |
| 119 | + page.next_page = next_page&.id |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + trait :ready_for_api_routing do |
| 126 | + transient do |
| 127 | + pages_count { 5 } |
| 128 | + end |
| 129 | + |
| 130 | + pages do |
| 131 | + Array.new(pages_count) { association(:page_resource, :with_api_selection_settings) } |
| 132 | + end |
| 133 | + |
| 134 | + after(:build) do |form| |
| 135 | + if form.pages.present? |
| 136 | + # assign position and next_page to each page |
| 137 | + form.pages.each.with_index(1).each_cons(2) do |page_with_index, next_page_with_index| |
| 138 | + page, page_index = page_with_index |
| 139 | + next_page, _next_page_index = next_page_with_index |
| 140 | + |
| 141 | + page.position = page_index |
| 142 | + page.next_page = next_page&.id |
| 143 | + end |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + trait :missing_pages do |
| 149 | + incomplete_tasks { %i[missing_pages] } |
| 150 | + end |
| 151 | + end |
| 152 | +end |
0 commit comments