Skip to content

Commit 09dae59

Browse files
authored
Bugfix/handle submenu callback (#287)
* Handle the case where submenu is missing * Unit test callback crashing fix * Updated cleanup to exit early when submenu is gone
1 parent 547f841 commit 09dae59

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/SubMenu.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default class SubMenu extends AbstractMenu {
6868
if (this.props.forceOpen || this.state.visible) {
6969
const wrapper = window.requestAnimationFrame || setTimeout;
7070
wrapper(() => {
71+
if (!this.subMenu) return;
7172
const styles = this.props.rtl
7273
? this.getRTLMenuPosition()
7374
: this.getMenuPosition();
@@ -88,6 +89,7 @@ export default class SubMenu extends AbstractMenu {
8889
});
8990
} else {
9091
const cleanup = () => {
92+
if (!this.subMenu) return;
9193
this.subMenu.removeEventListener('transitionend', cleanup);
9294
this.subMenu.style.removeProperty('bottom');
9395
this.subMenu.style.removeProperty('right');

tests/SubMenu.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,19 @@ describe('<SubMenu/>', () => {
4242
await user.hover(wrapper);
4343
await expect(() => waitForMenuVisible(submenu)).rejects.toThrow();
4444
});
45+
46+
it('should not crash after immediate unmount', async () => {
47+
let callback = null;
48+
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => {
49+
callback = cb;
50+
return 1;
51+
});
52+
const { unmount } = render(<SubMenu title='Title' hoverDelay={0} />);
53+
const user = userEvent.setup();
54+
const wrapper = screen.getByText('Title');
55+
await user.hover(wrapper);
56+
unmount();
57+
58+
expect(callback).not.toThrow();
59+
});
4560
});

0 commit comments

Comments
 (0)