|
146 | 146 | open_court_report_modal # Reopen using the helper |
147 | 147 | expect(page).not_to have_selector(".select-required-error", visible: :visible) # Error should be gone |
148 | 148 | 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 |
149 | 187 | end |
150 | 188 |
|
151 | 189 | context "when logged in as a supervisor" do |
|
0 commit comments