-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WD-11593 - refactor: remove nested components from ActionLogs (#1783)
- Loading branch information
1 parent
62ec9c1
commit 524d693
Showing
7 changed files
with
130 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/pages/EntityDetails/Model/Logs/ActionLogs/ActionPayloadModal/ActionPayloadModal.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { screen, within } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { vi } from "vitest"; | ||
|
||
import * as componentUtils from "components/utils"; | ||
import { renderComponent } from "testing/utils"; | ||
|
||
import ActionPayloadModal from "./ActionPayloadModal"; | ||
import { Label } from "./types"; | ||
|
||
vi.mock("components/utils", async () => { | ||
const utils = await vi.importActual("components/utils"); | ||
return { | ||
...utils, | ||
copyToClipboard: vi.fn(), | ||
}; | ||
}); | ||
|
||
describe("ActionPayloadModal", () => { | ||
const mockPayload = { key1: "value1", test: 123 }; | ||
|
||
it("should return null if payload is null", () => { | ||
const { | ||
result: { container }, | ||
} = renderComponent( | ||
<ActionPayloadModal payload={null} onClose={vi.fn()} />, | ||
); | ||
expect(container.tagName).toBe("DIV"); | ||
expect(container.children.length).toBe(1); | ||
expect(container.firstChild).toBeEmptyDOMElement(); | ||
}); | ||
|
||
it("should display action payload modal if payload is not null", async () => { | ||
renderComponent( | ||
<ActionPayloadModal payload={mockPayload} onClose={vi.fn()} />, | ||
); | ||
const actionPayloadModal = screen.getByTestId("action-payload-modal"); | ||
expect(actionPayloadModal).toBeInTheDocument(); | ||
expect( | ||
within(actionPayloadModal).getByRole("dialog", { name: Label.TITLE }), | ||
).toBeInTheDocument(); | ||
const codeSnippetLines = document.querySelectorAll(".p-code-snippet__line"); | ||
expect(codeSnippetLines).toHaveLength(4); | ||
expect(codeSnippetLines[0]).toHaveTextContent("{"); | ||
expect(codeSnippetLines[1]).toHaveTextContent('"key1": "value1",'); | ||
expect(codeSnippetLines[2]).toHaveTextContent('"test": 123'); | ||
expect(codeSnippetLines[3]).toHaveTextContent("}"); | ||
}); | ||
|
||
it("should copy the payload to clipboard", async () => { | ||
renderComponent( | ||
<ActionPayloadModal payload={mockPayload} onClose={vi.fn()} />, | ||
); | ||
await userEvent.click(screen.getByRole("button", { name: Label.COPY })); | ||
expect(componentUtils.copyToClipboard).toHaveBeenCalledWith(`{ | ||
"key1": "value1", | ||
"test": 123 | ||
}`); | ||
}); | ||
|
||
it("should close the modal", async () => { | ||
const onClose = vi.fn(); | ||
renderComponent( | ||
<ActionPayloadModal payload={mockPayload} onClose={onClose} />, | ||
); | ||
await userEvent.click( | ||
screen.getByRole("button", { name: "Close active modal" }), | ||
); | ||
expect(onClose).toHaveBeenCalledOnce(); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
src/pages/EntityDetails/Model/Logs/ActionLogs/ActionPayloadModal/ActionPayloadModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { ActionResult } from "@canonical/jujulib/dist/api/facades/action/ActionV7"; | ||
import { | ||
Button, | ||
CodeSnippet, | ||
CodeSnippetBlockAppearance, | ||
Modal, | ||
} from "@canonical/react-components"; | ||
|
||
import { copyToClipboard } from "components/utils"; | ||
|
||
import { Label } from "./types"; | ||
|
||
type Props = { | ||
payload: ActionResult["output"] | null; | ||
onClose: () => void; | ||
}; | ||
|
||
const ActionPayloadModal = ({ | ||
payload, | ||
onClose, | ||
}: Props): JSX.Element | null => { | ||
if (!payload) { | ||
return null; | ||
} | ||
const json = JSON.stringify(payload, null, 2); | ||
return ( | ||
<Modal | ||
close={onClose} | ||
title={Label.TITLE} | ||
buttonRow={ | ||
<Button appearance="neutral" onClick={() => copyToClipboard(json)}> | ||
{Label.COPY} | ||
</Button> | ||
} | ||
data-testid="action-payload-modal" | ||
> | ||
<CodeSnippet | ||
blocks={[ | ||
{ | ||
appearance: CodeSnippetBlockAppearance.NUMBERED, | ||
wrapLines: true, | ||
code: json, | ||
}, | ||
]} | ||
/> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default ActionPayloadModal; |
1 change: 1 addition & 0 deletions
1
src/pages/EntityDetails/Model/Logs/ActionLogs/ActionPayloadModal/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./ActionPayloadModal"; |
4 changes: 4 additions & 0 deletions
4
src/pages/EntityDetails/Model/Logs/ActionLogs/ActionPayloadModal/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum Label { | ||
TITLE = "Action result payload", | ||
COPY = "Copy to clipboard", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters