|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require "rails_helper" |
2 | 4 |
|
| 5 | +VOLUNTEER_SELECT_ID = "multiple-select-field2" |
| 6 | +SUPERVISOR_SELECT_ID = "multiple-select-field1" |
| 7 | +CONTACT_TYPE_SELECT_ID = "multiple-select-field3" |
| 8 | +CONTACT_TYPE_GROUP_SELECT_ID = "multiple-select-field4" |
| 9 | + |
3 | 10 | RSpec.describe "reports", :js, type: :system do |
4 | | - context "volunteer user" do |
5 | | - it "redirects to root" do |
| 11 | + shared_examples "downloads report button" do |button_name, feedback| |
| 12 | + it "downloads #{button_name.downcase}", :aggregate_failures do |
| 13 | + expect(page).to have_button(button_name) |
| 14 | + click_on button_name |
| 15 | + expect(page).to have_text(feedback) |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + shared_examples "downloads case contacts report with filter" do |filter_name, setup_action, filter_action| |
| 20 | + it "downloads case contacts report with #{filter_name}" do |
| 21 | + instance_exec(&setup_action) if setup_action |
| 22 | + visit reports_path |
| 23 | + instance_exec(&filter_action) |
| 24 | + click_on "Download Report" |
| 25 | + expect(page).to have_text("Downloading Report") |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + shared_examples "empty select downloads report" do |select_id, description| |
| 30 | + it "renders the #{description} select with no options and downloads the report" do |
| 31 | + expect(page).to have_select(select_id, options: []) |
| 32 | + click_on "Download Report" |
| 33 | + expect(page).to have_text("Downloading Report") |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context "with a volunteer user" do |
| 38 | + before do |
6 | 39 | user = create(:volunteer) |
7 | 40 |
|
8 | 41 | sign_in user |
9 | 42 | visit reports_path |
| 43 | + end |
10 | 44 |
|
| 45 | + it "redirects to root", :aggregate_failures do |
11 | 46 | expect(page).not_to have_text "Case Contacts Report" |
12 | 47 | expect(page).to have_text "Sorry, you are not authorized to perform this action." |
13 | 48 | end |
14 | 49 | end |
15 | 50 |
|
16 | | - context "supervisor user" do |
17 | | - it "renders form elements", :js do |
18 | | - user = create(:supervisor) |
| 51 | + %i[supervisor casa_admin].each do |role| |
| 52 | + # rubocop:disable RSpec/MultipleMemoizedHelpers |
| 53 | + context "with a #{role} user" do |
| 54 | + let(:user) { create(role) } |
| 55 | + let(:volunteer_name) { Faker::Name.unique.name } |
| 56 | + let(:supervisor_name) { Faker::Name.unique.name } |
| 57 | + let(:contact_type_name) { Faker::Lorem.unique.word } |
| 58 | + let(:contact_type_group_name) { Faker::Lorem.unique.word } |
| 59 | + let(:filter_start_date) { "2025-01-01" } |
| 60 | + let(:filter_end_date) { "2025-10-08" } |
19 | 61 |
|
20 | | - sign_in user |
21 | | - visit reports_path |
| 62 | + before do |
| 63 | + sign_in user |
| 64 | + visit reports_path |
| 65 | + end |
22 | 66 |
|
23 | | - expect(page).to have_text "Case Contacts Report" |
24 | | - expect(page).to have_field("report_start_date", with: 6.months.ago.strftime("%Y-%m-%d")) |
25 | | - expect(page).to have_field("report_end_date", with: Date.today) |
26 | | - expect(page).to have_text "Assigned To" |
27 | | - expect(page).to have_text "Volunteers" |
28 | | - expect(page).to have_text "Contact Type" |
29 | | - expect(page).to have_text "Contact Type Group" |
30 | | - expect(page).to have_text "Want Driving Reimbursement" |
31 | | - expect(page).to have_text "Contact Made" |
32 | | - expect(page).to have_text "Transition Aged Youth" |
33 | | - expect(page).to have_field("Both", count: 3) |
34 | | - expect(page).to have_field("Yes", count: 3) |
35 | | - expect(page).to have_field("No", count: 3) |
36 | | - end |
| 67 | + it "renders form elements", :aggregate_failures do |
| 68 | + expect(page).to have_text "Case Contacts Report" |
| 69 | + expect(page).to have_field("report_start_date", with: 6.months.ago.strftime("%Y-%m-%d")) |
| 70 | + expect(page).to have_field("report_end_date", with: Date.today) |
| 71 | + expect(page).to have_text "Assigned To" |
| 72 | + expect(page).to have_text "Volunteers" |
| 73 | + expect(page).to have_text "Contact Type" |
| 74 | + expect(page).to have_text "Contact Type Group" |
| 75 | + expect(page).to have_text "Want Driving Reimbursement" |
| 76 | + expect(page).to have_text "Contact Made" |
| 77 | + expect(page).to have_text "Transition Aged Youth" |
| 78 | + expect(page).to have_field("Both", count: 3) |
| 79 | + expect(page).to have_field("Yes", count: 3) |
| 80 | + expect(page).to have_field("No", count: 3) |
| 81 | + end |
37 | 82 |
|
38 | | - it "downloads report", :js do |
39 | | - user = create(:supervisor) |
| 83 | + it "downloads case contacts report with default filters" do |
| 84 | + click_on "Download Report" |
| 85 | + expect(page).to have_text("Downloading Report") |
| 86 | + end |
40 | 87 |
|
41 | | - sign_in user |
42 | | - visit reports_path |
| 88 | + include_examples "downloads report button", "Mileage Report", "Downloading Mileage Report" |
| 89 | + include_examples "downloads report button", "Missing Data Report", "Downloading Missing Data Report" |
| 90 | + include_examples "downloads report button", "Learning Hours Report", "Downloading Learning Hours Report" |
| 91 | + include_examples "downloads report button", "Export Volunteers Emails", "Downloading Export Volunteers Emails" |
| 92 | + include_examples "downloads report button", "Followups Report", "Downloading Followups Report" |
| 93 | + include_examples "downloads report button", "Placements Report", "Downloading Placements Report" |
43 | 94 |
|
44 | | - click_on "Download Report" |
45 | | - expect(page).to have_text("Downloading Report") |
46 | | - end |
47 | | - end |
| 95 | + shared_examples "case contacts report with filter" do |filter_type| |
| 96 | + it "downloads case contacts report with #{filter_type}" do |
| 97 | + click_on "Download Report" |
| 98 | + expect(page).to have_text("Downloading Report") |
| 99 | + end |
| 100 | + end |
48 | 101 |
|
49 | | - context "casa_admin user" do |
50 | | - it "renders form elements", :js do |
51 | | - user = create(:casa_admin) |
| 102 | + context "with an assigned supervisor filter" do |
| 103 | + before do |
| 104 | + create(:supervisor, casa_org: user.casa_org, display_name: supervisor_name) |
| 105 | + visit reports_path |
| 106 | + select_report_filter_option(SUPERVISOR_SELECT_ID, supervisor_name) |
| 107 | + end |
52 | 108 |
|
53 | | - sign_in user |
54 | | - visit reports_path |
| 109 | + include_examples "case contacts report with filter", "assigned supervisor" |
| 110 | + end |
55 | 111 |
|
56 | | - expect(page).to have_text "Case Contacts Report" |
57 | | - expect(page).to have_field("report_start_date", with: 6.months.ago.strftime("%Y-%m-%d")) |
58 | | - expect(page).to have_field("report_end_date", with: Date.today) |
59 | | - expect(page).to have_text "Assigned To" |
60 | | - expect(page).to have_text "Volunteers" |
61 | | - expect(page).to have_text "Contact Type" |
62 | | - expect(page).to have_text "Contact Type Group" |
63 | | - expect(page).to have_text "Want Driving Reimbursement" |
64 | | - expect(page).to have_text "Contact Made" |
65 | | - expect(page).to have_text "Transition Aged Youth" |
66 | | - expect(page).to have_field("Both", count: 3) |
67 | | - expect(page).to have_field("Yes", count: 3) |
68 | | - expect(page).to have_field("No", count: 3) |
69 | | - end |
| 112 | + context "with a volunteer filter" do |
| 113 | + before do |
| 114 | + create(:volunteer, casa_org: user.casa_org, display_name: volunteer_name) |
| 115 | + visit reports_path |
| 116 | + select_report_filter_option(VOLUNTEER_SELECT_ID, volunteer_name) |
| 117 | + end |
70 | 118 |
|
71 | | - it "downloads report", :js do |
72 | | - user = create(:casa_admin) |
| 119 | + include_examples "case contacts report with filter", "volunteer" |
| 120 | + end |
73 | 121 |
|
74 | | - sign_in user |
75 | | - visit reports_path |
| 122 | + context "with a contact type filter" do |
| 123 | + before do |
| 124 | + create(:contact_type, casa_org: user.casa_org, name: contact_type_name) |
| 125 | + visit reports_path |
| 126 | + select_report_filter_option(CONTACT_TYPE_SELECT_ID, contact_type_name) |
| 127 | + end |
76 | 128 |
|
77 | | - click_on "Download Report" |
78 | | - expect(page).to have_text("Downloading Report") |
| 129 | + include_examples "case contacts report with filter", "contact type" |
| 130 | + end |
| 131 | + |
| 132 | + context "with a contact type group filter" do |
| 133 | + before do |
| 134 | + create(:contact_type_group, casa_org: user.casa_org, name: contact_type_group_name) |
| 135 | + visit reports_path |
| 136 | + select_report_filter_option(CONTACT_TYPE_GROUP_SELECT_ID, contact_type_group_name) |
| 137 | + end |
| 138 | + |
| 139 | + include_examples "case contacts report with filter", "contact type group" |
| 140 | + end |
| 141 | + |
| 142 | + context "with a driving reimbursement filter" do |
| 143 | + before do |
| 144 | + visit reports_path |
| 145 | + choose_report_radio_option("want_driving_reimbursement", "true") |
| 146 | + end |
| 147 | + |
| 148 | + include_examples "case contacts report with filter", "driving reimbursement" |
| 149 | + end |
| 150 | + |
| 151 | + context "with a contact made filters" do |
| 152 | + before do |
| 153 | + visit reports_path |
| 154 | + choose_report_radio_option("contact_made", "true") |
| 155 | + end |
| 156 | + |
| 157 | + include_examples "case contacts report with filter", "contact made" |
| 158 | + end |
| 159 | + |
| 160 | + context "with a transition aged youth filter" do |
| 161 | + before do |
| 162 | + visit reports_path |
| 163 | + choose_report_radio_option("has_transitioned", "true") |
| 164 | + end |
| 165 | + |
| 166 | + include_examples "case contacts report with filter", "transition aged youth" |
| 167 | + end |
| 168 | + |
| 169 | + context "with a date range filter" do |
| 170 | + before do |
| 171 | + visit reports_path |
| 172 | + set_report_date_range(start_date: filter_start_date, end_date: filter_end_date) |
| 173 | + end |
| 174 | + |
| 175 | + include_examples "case contacts report with filter", "date range" |
| 176 | + end |
| 177 | + |
| 178 | + context "with multiple filters" do |
| 179 | + before do |
| 180 | + create(:volunteer, casa_org: user.casa_org, display_name: volunteer_name) |
| 181 | + create(:contact_type, casa_org: user.casa_org, name: contact_type_name) |
| 182 | + visit reports_path |
| 183 | + set_report_date_range(start_date: filter_start_date, end_date: filter_end_date) |
| 184 | + select_report_filter_option(VOLUNTEER_SELECT_ID, volunteer_name) |
| 185 | + select_report_filter_option(CONTACT_TYPE_SELECT_ID, contact_type_name) |
| 186 | + choose_report_radio_option("want_driving_reimbursement", "false") |
| 187 | + end |
| 188 | + |
| 189 | + include_examples "case contacts report with filter", "multiple filters" |
| 190 | + end |
| 191 | + |
| 192 | + context "with no volunteers in the org" do |
| 193 | + include_examples "empty select downloads report", VOLUNTEER_SELECT_ID, "volunteers" |
| 194 | + end |
| 195 | + |
| 196 | + context "with no contact type groups in the org" do |
| 197 | + include_examples "empty select downloads report", CONTACT_TYPE_GROUP_SELECT_ID, "contact type groups" |
| 198 | + end |
| 199 | + |
| 200 | + context "with no contact types in the org" do |
| 201 | + include_examples "empty select downloads report", CONTACT_TYPE_SELECT_ID, "contact types" |
| 202 | + end |
79 | 203 | end |
| 204 | + # rubocop:enable RSpec/MultipleMemoizedHelpers |
| 205 | + end |
| 206 | + |
| 207 | + private |
| 208 | + |
| 209 | + def select_report_filter_option(select_id, option) |
| 210 | + expect(page).to have_select(select_id, with_options: [option]) |
| 211 | + find("##{select_id}").select(option) |
| 212 | + end |
| 213 | + |
| 214 | + def set_report_date_range(start_date:, end_date:) |
| 215 | + fill_in "report_start_date", with: start_date |
| 216 | + fill_in "report_end_date", with: end_date |
| 217 | + end |
| 218 | + |
| 219 | + def choose_report_radio_option(field_name, value) |
| 220 | + find("input[name=\"report[#{field_name}]\"][value=\"#{value}\"]", visible: :all).click |
80 | 221 | end |
81 | 222 | end |
0 commit comments