import * as React from "react"; import {render, act} from "@testing-library/react"; import {Snippet} from "../src"; describe("Snippet", () => { it("should render correctly", () => { const wrapper = render(); expect(() => wrapper.unmount()).not.toThrow(); }); it("ref should be forwarded", () => { const ref = React.createRef(); render(); expect(ref.current).not.toBeNull(); }); it("should include the code", () => { const wrapper = render( npm install @nextui-org/react, ); expect(wrapper.getByTestId("code-test")).toHaveTextContent("npm install @nextui-org/react"); }); it("should render multiple
 tags when children is an array of string", () => {
    const wrapper = render(
      
        {["npm install @nextui-org/react", "npm install @nextui-org/react"]}
      ,
    );

    expect(wrapper.getByTestId("code-test").querySelectorAll("pre")).toHaveLength(2);
  });

  it('should not render "copy" button when hideCopyButton is true', () => {
    const wrapper = render();

    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', async () => {
    jest.spyOn(navigator.clipboard, "writeText");

    let code = "npm install @nextui-org/react";

    const wrapper = render({code});

    await act(async () => {
      await wrapper.getByRole("button").click();
      expect(navigator.clipboard.writeText).toHaveBeenCalledWith(code);
    });
  });
});