|
| 1 | +import { render, screen } from "@testing-library/svelte"; |
| 2 | +import { tick } from "svelte"; |
| 3 | +import FloatingPortalTest from "./FloatingPortal.test.svelte"; |
| 4 | + |
| 5 | +describe("FloatingPortal", () => { |
| 6 | + afterEach(() => { |
| 7 | + const existingContainer = document.querySelector( |
| 8 | + "[data-portal]", |
| 9 | + ) as HTMLElement; |
| 10 | + existingContainer?.remove(); |
| 11 | + }); |
| 12 | + |
| 13 | + it("renders floating portal content", async () => { |
| 14 | + render(FloatingPortalTest); |
| 15 | + |
| 16 | + const referenceButton = await screen.findByText("Reference button"); |
| 17 | + expect(referenceButton).toBeInTheDocument(); |
| 18 | + |
| 19 | + const portalContent = await screen.findByText("Floating portal content"); |
| 20 | + expect(portalContent).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("positions portal relative to reference element", async () => { |
| 24 | + render(FloatingPortalTest); |
| 25 | + |
| 26 | + const referenceButton = await screen.findByText("Reference button"); |
| 27 | + const portalContent = await screen.findByText("Floating portal content"); |
| 28 | + |
| 29 | + expect(referenceButton).toBeInTheDocument(); |
| 30 | + expect(portalContent).toBeInTheDocument(); |
| 31 | + |
| 32 | + const portalContainer = portalContent.closest("[data-portal]"); |
| 33 | + expect(portalContainer).toBeInTheDocument(); |
| 34 | + expect(portalContainer?.parentElement).toBe(document.body); |
| 35 | + }); |
| 36 | + |
| 37 | + it("supports different placement options", async () => { |
| 38 | + const testPlacement = async ( |
| 39 | + placement: |
| 40 | + | "top" |
| 41 | + | "right" |
| 42 | + | "bottom" |
| 43 | + | "left" |
| 44 | + | "top-start" |
| 45 | + | "bottom-end", |
| 46 | + ) => { |
| 47 | + const { unmount } = render(FloatingPortalTest, { |
| 48 | + props: { placement }, |
| 49 | + }); |
| 50 | + |
| 51 | + const portalContent = await screen.findByText("Floating portal content"); |
| 52 | + expect(portalContent).toBeInTheDocument(); |
| 53 | + |
| 54 | + const floatingElement = portalContent.parentElement; |
| 55 | + assert(floatingElement instanceof HTMLElement); |
| 56 | + const styles = window.getComputedStyle(floatingElement); |
| 57 | + if (styles.position) { |
| 58 | + expect(styles.position).toBe("absolute"); |
| 59 | + } |
| 60 | + |
| 61 | + unmount(); |
| 62 | + }; |
| 63 | + |
| 64 | + await testPlacement("top"); |
| 65 | + await testPlacement("right"); |
| 66 | + await testPlacement("bottom"); |
| 67 | + await testPlacement("left"); |
| 68 | + await testPlacement("top-start"); |
| 69 | + await testPlacement("bottom-end"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("handles conditional rendering", async () => { |
| 73 | + const { component } = render(FloatingPortalTest, { |
| 74 | + props: { showPortal: false }, |
| 75 | + }); |
| 76 | + |
| 77 | + let portalContent = screen.queryByText("Floating portal content"); |
| 78 | + expect(portalContent).not.toBeInTheDocument(); |
| 79 | + |
| 80 | + component.$set({ showPortal: true }); |
| 81 | + |
| 82 | + portalContent = await screen.findByText("Floating portal content"); |
| 83 | + expect(portalContent).toBeInTheDocument(); |
| 84 | + |
| 85 | + component.$set({ showPortal: false }); |
| 86 | + await tick(); |
| 87 | + |
| 88 | + portalContent = screen.queryByText("Floating portal content"); |
| 89 | + expect(portalContent).not.toBeInTheDocument(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("updates position when offset changes", async () => { |
| 93 | + const { component } = render(FloatingPortalTest); |
| 94 | + |
| 95 | + const portalContent = await screen.findByText("Floating portal content"); |
| 96 | + expect(portalContent).toBeInTheDocument(); |
| 97 | + |
| 98 | + const floatingElement = portalContent.parentElement; |
| 99 | + assert(floatingElement instanceof HTMLElement); |
| 100 | + |
| 101 | + component.$set({ offset: 20 }); |
| 102 | + await tick(); |
| 103 | + |
| 104 | + const styles = window.getComputedStyle(floatingElement); |
| 105 | + if (styles.position) { |
| 106 | + expect(styles.position).toBe("absolute"); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + it("renders custom slot content", async () => { |
| 111 | + render(FloatingPortalTest, { |
| 112 | + props: { portalContent: "Custom floating content" }, |
| 113 | + }); |
| 114 | + |
| 115 | + const portalContent = await screen.findByText("Custom floating content"); |
| 116 | + expect(portalContent).toBeInTheDocument(); |
| 117 | + }); |
| 118 | + |
| 119 | + it("cleans up auto-update on unmount", async () => { |
| 120 | + const { unmount } = render(FloatingPortalTest); |
| 121 | + |
| 122 | + const portalContent = await screen.findByText("Floating portal content"); |
| 123 | + expect(portalContent).toBeInTheDocument(); |
| 124 | + |
| 125 | + unmount(); |
| 126 | + |
| 127 | + const remainingContainer = document.querySelector("[data-portal]"); |
| 128 | + expect(remainingContainer).not.toBeInTheDocument(); |
| 129 | + }); |
| 130 | + |
| 131 | + it("works with autoUpdate disabled", async () => { |
| 132 | + render(FloatingPortalTest, { |
| 133 | + props: { autoUpdate: false }, |
| 134 | + }); |
| 135 | + |
| 136 | + const portalContent = await screen.findByText("Floating portal content"); |
| 137 | + expect(portalContent).toBeInTheDocument(); |
| 138 | + |
| 139 | + const floatingElement = portalContent.parentElement; |
| 140 | + assert(floatingElement instanceof HTMLElement); |
| 141 | + const styles = window.getComputedStyle(floatingElement); |
| 142 | + if (styles.position) { |
| 143 | + expect(styles.position).toBe("absolute"); |
| 144 | + } |
| 145 | + }); |
| 146 | +}); |
0 commit comments