|
| 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 existingPortals = document.querySelectorAll("[data-portal]"); |
| 9 | + for (const portal of existingPortals) { |
| 10 | + portal.remove(); |
| 11 | + } |
| 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 | + const portalElement = portalContent.closest("[data-portal]"); |
| 21 | + assert(portalElement instanceof HTMLElement); |
| 22 | + expect(portalElement.parentElement).toBe(document.body); |
| 23 | + expect(portalElement.tagName).toBe("DIV"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("multiple portals each have their own instance", async () => { |
| 27 | + render(PortalMultipleTest); |
| 28 | + |
| 29 | + const portalContent1 = await screen.findByText("Portal content 1"); |
| 30 | + const portalContent2 = await screen.findByText("Portal content 2"); |
| 31 | + const portalContent3 = await screen.findByText("Portal content 3"); |
| 32 | + |
| 33 | + expect(portalContent1).toBeInTheDocument(); |
| 34 | + expect(portalContent2).toBeInTheDocument(); |
| 35 | + expect(portalContent3).toBeInTheDocument(); |
| 36 | + |
| 37 | + const portalElement1 = portalContent1.closest("[data-portal]"); |
| 38 | + const portalElement2 = portalContent2.closest("[data-portal]"); |
| 39 | + const portalElement3 = portalContent3.closest("[data-portal]"); |
| 40 | + |
| 41 | + expect(portalElement1).not.toBe(portalElement2); |
| 42 | + expect(portalElement2).not.toBe(portalElement3); |
| 43 | + expect(portalElement1).not.toBe(portalElement3); |
| 44 | + expect(portalElement1).toBeInTheDocument(); |
| 45 | + expect(portalElement2).toBeInTheDocument(); |
| 46 | + expect(portalElement3).toBeInTheDocument(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("removes portal element when instance is unmounted", async () => { |
| 50 | + const { unmount } = render(PortalTest); |
| 51 | + |
| 52 | + const portalContent = await screen.findByText("Portal content"); |
| 53 | + const portalElement = portalContent.closest("[data-portal]"); |
| 54 | + assert(portalElement instanceof HTMLElement); |
| 55 | + |
| 56 | + unmount(); |
| 57 | + |
| 58 | + const remainingPortal = document.querySelector("[data-portal]"); |
| 59 | + expect(remainingPortal).not.toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("each portal instance is independent", async () => { |
| 63 | + const { unmount: unmount1 } = render(PortalTest, { |
| 64 | + props: { portalContent: "Portal 1" }, |
| 65 | + }); |
| 66 | + |
| 67 | + const portalContent1 = await screen.findByText("Portal 1"); |
| 68 | + const portalElement1 = portalContent1.closest("[data-portal]"); |
| 69 | + assert(portalElement1 instanceof HTMLElement); |
| 70 | + |
| 71 | + const { unmount: unmount2 } = render(PortalTest, { |
| 72 | + props: { portalContent: "Portal 2" }, |
| 73 | + }); |
| 74 | + |
| 75 | + const portalContent2 = await screen.findByText("Portal 2"); |
| 76 | + const portalElement2 = portalContent2.closest("[data-portal]"); |
| 77 | + assert(portalElement2 instanceof HTMLElement); |
| 78 | + |
| 79 | + expect(portalElement1).not.toBe(portalElement2); |
| 80 | + |
| 81 | + unmount1(); |
| 82 | + |
| 83 | + const remainingPortals = document.querySelectorAll("[data-portal]"); |
| 84 | + expect(remainingPortals).toHaveLength(1); |
| 85 | + expect(await screen.findByText("Portal 2")).toBeInTheDocument(); |
| 86 | + |
| 87 | + unmount2(); |
| 88 | + |
| 89 | + const finalPortals = document.querySelectorAll("[data-portal]"); |
| 90 | + expect(finalPortals).toHaveLength(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it("renders slot content correctly", async () => { |
| 94 | + render(PortalTest, { |
| 95 | + props: { portalContent: "Custom portal content" }, |
| 96 | + }); |
| 97 | + |
| 98 | + const portalContent = await screen.findByText("Custom portal content"); |
| 99 | + expect(portalContent).toBeInTheDocument(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("handles conditional rendering", async () => { |
| 103 | + const { component } = render(PortalTest, { |
| 104 | + props: { showPortal: false }, |
| 105 | + }); |
| 106 | + |
| 107 | + let portalContent = screen.queryByText("Portal content"); |
| 108 | + expect(portalContent).not.toBeInTheDocument(); |
| 109 | + |
| 110 | + let portalElement = document.querySelector("[data-portal]"); |
| 111 | + expect(portalElement).not.toBeInTheDocument(); |
| 112 | + |
| 113 | + component.$set({ showPortal: true }); |
| 114 | + |
| 115 | + portalContent = await screen.findByText("Portal content"); |
| 116 | + expect(portalContent).toBeInTheDocument(); |
| 117 | + |
| 118 | + portalElement = portalContent.closest("[data-portal]"); |
| 119 | + expect(portalElement).toBeInTheDocument(); |
| 120 | + |
| 121 | + component.$set({ showPortal: false }); |
| 122 | + await tick(); |
| 123 | + |
| 124 | + portalContent = screen.queryByText("Portal content"); |
| 125 | + expect(portalContent).not.toBeInTheDocument(); |
| 126 | + |
| 127 | + portalElement = document.querySelector("[data-portal]"); |
| 128 | + expect(portalElement).not.toBeInTheDocument(); |
| 129 | + }); |
| 130 | + |
| 131 | + it("uses custom tag when tag prop is specified", async () => { |
| 132 | + render(PortalTest, { |
| 133 | + props: { tag: "section" }, |
| 134 | + }); |
| 135 | + |
| 136 | + const portalContent = await screen.findByText("Portal content"); |
| 137 | + const portalElement = portalContent.closest("[data-portal]"); |
| 138 | + assert(portalElement instanceof HTMLElement); |
| 139 | + |
| 140 | + expect(portalElement.tagName).toBe("SECTION"); |
| 141 | + }); |
| 142 | + |
| 143 | + it("supports different custom tags", async () => { |
| 144 | + const { unmount: unmount1 } = render(PortalTest, { |
| 145 | + props: { portalContent: "Article portal", tag: "article" }, |
| 146 | + }); |
| 147 | + |
| 148 | + const portalContent1 = await screen.findByText("Article portal"); |
| 149 | + const portalElement1 = portalContent1.closest("[data-portal]"); |
| 150 | + assert(portalElement1 instanceof HTMLElement); |
| 151 | + expect(portalElement1.tagName).toBe("ARTICLE"); |
| 152 | + |
| 153 | + const { unmount: unmount2 } = render(PortalTest, { |
| 154 | + props: { portalContent: "Section portal", tag: "section" }, |
| 155 | + }); |
| 156 | + |
| 157 | + const portalContent2 = await screen.findByText("Section portal"); |
| 158 | + const portalElement2 = portalContent2.closest("[data-portal]"); |
| 159 | + assert(portalElement2 instanceof HTMLElement); |
| 160 | + expect(portalElement2.tagName).toBe("SECTION"); |
| 161 | + |
| 162 | + unmount1(); |
| 163 | + unmount2(); |
| 164 | + }); |
| 165 | + |
| 166 | + it("forwards rest props to the portal element", async () => { |
| 167 | + render(PortalTest, { |
| 168 | + props: { |
| 169 | + class: "custom-portal-class", |
| 170 | + id: "test-portal-id", |
| 171 | + "data-testid": "portal-test", |
| 172 | + "aria-label": "Test portal", |
| 173 | + style: "background-color: red;", |
| 174 | + }, |
| 175 | + }); |
| 176 | + |
| 177 | + const portalContent = await screen.findByText("Portal content"); |
| 178 | + const portalElement = portalContent.closest("[data-portal]"); |
| 179 | + assert(portalElement instanceof HTMLElement); |
| 180 | + |
| 181 | + expect(portalElement).toHaveClass("custom-portal-class"); |
| 182 | + expect(portalElement).toHaveAttribute("id", "test-portal-id"); |
| 183 | + expect(portalElement).toHaveAttribute("data-testid", "portal-test"); |
| 184 | + expect(portalElement).toHaveAttribute("aria-label", "Test portal"); |
| 185 | + expect(portalElement.getAttribute("style")).toContain("background-color"); |
| 186 | + }); |
| 187 | +}); |
0 commit comments