Skip to content

Commit 1f0caf3

Browse files
Test: add spec for report generation and attachment in case court reports
Signed-off-by: Audrea Cook <[email protected]>
1 parent f298035 commit 1f0caf3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

spec/system/case_court_reports/index_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,44 @@
146146
open_court_report_modal # Reopen using the helper
147147
expect(page).not_to have_selector(".select-required-error", visible: :visible) # Error should be gone
148148
end
149+
150+
# NOTE: select by option VALUE (stable), stub `window.open` to capture the download URL,
151+
# wait for the ActiveStorage attachment, and assert button state + opened URL.
152+
it "generates and attaches a report on success", :aggregate_failures, :js do # rubocop:disable RSpec/ExampleLength
153+
transition_case = casa_cases.detect(&:in_transition_age?)
154+
155+
# Precondition: no report attached
156+
expect(transition_case.court_reports.attached?).to be false
157+
158+
# Stub window.open so we can capture the download URL in the browser
159+
page.execute_script(<<~JS)
160+
window.__last_opened_url = null;
161+
window.open = function(url) { window.__last_opened_url = url; };
162+
JS
163+
164+
# Ensure the option exists, then select it by VALUE (case number)
165+
expect(page).to have_selector("#case-selection option[value='#{transition_case.case_number}']", visible: :all)
166+
find("#case-selection").find("option[value='#{transition_case.case_number}']").select_option
167+
168+
# Trigger generation
169+
click_button "Generate Report"
170+
171+
# Button should be disabled while processing
172+
expect(page).to have_selector("#btnGenerateReport[disabled]")
173+
174+
# Wait for the controller to attach the file (allow longer timeout on CI)
175+
wait_for_report_attachment(transition_case, timeout: 10)
176+
transition_case.reload
177+
178+
# Postcondition: report attached and button re-enabled
179+
expect(transition_case.court_reports.attached?).to be true
180+
expect(page).not_to have_selector("#btnGenerateReport[disabled]", visible: :all)
181+
182+
# Verify the browser attempted to open the generated .docx link
183+
opened_url = page.evaluate_script("window.__last_opened_url")
184+
expect(opened_url).to be_present
185+
expect(opened_url).to match(/#{Regexp.escape(transition_case.case_number)}.*\.docx$/i)
186+
end
149187
end
150188

151189
context "when logged in as a supervisor" do

0 commit comments

Comments
 (0)