mirror of
https://github.com/nextui-org/nextui.git
synced 2025-12-08 19:26:11 +00:00
feat(snippet): tests added
This commit is contained in:
parent
3789144e63
commit
11fe7ee05c
@ -17,7 +17,7 @@ describe("Code", () => {
|
||||
expect(ref.current).not.toBeNull();
|
||||
});
|
||||
|
||||
it("should support include the code", () => {
|
||||
it("should include the code", () => {
|
||||
const wrapper = render(<Code data-testid="code-test">npm install @nextui-org/react</Code>);
|
||||
|
||||
expect(wrapper.getByTestId("code-test")).toHaveTextContent("npm install @nextui-org/react");
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import * as React from "react";
|
||||
import {render} from "@testing-library/react";
|
||||
import {render, act} from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
|
||||
import {Snippet} from "../src";
|
||||
|
||||
@ -16,4 +17,64 @@ describe("Snippet", () => {
|
||||
render(<Snippet ref={ref} />);
|
||||
expect(ref.current).not.toBeNull();
|
||||
});
|
||||
|
||||
it("should include the code", () => {
|
||||
const wrapper = render(
|
||||
<Snippet data-testid="code-test">npm install @nextui-org/react</Snippet>,
|
||||
);
|
||||
|
||||
expect(wrapper.getByTestId("code-test")).toHaveTextContent("npm install @nextui-org/react");
|
||||
});
|
||||
|
||||
it("should render multiple <pre> tags when children is an array of string", () => {
|
||||
const wrapper = render(
|
||||
<Snippet data-testid="code-test">
|
||||
{["npm install @nextui-org/react", "npm install @nextui-org/react"]}
|
||||
</Snippet>,
|
||||
);
|
||||
|
||||
expect(wrapper.getByTestId("code-test").querySelectorAll("pre")).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should not render "copy" button when hideCopyButton is true', () => {
|
||||
const wrapper = render(<Snippet hideCopyButton />);
|
||||
|
||||
expect(() => wrapper.getByRole("button")).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Snippet - Clipboard", () => {
|
||||
beforeAll(() => {
|
||||
jest.useFakeTimers();
|
||||
// navigator.clipboard.writeText mock
|
||||
Object.assign(navigator, {
|
||||
clipboard: {
|
||||
writeText: (data: string) =>
|
||||
new Promise((res, rej) => {
|
||||
try {
|
||||
res(data);
|
||||
} catch (error) {
|
||||
rej(error);
|
||||
}
|
||||
}),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('should copy text to clipboard when "copy" button is clicked', () => {
|
||||
jest.spyOn(navigator.clipboard, "writeText");
|
||||
|
||||
let code = "npm install @nextui-org/react";
|
||||
|
||||
act(() => {
|
||||
const wrapper = render(<Snippet data-testid="code-test">{code}</Snippet>);
|
||||
|
||||
userEvent.click(wrapper.getByRole("button"));
|
||||
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(code);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user