|
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 3 | +import { act } from "react-dom/test-utils"; |
| 4 | +import { Panel } from "./Panel"; |
| 5 | +import { PanelGroup } from "./PanelGroup"; |
| 6 | +import { PanelResizeHandle } from "./PanelResizeHandle"; |
| 7 | + |
| 8 | +describe("PanelGroup", () => { |
| 9 | + let expectedWarnings: string[] = []; |
| 10 | + |
| 11 | + function expectWarning(expectedMessage: string) { |
| 12 | + expectedWarnings.push(expectedMessage); |
| 13 | + } |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + // @ts-expect-error |
| 17 | + global.IS_REACT_ACT_ENVIRONMENT = true; |
| 18 | + |
| 19 | + expectedWarnings = []; |
| 20 | + |
| 21 | + vi.spyOn(console, "warn").mockImplementation((actualMessage: string) => { |
| 22 | + const match = expectedWarnings.findIndex((expectedMessage) => { |
| 23 | + return actualMessage.includes(expectedMessage); |
| 24 | + }); |
| 25 | + |
| 26 | + if (match >= 0) { |
| 27 | + expectedWarnings.splice(match, 1); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + throw Error(`Unexpected warning: ${actualMessage}`); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + vi.clearAllMocks(); |
| 37 | + expect(expectedWarnings).toHaveLength(0); |
| 38 | + }); |
| 39 | + |
| 40 | + describe("DEV warnings", () => { |
| 41 | + test("should warn about server rendered panels with no default size", () => { |
| 42 | + act(() => { |
| 43 | + // No warning expected if default sizes provided |
| 44 | + renderToStaticMarkup( |
| 45 | + <PanelGroup direction="horizontal"> |
| 46 | + <Panel defaultSize={100} /> |
| 47 | + <PanelResizeHandle /> |
| 48 | + <Panel defaultSize={1_000} /> |
| 49 | + </PanelGroup> |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + expectWarning( |
| 54 | + "Panel defaultSize prop recommended to avoid layout shift after server rendering" |
| 55 | + ); |
| 56 | + |
| 57 | + act(() => { |
| 58 | + renderToStaticMarkup( |
| 59 | + <PanelGroup direction="horizontal"> |
| 60 | + <Panel id="one" /> |
| 61 | + </PanelGroup> |
| 62 | + ); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
0 commit comments