-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:Aiven-Open/klaw into json-support
- Loading branch information
Showing
32 changed files
with
953 additions
and
580 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,64 @@ | ||
import { render, screen, cleanup, waitFor } from "@testing-library/react"; | ||
import { userEvent } from "@testing-library/user-event"; | ||
import ClipBoard from "src/app/components/Clipboard"; | ||
|
||
describe("ClipBoard", () => { | ||
const mockCopyToClipboard = jest.fn(); | ||
Object.defineProperty(global.navigator, "clipboard", { | ||
value: { writeText: mockCopyToClipboard }, | ||
writable: true, | ||
}); | ||
|
||
const props = { | ||
text: "Test text", | ||
accessibleCopyDescription: "Copy to clipboard", | ||
accessibleCopiedDescription: "Copied to clipboard", | ||
description: "Test description", | ||
}; | ||
|
||
beforeEach(() => { | ||
render(<ClipBoard {...props} />); | ||
}); | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("renders correctly", () => { | ||
const button = screen.getByRole("button", { | ||
name: props.accessibleCopyDescription, | ||
}); | ||
expect(button).toBeVisible(); | ||
}); | ||
|
||
it("copies text to clipboard when button is clicked", async () => { | ||
const button = screen.getByRole("button", { | ||
name: props.accessibleCopyDescription, | ||
}); | ||
await userEvent.click(button); | ||
await waitFor(() => | ||
expect(mockCopyToClipboard).toHaveBeenCalledWith(props.text) | ||
); | ||
}); | ||
|
||
it("renders feedback tooltip after button is clicked", async () => { | ||
const button = screen.getByRole("button", { | ||
name: props.accessibleCopyDescription, | ||
}); | ||
await userEvent.click(button); | ||
await waitFor(() => expect(screen.getByText("Copied")).toBeVisible()); | ||
}); | ||
|
||
it("renders accessible feedback after button is clicked", async () => { | ||
const button = screen.getByRole("button", { | ||
name: props.accessibleCopyDescription, | ||
}); | ||
await userEvent.click(button); | ||
await waitFor(() => | ||
expect( | ||
screen.getByLabelText(props.accessibleCopiedDescription) | ||
).toBeInTheDocument() | ||
); | ||
}); | ||
}); |
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,85 @@ | ||
import { Tooltip, PositionerPlacement, Button } from "@aivenio/aquarium"; | ||
import duplicate from "@aivenio/aquarium/icons/duplicate"; | ||
import { useRef, useState, useEffect, FormEvent } from "react"; | ||
|
||
const copyToClipboard = async (text: string): Promise<void> => { | ||
try { | ||
await navigator.clipboard.writeText(text); | ||
} catch (error) { | ||
console.error("Failed to copy to clipboard:", error); | ||
} | ||
}; | ||
|
||
const ClipBoard = ({ | ||
text, | ||
accessibleCopyDescription, | ||
accessibleCopiedDescription, | ||
description, | ||
}: { | ||
text: string; | ||
accessibleCopyDescription: string; | ||
accessibleCopiedDescription: string; | ||
description?: string; | ||
}) => { | ||
const feedbackTimerRef = useRef<number>(); | ||
|
||
const [showCopyFeedback, setShowCopyFeedback] = useState(false); | ||
|
||
const clearTimeouts = () => { | ||
window.clearTimeout(feedbackTimerRef.current); | ||
}; | ||
|
||
useEffect(() => () => clearTimeouts(), []); | ||
|
||
function handleCopy(event: FormEvent) { | ||
event.preventDefault(); | ||
copyToClipboard(text); | ||
setShowCopyFeedback(true); | ||
feedbackTimerRef.current = window.setTimeout( | ||
() => setShowCopyFeedback(false), | ||
1000 | ||
); | ||
} | ||
|
||
if (showCopyFeedback) { | ||
return ( | ||
<Tooltip | ||
key="copied" | ||
content="Copied" | ||
isOpen | ||
placement={PositionerPlacement.top} | ||
> | ||
<Button.SecondaryGhost | ||
key="copy-button" | ||
aria-label={accessibleCopiedDescription} | ||
onClick={handleCopy} | ||
icon={duplicate} | ||
> | ||
{description} | ||
</Button.SecondaryGhost> | ||
<div aria-live="polite" className={"visually-hidden"}> | ||
{accessibleCopiedDescription} | ||
</div> | ||
</Tooltip> | ||
); | ||
} | ||
|
||
return ( | ||
<Tooltip | ||
key="copy-to-clipboard" | ||
content="Copy" | ||
placement={PositionerPlacement.top} | ||
> | ||
<Button.SecondaryGhost | ||
key="copy-button" | ||
aria-label={accessibleCopyDescription} | ||
onClick={handleCopy} | ||
icon={duplicate} | ||
> | ||
{description} | ||
</Button.SecondaryGhost> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
export default 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
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
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
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
Oops, something went wrong.