|
| 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 | +}); |
0 commit comments