|
| 1 | +import { render, screen } from "@testing-library/svelte"; |
| 2 | +import { tick } from "svelte"; |
| 3 | +import PortalMultipleTest from "./Portal.multiple.test.svelte"; |
| 4 | +import PortalTest from "./Portal.test.svelte"; |
| 5 | + |
| 6 | +describe("Portal", () => { |
| 7 | + afterEach(() => { |
| 8 | + const existingContainer = document.querySelector( |
| 9 | + "[data-portal]", |
| 10 | + ) as HTMLElement; |
| 11 | + existingContainer?.remove(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("renders portal content", async () => { |
| 15 | + render(PortalTest); |
| 16 | + |
| 17 | + const portalContent = await screen.findByText("Portal content"); |
| 18 | + expect(portalContent).toBeInTheDocument(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("creates portal container in document.body", async () => { |
| 22 | + render(PortalTest); |
| 23 | + |
| 24 | + const portalContent = await screen.findByText("Portal content"); |
| 25 | + expect(portalContent).toBeInTheDocument(); |
| 26 | + |
| 27 | + const portalContainer = portalContent.closest("[data-portal]"); |
| 28 | + expect(portalContainer).toBeInTheDocument(); |
| 29 | + expect(portalContainer?.parentElement).toBe(document.body); |
| 30 | + }); |
| 31 | + |
| 32 | + it("portal container has correct attributes and classes", async () => { |
| 33 | + render(PortalTest); |
| 34 | + |
| 35 | + const portalContent = await screen.findByText("Portal content"); |
| 36 | + const portalContainer = portalContent.closest("[data-portal]"); |
| 37 | + assert(portalContainer instanceof HTMLElement); |
| 38 | + |
| 39 | + expect(portalContainer).toHaveAttribute("data-portal"); |
| 40 | + expect(portalContainer).toHaveClass("bx--portal-container"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("multiple portals share the same container", async () => { |
| 44 | + render(PortalMultipleTest); |
| 45 | + |
| 46 | + const portalContent1 = await screen.findByText("Portal content 1"); |
| 47 | + const portalContent2 = await screen.findByText("Portal content 2"); |
| 48 | + const portalContent3 = await screen.findByText("Portal content 3"); |
| 49 | + |
| 50 | + expect(portalContent1).toBeInTheDocument(); |
| 51 | + expect(portalContent2).toBeInTheDocument(); |
| 52 | + expect(portalContent3).toBeInTheDocument(); |
| 53 | + |
| 54 | + const container1 = portalContent1.closest("[data-portal]"); |
| 55 | + const container2 = portalContent2.closest("[data-portal]"); |
| 56 | + const container3 = portalContent3.closest("[data-portal]"); |
| 57 | + |
| 58 | + expect(container1).toBe(container2); |
| 59 | + expect(container2).toBe(container3); |
| 60 | + expect(container1).toBeInTheDocument(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("removes portal container when all instances are unmounted", async () => { |
| 64 | + const { unmount } = render(PortalTest); |
| 65 | + |
| 66 | + const portalContent = await screen.findByText("Portal content"); |
| 67 | + const portalContainer = portalContent.closest("[data-portal]"); |
| 68 | + assert(portalContainer instanceof HTMLElement); |
| 69 | + |
| 70 | + unmount(); |
| 71 | + |
| 72 | + const remainingContainer = document.querySelector("[data-portal]"); |
| 73 | + expect(remainingContainer).not.toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("removes portal container only when last instance is unmounted", async () => { |
| 77 | + const { unmount: unmount1 } = render(PortalTest, { |
| 78 | + props: { portalContent: "Portal 1" }, |
| 79 | + }); |
| 80 | + |
| 81 | + const portalContent1 = await screen.findByText("Portal 1"); |
| 82 | + let portalContainer = portalContent1.closest("[data-portal]"); |
| 83 | + assert(portalContainer instanceof HTMLElement); |
| 84 | + |
| 85 | + const { unmount: unmount2 } = render(PortalTest, { |
| 86 | + props: { portalContent: "Portal 2" }, |
| 87 | + }); |
| 88 | + |
| 89 | + const portalContent2 = await screen.findByText("Portal 2"); |
| 90 | + portalContainer = portalContent2.closest("[data-portal]"); |
| 91 | + assert(portalContainer instanceof HTMLElement); |
| 92 | + |
| 93 | + unmount1(); |
| 94 | + |
| 95 | + portalContainer = document.querySelector("[data-portal]"); |
| 96 | + expect(portalContainer).toBeInTheDocument(); |
| 97 | + expect(await screen.findByText("Portal 2")).toBeInTheDocument(); |
| 98 | + |
| 99 | + unmount2(); |
| 100 | + |
| 101 | + portalContainer = document.querySelector("[data-portal]"); |
| 102 | + expect(portalContainer).not.toBeInTheDocument(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("renders slot content correctly", async () => { |
| 106 | + render(PortalTest, { |
| 107 | + props: { portalContent: "Custom portal content" }, |
| 108 | + }); |
| 109 | + |
| 110 | + const portalContent = await screen.findByText("Custom portal content"); |
| 111 | + expect(portalContent).toBeInTheDocument(); |
| 112 | + }); |
| 113 | + |
| 114 | + it("handles conditional rendering", async () => { |
| 115 | + const { component } = render(PortalTest, { |
| 116 | + props: { showPortal: false }, |
| 117 | + }); |
| 118 | + |
| 119 | + let portalContent = screen.queryByText("Portal content"); |
| 120 | + expect(portalContent).not.toBeInTheDocument(); |
| 121 | + |
| 122 | + let portalContainer = document.querySelector("[data-portal]"); |
| 123 | + expect(portalContainer).not.toBeInTheDocument(); |
| 124 | + |
| 125 | + component.$set({ showPortal: true }); |
| 126 | + |
| 127 | + portalContent = await screen.findByText("Portal content"); |
| 128 | + expect(portalContent).toBeInTheDocument(); |
| 129 | + |
| 130 | + portalContainer = portalContent.closest("[data-portal]"); |
| 131 | + expect(portalContainer).toBeInTheDocument(); |
| 132 | + |
| 133 | + component.$set({ showPortal: false }); |
| 134 | + await tick(); |
| 135 | + |
| 136 | + portalContent = screen.queryByText("Portal content"); |
| 137 | + expect(portalContent).not.toBeInTheDocument(); |
| 138 | + |
| 139 | + portalContainer = document.querySelector("[data-portal]"); |
| 140 | + expect(portalContainer).not.toBeInTheDocument(); |
| 141 | + }); |
| 142 | + |
| 143 | + it("portal container has pointer-events: none", async () => { |
| 144 | + render(PortalTest); |
| 145 | + |
| 146 | + const portalContent = await screen.findByText("Portal content"); |
| 147 | + const portalContainer = portalContent.closest("[data-portal]"); |
| 148 | + assert(portalContainer instanceof HTMLElement); |
| 149 | + |
| 150 | + expect(portalContainer).toHaveClass("bx--portal-container"); |
| 151 | + }); |
| 152 | + |
| 153 | + it("portal content has pointer-events: auto", async () => { |
| 154 | + render(PortalTest); |
| 155 | + |
| 156 | + const portalContent = await screen.findByText("Portal content"); |
| 157 | + expect(portalContent).toBeInTheDocument(); |
| 158 | + const styles = window.getComputedStyle(portalContent); |
| 159 | + expect(styles.pointerEvents).toBe("auto"); |
| 160 | + }); |
| 161 | +}); |
0 commit comments