Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/allotment.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { act } from "react";
import { createRoot, Root } from "react-dom/client";

import { Allotment } from "./index";

class ResizeObserverMock {
observe() {}
unobserve() {}
disconnect() {}
}

describe("Allotment", () => {
let container: HTMLDivElement;
let root: Root;

beforeAll(() => {
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
(globalThis as any).ResizeObserver ??= ResizeObserverMock;
});

beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
});

afterEach(() => {
act(() => root.unmount());
container.remove();
});

test("double-clicking a sash calls the latest onReset", () => {
const firstOnReset = jest.fn();
const secondOnReset = jest.fn();

const render = (onReset: () => void) =>
act(() => {
root.render(
<Allotment defaultSizes={[100, 100]} onReset={onReset}>
<div key="a" />
<div key="b" />
</Allotment>,
);
});

render(firstOnReset);
render(secondOnReset);

const sash = container.querySelector(".sash");
expect(sash).not.toBeNull();

act(() => {
sash!.dispatchEvent(new MouseEvent("dblclick", { bubbles: true }));
});

expect(firstOnReset).not.toHaveBeenCalled();
expect(secondOnReset).toHaveBeenCalledTimes(1);
});
});
9 changes: 7 additions & 2 deletions src/allotment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
const splitViewViewRef = useRef(new Map<React.Key, HTMLElement>());
const layoutService = useRef<LayoutService>(new LayoutService());
const views = useRef<PaneView[]>([]);
const onResetRef = useRef(onReset);

const [dimensionsInitialized, setDimensionsInitialized] = useState(false);

Expand Down Expand Up @@ -287,8 +288,8 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
});

splitViewRef.current.on("sashreset", (index: number) => {
if (onReset) {
onReset();
if (onResetRef.current) {
onResetRef.current();
} else {
if (resizeToPreferredSize(index)) {
return;
Expand Down Expand Up @@ -470,6 +471,10 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
}
}, [onDragEnd]);

useEffect(() => {
onResetRef.current = onReset;
}, [onReset]);

useResizeObserver({
ref: containerRef,
onResize: ({ width, height }) => {
Expand Down
Loading