|
| 1 | +/** |
| 2 | + * This is a reference implementation of the usePortal hook from react-useportal: https://github.com/iamthesiz/react-useportal/blob/master/usePortal.test.ts |
| 3 | + * The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/react-useportal/blob/master/license.md |
| 4 | + */ |
| 5 | + |
| 6 | +import { renderHook, act } from "@testing-library/react"; |
| 7 | +import { usePortal, errorMessage1 } from "./usePortal"; |
| 8 | + |
| 9 | +jest.mock("./useSSR", () => ({ |
| 10 | + useSSR: jest.fn(() => ({ isServer: false, isBrowser: true })), |
| 11 | +})); |
| 12 | + |
| 13 | +describe("usePortal", () => { |
| 14 | + it("should not be open", () => { |
| 15 | + const { result } = renderHook(() => usePortal()); |
| 16 | + const { isOpen } = result.current; |
| 17 | + expect(isOpen).toBe(false); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should error if no event is passed and no ref is set", () => { |
| 21 | + const { result } = renderHook(() => usePortal()); |
| 22 | + try { |
| 23 | + result.current.openPortal(); |
| 24 | + } catch (err) { |
| 25 | + expect(err.message).toBe(errorMessage1); |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + it("does not error if programmatically opening the portal", () => { |
| 30 | + const { result } = renderHook(() => |
| 31 | + usePortal({ programmaticallyOpen: true }), |
| 32 | + ); |
| 33 | + act(() => { |
| 34 | + expect(result.current.openPortal).not.toThrow(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should open portal when openPortal is called", () => { |
| 39 | + const { result } = renderHook(() => |
| 40 | + usePortal({ programmaticallyOpen: true }), |
| 41 | + ); |
| 42 | + act(() => { |
| 43 | + result.current.openPortal(); |
| 44 | + }); |
| 45 | + expect(result.current.isOpen).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should close portal when closePortal is called", () => { |
| 49 | + const { result } = renderHook(() => usePortal({ isOpen: true })); |
| 50 | + act(() => { |
| 51 | + result.current.closePortal(); |
| 52 | + }); |
| 53 | + expect(result.current.isOpen).toBe(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should toggle portal state when togglePortal is called", () => { |
| 57 | + const { result } = renderHook(() => |
| 58 | + usePortal({ programmaticallyOpen: true }), |
| 59 | + ); |
| 60 | + act(() => { |
| 61 | + result.current.togglePortal(); |
| 62 | + }); |
| 63 | + expect(result.current.isOpen).toBe(true); |
| 64 | + act(() => { |
| 65 | + result.current.togglePortal(); |
| 66 | + }); |
| 67 | + expect(result.current.isOpen).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should throw error when openPortal is called without event and programmaticallyOpen is false", () => { |
| 71 | + const { result } = renderHook(() => |
| 72 | + usePortal({ programmaticallyOpen: false }), |
| 73 | + ); |
| 74 | + expect(() => { |
| 75 | + act(() => { |
| 76 | + result.current.openPortal(); |
| 77 | + }); |
| 78 | + }).toThrow(errorMessage1); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should call onOpen callback when portal is opened", () => { |
| 82 | + const onOpen = jest.fn(); |
| 83 | + const { result } = renderHook(() => |
| 84 | + usePortal({ onOpen, programmaticallyOpen: true }), |
| 85 | + ); |
| 86 | + act(() => { |
| 87 | + result.current.openPortal(); |
| 88 | + }); |
| 89 | + expect(onOpen).toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should call onClose callback when portal is closed", () => { |
| 93 | + const onClose = jest.fn(); |
| 94 | + const { result } = renderHook(() => usePortal({ isOpen: true, onClose })); |
| 95 | + act(() => { |
| 96 | + result.current.closePortal(); |
| 97 | + }); |
| 98 | + expect(onClose).toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should close portal when Escape key is pressed", () => { |
| 102 | + const { result } = renderHook(() => usePortal({ isOpen: true })); |
| 103 | + act(() => { |
| 104 | + const event = new KeyboardEvent("keydown", { key: "Escape" }); |
| 105 | + document.dispatchEvent(event); |
| 106 | + }); |
| 107 | + expect(result.current.isOpen).toBe(false); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should close portal when clicking outside", () => { |
| 111 | + const { result } = renderHook(() => usePortal({ isOpen: true })); |
| 112 | + act(() => { |
| 113 | + const event = new MouseEvent("mousedown", { bubbles: true }); |
| 114 | + document.body.dispatchEvent(event); |
| 115 | + }); |
| 116 | + expect(result.current.isOpen).toBe(false); |
| 117 | + }); |
| 118 | +}); |
0 commit comments