|
| 1 | +import React from "react"; |
| 2 | +import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; |
| 3 | + |
| 4 | +import "@testing-library/jest-dom"; |
| 5 | + |
| 6 | +import { ActivityExecutionErrorReportModal } from "../ActivityExecutionErrorReportModal"; |
| 7 | +import { SilkActivityControl, SilkActivityControlProps } from "../SilkActivityControl"; |
| 8 | + |
| 9 | +type FailureReportAction = NonNullable<SilkActivityControlProps["failureReportAction"]>; |
| 10 | + |
| 11 | +const renderFailedActivity = ( |
| 12 | + failureReportAction: Partial<FailureReportAction>, |
| 13 | + additionalContent?: React.ReactNode, |
| 14 | +) => { |
| 15 | + const props: SilkActivityControlProps = { |
| 16 | + label: "Execution", |
| 17 | + initialStatus: { |
| 18 | + activity: "execute", |
| 19 | + activityLabel: "Execution", |
| 20 | + isRunning: false, |
| 21 | + failed: true, |
| 22 | + statusName: "Finished", |
| 23 | + progress: 100, |
| 24 | + lastUpdateTime: 0, |
| 25 | + message: "Failed", |
| 26 | + cancelled: false, |
| 27 | + concreteStatus: "Failed", |
| 28 | + }, |
| 29 | + registerForUpdates: jest.fn(), |
| 30 | + unregisterFromUpdates: jest.fn(), |
| 31 | + showStartAction: false, |
| 32 | + showStopAction: false, |
| 33 | + showReloadAction: false, |
| 34 | + executeActivityAction: jest.fn(), |
| 35 | + translate: (key) => key, |
| 36 | + failureReportAction: { |
| 37 | + renderReport: () => <p>Error details</p>, |
| 38 | + renderMarkdown: true, |
| 39 | + fetchErrorReport: async () => "Error details", |
| 40 | + downloadButtonValue: "Download", |
| 41 | + closeButtonValue: "Close", |
| 42 | + ...failureReportAction, |
| 43 | + }, |
| 44 | + }; |
| 45 | + const utils = render( |
| 46 | + <> |
| 47 | + {additionalContent} |
| 48 | + <SilkActivityControl {...props} /> |
| 49 | + </>, |
| 50 | + ); |
| 51 | + const openErrorReport = () => |
| 52 | + fireEvent.click(utils.container.querySelector("[data-test-id='activity-show-error-report']") as HTMLElement); |
| 53 | + return { ...utils, openErrorReport }; |
| 54 | +}; |
| 55 | + |
| 56 | +describe("SilkActivityControl error report", () => { |
| 57 | + it("uses the title as accessible name of the error report dialog", async () => { |
| 58 | + const { openErrorReport } = renderFailedActivity({ title: "Error report" }); |
| 59 | + openErrorReport(); |
| 60 | + expect(await screen.findByRole("dialog", { name: "Error report" })).toHaveAttribute("aria-modal", "true"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("forwards `aria-label` to a titleless error report dialog", async () => { |
| 64 | + const { openErrorReport } = renderFailedActivity({ |
| 65 | + title: "", |
| 66 | + "aria-label": "Activity execution error report", |
| 67 | + }); |
| 68 | + openErrorReport(); |
| 69 | + const dialog = await screen.findByRole("dialog", { name: "Activity execution error report" }); |
| 70 | + expect(dialog).toHaveAttribute("aria-modal", "true"); |
| 71 | + |
| 72 | + fireEvent.click(screen.getByRole("button", { name: "Close" })); |
| 73 | + await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); |
| 74 | + }); |
| 75 | + |
| 76 | + it("uses the translated `showErrorReport` text as fallback name for a titleless error report dialog", async () => { |
| 77 | + const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => undefined); |
| 78 | + try { |
| 79 | + const { openErrorReport } = renderFailedActivity({ title: "", "aria-label": "" }); |
| 80 | + openErrorReport(); |
| 81 | + expect(await screen.findByRole("dialog", { name: "showErrorReport" })).toHaveAttribute( |
| 82 | + "aria-modal", |
| 83 | + "true", |
| 84 | + ); |
| 85 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 86 | + } finally { |
| 87 | + warnSpy.mockRestore(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it("forwards the full screen toggler texts to the error report dialog", async () => { |
| 92 | + const { openErrorReport } = renderFailedActivity({ |
| 93 | + title: "Error report", |
| 94 | + fullScreenTogglerTexts: { maximize: "Vollbild anzeigen", minimize: "Vollbild verlassen" }, |
| 95 | + }); |
| 96 | + openErrorReport(); |
| 97 | + const dialog = await screen.findByRole("dialog", { name: "Error report" }); |
| 98 | + const toggler = within(dialog).getByRole("button", { name: "Vollbild anzeigen" }); |
| 99 | + |
| 100 | + fireEvent.click(toggler); |
| 101 | + expect(toggler).toHaveAccessibleName("Vollbild verlassen"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("forwards `aria-labelledby` to a titleless error report dialog", async () => { |
| 105 | + const { openErrorReport } = renderFailedActivity( |
| 106 | + { "aria-labelledby": "external-report-label" }, |
| 107 | + <span id="external-report-label">Referenced error report label</span>, |
| 108 | + ); |
| 109 | + openErrorReport(); |
| 110 | + const dialog = await screen.findByRole("dialog", { name: "Referenced error report label" }); |
| 111 | + expect(dialog).toHaveAttribute("aria-labelledby", "external-report-label"); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe("ActivityExecutionErrorReportModal", () => { |
| 116 | + it.each([undefined, ""])( |
| 117 | + "uses an english fallback name if neither title nor aria attributes are given (aria-label: %p)", |
| 118 | + (ariaLabel) => { |
| 119 | + render( |
| 120 | + <ActivityExecutionErrorReportModal |
| 121 | + aria-label={ariaLabel} |
| 122 | + onDiscard={jest.fn()} |
| 123 | + report={<p>Error details</p>} |
| 124 | + downloadButtonValue="Download" |
| 125 | + closeButtonValue="Close" |
| 126 | + fetchErrorReport={async () => "Error details"} |
| 127 | + />, |
| 128 | + ); |
| 129 | + expect(screen.getByRole("dialog", { name: "Activity execution error report" })).toBeInTheDocument(); |
| 130 | + }, |
| 131 | + ); |
| 132 | +}); |
0 commit comments