|
1 | 1 | import { render } from "@testing-library/svelte"; |
2 | 2 |
|
3 | 3 | import TestPortalWrapper from "./TestPortalWrapper.svelte"; |
| 4 | +import TestLifecycle from "./TestLifecycle.svelte"; |
| 5 | +import { tick } from "svelte"; |
4 | 6 |
|
5 | | -describe("<Portal />", () => { |
| 7 | +describe("<Portal /> target", () => { |
6 | 8 | let wrapper; |
7 | 9 |
|
8 | 10 | beforeEach(() => { |
@@ -42,3 +44,34 @@ describe("<Portal />", () => { |
42 | 44 | expect(isPortalContainerEmpty).toBe(true); |
43 | 45 | }); |
44 | 46 | }); |
| 47 | + |
| 48 | +describe("<Portal /> lifecycle", () => { |
| 49 | + let targetEl; |
| 50 | + let lifecycleComponent; |
| 51 | + beforeEach(() => { |
| 52 | + let { container, component } = render(TestLifecycle); |
| 53 | + lifecycleComponent = component; |
| 54 | + targetEl = container.querySelector("#modals"); |
| 55 | + }); |
| 56 | + it("should be added and removed from dom", async () => { |
| 57 | + expect(targetEl.children).toHaveLength(1); |
| 58 | + lifecycleComponent.$set({ modalVisible: false }); |
| 59 | + await tick(); |
| 60 | + expect(targetEl.children).toHaveLength(0); |
| 61 | + lifecycleComponent.$set({ modalVisible: true }); |
| 62 | + await tick(); |
| 63 | + expect(targetEl.children).toHaveLength(1); |
| 64 | + }); |
| 65 | + it("should be removed from dom after after outro", async () => { |
| 66 | + lifecycleComponent.$set({ containerVisible: false }); |
| 67 | + await tick(); |
| 68 | + expect(targetEl.children).toHaveLength(1); |
| 69 | + await new Promise((resolve) => { |
| 70 | + const unsubscribe = lifecycleComponent.$on("outroend", () => { |
| 71 | + resolve(); |
| 72 | + unsubscribe(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + expect(targetEl.children).toHaveLength(0); |
| 76 | + }); |
| 77 | +}); |
0 commit comments