-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcode-snippet.test.tsx
50 lines (40 loc) · 1.45 KB
/
code-snippet.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React from "react"
import "@testing-library/jest-dom"
import { userEvent } from "@testing-library/user-event"
import { screen, render, fireEvent } from "@testing-library/react"
import { a11yTest } from "@wpmudev/sui-dev-utils"
import { CodeSnippet, CodeSnippetProps } from "../src"
describe("@wpmudev/sui-code-snippet", () => {
const code = "__CODE_SNIPPET__"
const Component = (props: CodeSnippetProps) => {
return <CodeSnippet {...props}>{code}</CodeSnippet>
}
// Test for rendering the component.
it("renders correctly", () => {
render(<Component />)
expect(screen.getByTestId("code-snippet")).toBeInTheDocument()
})
// Test for rendering the code correctly.
it("code renders correctly", () => {
render(<Component />)
expect(screen.getByText(code)).toBeInTheDocument()
})
// Test for copying the code correctly.
it("copy button works correctly", async () => {
// Setup userEvent.
userEvent.setup()
render(<Component />)
const copyBtn = await screen.findByTestId("code-snippet-copy-btn")
// Ensure the copy button is present.
expect(copyBtn).toBeInTheDocument()
// Simulate a click event on the copy button.
fireEvent.click(copyBtn)
// Read the clipboard contents and assert that it matches the code.
const clipboardText = await navigator.clipboard.readText()
expect(clipboardText).toBe(code)
})
// eslint-disable-next-line jest/expect-expect
it("passes a11y test", async () => {
await a11yTest(<Component />)
})
})