Skip to content

Commit 0eb8383

Browse files
authored
Merge pull request #6581 from AudTheCodeWitch/acook/6313/update-case-court-reports-index-spec
6313 - Case Court Reports Index Spec Overhaul
2 parents a62148f + 1f0caf3 commit 0eb8383

File tree

6 files changed

+331
-144
lines changed

6 files changed

+331
-144
lines changed
735 KB
Binary file not shown.

spec/rails_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
config.include TwilioHelper, type: :request
6363
config.include TwilioHelper, type: :system
6464
config.include Support::RequestHelpers, type: :request
65+
config.include CaseCourtReportHelpers, type: :system
6566

6667
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
6768
config.fixture_paths = [

spec/requests/case_court_reports_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,44 @@
166166
end
167167
end
168168

169+
context "with date filtering" do
170+
let(:casa_case) { volunteer.casa_cases.first }
171+
let!(:contact_in_range) do
172+
create(:case_contact, casa_case: casa_case, occurred_at: Date.new(2025, 10, 10))
173+
end
174+
let!(:contact_out_of_range) do
175+
create(:case_contact, casa_case: casa_case, occurred_at: Date.new(2024, 10, 30))
176+
end
177+
let(:params) {
178+
{
179+
case_court_report: {
180+
case_number: casa_case.case_number.to_s,
181+
start_date: "2025-10-01",
182+
end_date: "2025-10-23"
183+
}
184+
}
185+
}
186+
187+
it "includes contacts within the date range in the generated report" do
188+
post generate_case_court_reports_path, params: params, headers: {ACCEPT: "application/json"}
189+
get response.parsed_body["link"]
190+
docx_response = Docx::Document.open(StringIO.new(response.body))
191+
# The contact dates are in table cells, so we need to extract them specifically.
192+
table_texts = docx_response.tables.flat_map { |table| table.rows.flat_map { |row| row.cells.map(&:text) } }
193+
194+
expect(table_texts.join(" ")).to include(contact_in_range.occurred_at.strftime("%-m/%-d"))
195+
end
196+
197+
it "does not include contacts outside the date range in the generated report" do
198+
post generate_case_court_reports_path, params: params, headers: {ACCEPT: "application/json"}
199+
get response.parsed_body["link"]
200+
docx_response = Docx::Document.open(StringIO.new(response.body))
201+
table_texts = docx_response.tables.flat_map { |table| table.rows.flat_map { |row| row.cells.map(&:text) } }
202+
203+
expect(table_texts.join(" ")).not_to include(contact_out_of_range.occurred_at.strftime("%-m/%-d"))
204+
end
205+
end
206+
169207
context "when user timezone" do
170208
let(:server_time) { Time.zone.parse("2020-12-31 23:00:00") }
171209
let(:user_different_timezone) do
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
# Helper methods for case court reports system specs
4+
module CaseCourtReportHelpers
5+
# Finds the 'Generate Report' button, clicks it, and confirms the modal is visible.
6+
# Assumes the user is already on the case_court_reports_path.
7+
def open_court_report_modal
8+
find('[data-bs-target="#generate-docx-report-modal"]').click
9+
expect(page).to have_selector("#generate-docx-report-modal", visible: :visible)
10+
end
11+
12+
# Opens the Select2 dropdown within the (already open) report modal.
13+
# Confirms the dropdown options are visible.
14+
def open_case_select2_dropdown
15+
# Wait for the Select2 container to be visible
16+
expect(page).to have_css("#case_select_body .selection", visible: :visible)
17+
18+
# Click the container to open the dropdown
19+
find("#case_select_body .selection").click
20+
21+
# Wait for the dropdown to appear
22+
expect(page).to have_css(".select2-dropdown", visible: :visible)
23+
end
24+
25+
# Polls the database until the casa_case has an ActiveStorage court_report attached.
26+
# This is used after clicking 'Generate Report' to wait for the background job to complete.
27+
def wait_for_report_attachment(casa_case, timeout: 5)
28+
casa_case.reload # Ensure we have the latest record
29+
30+
Timeout.timeout(timeout) do
31+
until casa_case.court_reports.attached?
32+
sleep 0.2
33+
casa_case.reload
34+
end
35+
end
36+
end
37+
end

spec/system/casa_cases/show_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,34 @@
136136
expect(page).to have_content("Add to Calendar")
137137
end
138138

139+
context "court report download link visibility" do
140+
it "does not show download link to admin when report status is not submitted" do
141+
fixture = Rails.root.join("spec/fixtures/files/sample_report.docx")
142+
casa_case.court_reports.attach(
143+
io: File.open(fixture),
144+
filename: "sample_report.docx",
145+
content_type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
146+
)
147+
casa_case.update!(court_report_status: :in_review)
148+
149+
visit casa_case_path(casa_case.id)
150+
expect(page).not_to have_link("Click to download")
151+
end
152+
153+
it "shows download link to admin when report status is submitted" do
154+
fixture = Rails.root.join("spec/fixtures/files/sample_report.docx")
155+
casa_case.court_reports.attach(
156+
io: File.open(fixture),
157+
filename: "sample_report.docx",
158+
content_type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
159+
)
160+
casa_case.update!(court_report_status: :submitted)
161+
162+
visit casa_case_path(casa_case.id)
163+
expect(page).to have_link("Click to download")
164+
end
165+
end
166+
139167
context "when there is no future court date or court report due date" do
140168
before do
141169
casa_case = create(:casa_case, casa_org: organization)
@@ -189,6 +217,34 @@
189217
expect(page).to have_content(casa_case.case_court_orders[0].implementation_status_symbol)
190218
end
191219

220+
context "court report download link visibility" do
221+
it "does not show download link to supervisor when report status is not submitted" do
222+
fixture = Rails.root.join("spec/fixtures/files/sample_report.docx")
223+
casa_case.court_reports.attach(
224+
io: File.open(fixture),
225+
filename: "sample_report.docx",
226+
content_type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
227+
)
228+
casa_case.update!(court_report_status: :in_review)
229+
230+
visit casa_case_path(casa_case.id)
231+
expect(page).not_to have_link("Click to download")
232+
end
233+
234+
it "shows download link to supervisor when report status is submitted" do
235+
fixture = Rails.root.join("spec/fixtures/files/sample_report.docx")
236+
casa_case.court_reports.attach(
237+
io: File.open(fixture),
238+
filename: "sample_report.docx",
239+
content_type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
240+
)
241+
casa_case.update!(court_report_status: :submitted)
242+
243+
visit casa_case_path(casa_case.id)
244+
expect(page).to have_link("Click to download")
245+
end
246+
end
247+
192248
context "when old case contacts are hidden" do
193249
it "displays all case contacts to supervisor", :js do
194250
casa_case = create(:casa_case, casa_org: organization)

0 commit comments

Comments
 (0)