Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Feb 6, 2024
1 parent d2f6119 commit 12c8d67
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,24 @@ describe('HeaderActionMenu', () => {
expect(unmounts.FOO).toHaveBeenCalledTimes(1);
expect(unmounts.BAR).not.toHaveBeenCalled();
});

it('calls mount point `unmount` when unmounts', () => {
const TestComponent = () => {
const mounter = useHeaderActionMenuMounter(menuMount$);
return <HeaderActionMenu mounter={mounter} />;
};
component = mount(<TestComponent />);

act(() => {
menuMount$.next(createMountPoint('FOO'));
});
refresh();

expect(Object.keys(unmounts)).toEqual(['FOO']);
expect(unmounts.FOO).not.toHaveBeenCalled();

component.unmount();

expect(unmounts.FOO).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ export const HeaderActionMenu: FC<HeaderActionMenuProps> = ({ mounter }) => {
const unmountRef = useRef<UnmountCallback | null>(null);

useLayoutEffect(() => {
if (unmountRef.current) {
unmountRef.current();
unmountRef.current = null;
}

if (mounter.mount && elementRef.current) {
try {
unmountRef.current = mounter.mount(elementRef.current);
Expand All @@ -53,6 +48,12 @@ export const HeaderActionMenu: FC<HeaderActionMenuProps> = ({ mounter }) => {
console.error(e);
}
}
return () => {
if (unmountRef.current) {
unmountRef.current();
unmountRef.current = null;
}
};
}, [mounter]);

return <div data-test-subj="headerAppActionMenu" ref={elementRef} />;
Expand Down

0 comments on commit 12c8d67

Please sign in to comment.