|
| 1 | +import React from "react"; |
| 2 | +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 3 | + |
| 4 | +import "@testing-library/jest-dom"; |
| 5 | + |
| 6 | +import { SilkActivityControl, SilkActivityControlProps } from "../SilkActivityControl"; |
| 7 | + |
| 8 | +type FailureReportAction = NonNullable<SilkActivityControlProps["failureReportAction"]>; |
| 9 | + |
| 10 | +const renderFailedActivity = ( |
| 11 | + failureReportAction: Partial<FailureReportAction>, |
| 12 | + additionalContent?: React.ReactNode, |
| 13 | +) => { |
| 14 | + const props: SilkActivityControlProps = { |
| 15 | + label: "Execution", |
| 16 | + initialStatus: { |
| 17 | + activity: "execute", |
| 18 | + activityLabel: "Execution", |
| 19 | + isRunning: false, |
| 20 | + failed: true, |
| 21 | + statusName: "Finished", |
| 22 | + progress: 100, |
| 23 | + lastUpdateTime: 0, |
| 24 | + message: "Failed", |
| 25 | + cancelled: false, |
| 26 | + concreteStatus: "Failed", |
| 27 | + }, |
| 28 | + registerForUpdates: jest.fn(), |
| 29 | + unregisterFromUpdates: jest.fn(), |
| 30 | + showStartAction: false, |
| 31 | + showStopAction: false, |
| 32 | + showReloadAction: false, |
| 33 | + executeActivityAction: jest.fn(), |
| 34 | + translate: (key) => key, |
| 35 | + failureReportAction: { |
| 36 | + renderReport: () => <p>Error details</p>, |
| 37 | + renderMarkdown: true, |
| 38 | + fetchErrorReport: async () => "Error details", |
| 39 | + downloadButtonValue: "Download", |
| 40 | + closeButtonValue: "Close", |
| 41 | + ...failureReportAction, |
| 42 | + }, |
| 43 | + }; |
| 44 | + const utils = render( |
| 45 | + <> |
| 46 | + {additionalContent} |
| 47 | + <SilkActivityControl {...props} /> |
| 48 | + </>, |
| 49 | + ); |
| 50 | + const openErrorReport = () => |
| 51 | + fireEvent.click(utils.container.querySelector("[data-test-id='activity-show-error-report']") as HTMLElement); |
| 52 | + return { ...utils, openErrorReport }; |
| 53 | +}; |
| 54 | + |
| 55 | +describe("SilkActivityControl error report", () => { |
| 56 | + it("uses the title as accessible name of the error report dialog", async () => { |
| 57 | + const { openErrorReport } = renderFailedActivity({ title: "Error report" }); |
| 58 | + openErrorReport(); |
| 59 | + expect(await screen.findByRole("dialog", { name: "Error report" })).toHaveAttribute("aria-modal", "true"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("forwards `aria-label` to a titleless error report dialog", async () => { |
| 63 | + const { openErrorReport } = renderFailedActivity({ |
| 64 | + title: "", |
| 65 | + "aria-label": "Activity execution error report", |
| 66 | + }); |
| 67 | + openErrorReport(); |
| 68 | + const dialog = await screen.findByRole("dialog", { name: "Activity execution error report" }); |
| 69 | + expect(dialog).toHaveAttribute("aria-modal", "true"); |
| 70 | + |
| 71 | + fireEvent.click(screen.getByRole("button", { name: "Close" })); |
| 72 | + await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); |
| 73 | + }); |
| 74 | + |
| 75 | + it("forwards `aria-labelledby` to a titleless error report dialog", async () => { |
| 76 | + const { openErrorReport } = renderFailedActivity( |
| 77 | + { "aria-labelledby": "external-report-label" }, |
| 78 | + <span id="external-report-label">Referenced error report label</span>, |
| 79 | + ); |
| 80 | + openErrorReport(); |
| 81 | + const dialog = await screen.findByRole("dialog", { name: "Referenced error report label" }); |
| 82 | + expect(dialog).toHaveAttribute("aria-labelledby", "external-report-label"); |
| 83 | + }); |
| 84 | +}); |
0 commit comments