Skip to content

Commit b15bf65

Browse files
committed
test(code-snippet): add unit tests
1 parent 936a681 commit b15bf65

11 files changed

+232
-21
lines changed

tests/CodeSnippet.test.svelte

-21
This file was deleted.

tests/CodeSnippet/CodeSnippet.test.ts

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { render, screen } from "@testing-library/svelte";
2+
import CodeSnippetInline from "./CodeSnippetInline.test.svelte";
3+
import CodeSnippetMultiline from "./CodeSnippetMultiline.test.svelte";
4+
import CodeSnippetExpandable from "./CodeSnippetExpandable.test.svelte";
5+
import CodeSnippetExpandedByDefault from "./CodeSnippetExpandedByDefault.svelte";
6+
import CodeSnippetCopyButton from "./CodeSnippetCopyButton.test.svelte";
7+
import CodeSnippetCustomEvents from "./CodeSnippetCustomEvents.test.svelte";
8+
import CodeSnippetWithWrapText from "./CodeSnippetWithWrapText.test.svelte";
9+
import CodeSnippetWithHideShowMore from "./CodeSnippetWithHideShowMore.test.svelte";
10+
import CodeSnippetWithCustomCopyText from "./CodeSnippetWithCustomCopyText.test.svelte";
11+
import { user } from "../setup-tests";
12+
13+
describe("CodeSnippet", () => {
14+
test("should render inline variant", () => {
15+
const { container } = render(CodeSnippetInline);
16+
expect(container.querySelector(".bx--snippet--inline")).toBeInTheDocument();
17+
expect(screen.getByText("npm install -g @carbon/cli")).toBeInTheDocument();
18+
});
19+
20+
test("should render multiline variant", () => {
21+
const { container } = render(CodeSnippetMultiline);
22+
expect(container.querySelector(".bx--snippet--multi")).toBeInTheDocument();
23+
expect(screen.getByText(/node -v/)).toBeInTheDocument();
24+
});
25+
26+
test("should expand and collapse expandable snippet", async () => {
27+
const { container } = render(CodeSnippetExpandable);
28+
29+
expect(
30+
container.querySelector(".bx--snippet--expand"),
31+
).not.toBeInTheDocument();
32+
33+
const showMoreButton = screen.getByText("Show more");
34+
await user.click(showMoreButton);
35+
36+
expect(container.querySelector(".bx--snippet--expand")).toBeInTheDocument();
37+
expect(screen.getByText("Show less")).toBeInTheDocument();
38+
39+
const showLessButton = screen.getByText("Show less");
40+
await user.click(showLessButton);
41+
42+
expect(
43+
container.querySelector(".bx--snippet--expand"),
44+
).not.toBeInTheDocument();
45+
expect(screen.getByText("Show more")).toBeInTheDocument();
46+
});
47+
48+
test("should render expanded by default", async () => {
49+
const { container } = render(CodeSnippetExpandedByDefault);
50+
51+
expect(container.querySelector(".bx--snippet--expand")).toBeInTheDocument();
52+
expect(screen.getByText("Show less")).toBeInTheDocument();
53+
54+
await user.click(screen.getByText("Show less"));
55+
56+
expect(
57+
container.querySelector(".bx--snippet--expand"),
58+
).not.toBeInTheDocument();
59+
expect(screen.getByText("Show more")).toBeInTheDocument();
60+
});
61+
62+
test("should copy text when copy button is clicked", async () => {
63+
const originalClipboard = navigator.clipboard;
64+
const mockClipboard = {
65+
writeText: vi.fn().mockImplementation(() => Promise.resolve()),
66+
};
67+
Object.defineProperty(navigator, "clipboard", {
68+
value: mockClipboard,
69+
writable: true,
70+
});
71+
72+
const { container } = render(CodeSnippetCopyButton);
73+
74+
expect(container.querySelector(".bx--snippet--single")).toBeInTheDocument();
75+
expect(
76+
screen.getByText("npm install --save @carbon/icons"),
77+
).toBeInTheDocument();
78+
79+
const copyButton = screen.getByLabelText("Copy to clipboard");
80+
assert(copyButton);
81+
82+
await user.click(copyButton);
83+
expect(mockClipboard.writeText).toHaveBeenCalledWith(
84+
"npm install --save @carbon/icons",
85+
);
86+
expect(screen.getByText("Copied!")).toBeInTheDocument();
87+
88+
Object.defineProperty(navigator, "clipboard", {
89+
value: originalClipboard,
90+
writable: true,
91+
});
92+
});
93+
94+
test("should dispatch copy and copy error events", async () => {
95+
render(CodeSnippetCustomEvents);
96+
97+
expect(screen.getByText("Copy events: 0")).toBeInTheDocument();
98+
99+
const copyButton = screen.getByLabelText("Copy to clipboard");
100+
await user.click(copyButton);
101+
expect(screen.getByText("Copy events: 1")).toBeInTheDocument();
102+
});
103+
104+
test("should wrap text when wrapText is true", () => {
105+
const { container } = render(CodeSnippetWithWrapText);
106+
expect(
107+
container.querySelector(".bx--snippet--wraptext"),
108+
).toBeInTheDocument();
109+
});
110+
111+
test("should hide show more button when hideShowMore is true", () => {
112+
render(CodeSnippetWithHideShowMore);
113+
expect(screen.queryByText("Show more")).not.toBeInTheDocument();
114+
});
115+
116+
test("should display custom copy text", async () => {
117+
render(CodeSnippetWithCustomCopyText);
118+
119+
const copyButton = screen.getByLabelText("Copy to clipboard");
120+
await user.click(copyButton);
121+
expect(screen.getByText("Custom copied text!")).toBeInTheDocument();
122+
});
123+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
import { CodeSnippet } from "carbon-components-svelte";
3+
</script>
4+
5+
<CodeSnippet type="single" code="npm install --save @carbon/icons" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script lang="ts">
2+
import { CodeSnippet } from "carbon-components-svelte";
3+
4+
let copyCount = 0;
5+
6+
function handleCopy() {
7+
copyCount += 1;
8+
}
9+
</script>
10+
11+
Copy events: {copyCount}
12+
13+
<CodeSnippet
14+
type="single"
15+
code="npm install --save @carbon/icons"
16+
on:copy={handleCopy}
17+
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script lang="ts">
2+
import { CodeSnippet } from "carbon-components-svelte";
3+
</script>
4+
5+
<CodeSnippet
6+
type="multi"
7+
code={`node -v
8+
npm -v
9+
yarn -v
10+
git --version
11+
python --version
12+
java -version
13+
docker --version
14+
kubectl version`}
15+
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script lang="ts">
2+
import { CodeSnippet } from "carbon-components-svelte";
3+
</script>
4+
5+
<CodeSnippet
6+
type="multi"
7+
expanded
8+
code={`node -v
9+
npm -v
10+
yarn -v
11+
git --version
12+
python --version
13+
java -version
14+
docker --version
15+
kubectl version`}
16+
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
import { CodeSnippet } from "carbon-components-svelte";
3+
</script>
4+
5+
<CodeSnippet type="inline" code="npm install -g @carbon/cli" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
import { CodeSnippet } from "carbon-components-svelte";
3+
</script>
4+
5+
<CodeSnippet
6+
type="multi"
7+
code={`node -v
8+
npm -v
9+
yarn -v`}
10+
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts">
2+
import { CodeSnippet } from "carbon-components-svelte";
3+
</script>
4+
5+
<CodeSnippet
6+
type="single"
7+
code="npm install --save @carbon/icons"
8+
feedback="Custom copied text!"
9+
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script lang="ts">
2+
import { CodeSnippet } from "carbon-components-svelte";
3+
</script>
4+
5+
<CodeSnippet
6+
type="multi"
7+
showMoreLess={false}
8+
code={`node -v
9+
npm -v
10+
yarn -v
11+
git --version
12+
python --version
13+
java -version
14+
docker --version
15+
kubectl version`}
16+
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script lang="ts">
2+
import { CodeSnippet } from "carbon-components-svelte";
3+
</script>
4+
5+
<CodeSnippet
6+
type="multi"
7+
wrapText={true}
8+
code={`node -v
9+
npm -v
10+
yarn -v
11+
git --version
12+
python --version
13+
java -version
14+
docker --version
15+
kubectl version`}
16+
/>

0 commit comments

Comments
 (0)