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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import { render } from '@mongodb-js/testing-library-compass';
import { expect } from 'chai';
import { ContextMenuProvider } from './context-menu-provider';
import type { ContextMenuWrapperProps } from './types';

describe('ContextMenuProvider', function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't related to the failing test but was just part of the changes that did not end up included in the branch I was intending to merge

const TestMenu: React.FC<ContextMenuWrapperProps> = () => (
<div data-testid="test-menu">Test Menu</div>
);

const TestComponent = () => (
<div data-testid="test-content">Test Content</div>
);

describe('when nested', function () {
it('throws an error when providers are nested', function () {
expect(() => {
render(
<ContextMenuProvider menuWrapper={TestMenu}>
<div>
<ContextMenuProvider menuWrapper={TestMenu}>
<TestComponent />
</ContextMenuProvider>
</div>
</ContextMenuProvider>
);
}).to.throw(
'Duplicated ContextMenuProvider found. Please remove the nested provider.'
);
});
});

describe('when not nested', function () {
it('renders without error', function () {
render(
<ContextMenuProvider menuWrapper={TestMenu}>
<TestComponent />
</ContextMenuProvider>
);

expect(document.querySelector('[data-testid="test-content"]')).to.exist;
});
});
});
11 changes: 11 additions & 0 deletions packages/compass-context-menu/src/context-menu-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
useState,
useMemo,
createContext,
useContext,
} from 'react';
import type { ContextMenuContextType, ContextMenuState } from './types';
import type { EnhancedMouseEvent } from './context-menu-content';
Expand All @@ -22,6 +23,16 @@ export function ContextMenuProvider({
menu: ContextMenuState & { close: () => void };
}>;
}) {
// Check if there's already a parent context menu provider
const parentContext = useContext(ContextMenuContext);

// Prevent accidental nested providers
if (parentContext) {
throw new Error(
'Duplicated ContextMenuProvider found. Please remove the nested provider.'
);
}

const [menu, setMenu] = useState<ContextMenuState>({
isOpen: false,
itemGroups: [],
Expand Down
14 changes: 7 additions & 7 deletions packages/compass-context-menu/src/use-context-menu.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('useContextMenu', function () {

it('renders without error', function () {
render(
<ContextMenuProvider wrapper={TestMenu}>
<ContextMenuProvider menuWrapper={TestMenu}>
<TestComponent />
</ContextMenuProvider>
);
Expand All @@ -148,7 +148,7 @@ describe('useContextMenu', function () {
const onRegister = sinon.spy();

render(
<ContextMenuProvider wrapper={TestMenu}>
<ContextMenuProvider menuWrapper={TestMenu}>
<TestComponent onRegister={onRegister} />
</ContextMenuProvider>
);
Expand All @@ -159,7 +159,7 @@ describe('useContextMenu', function () {

it('shows context menu on right click', function () {
render(
<ContextMenuProvider wrapper={TestMenu}>
<ContextMenuProvider menuWrapper={TestMenu}>
<TestComponent />
</ContextMenuProvider>
);
Expand All @@ -174,7 +174,7 @@ describe('useContextMenu', function () {
describe('with nested context menus', function () {
it('shows only parent items when right clicking parent area', function () {
render(
<ContextMenuProvider wrapper={TestMenu}>
<ContextMenuProvider menuWrapper={TestMenu}>
<ParentComponent />
</ContextMenuProvider>
);
Expand All @@ -193,7 +193,7 @@ describe('useContextMenu', function () {

it('shows both parent and child items when right clicking child area', function () {
render(
<ContextMenuProvider wrapper={TestMenu}>
<ContextMenuProvider menuWrapper={TestMenu}>
<ParentComponent>
<ChildComponent />
</ParentComponent>
Expand All @@ -215,7 +215,7 @@ describe('useContextMenu', function () {
const childOnAction = sinon.spy();

render(
<ContextMenuProvider wrapper={TestMenu}>
<ContextMenuProvider menuWrapper={TestMenu}>
<ParentComponent onAction={parentOnAction}>
<ChildComponent onAction={childOnAction} />
</ParentComponent>
Expand All @@ -238,7 +238,7 @@ describe('useContextMenu', function () {
const childOnAction = sinon.spy();

render(
<ContextMenuProvider wrapper={TestMenu}>
<ContextMenuProvider menuWrapper={TestMenu}>
<ParentComponent onAction={parentOnAction}>
<ChildComponent onAction={childOnAction} />
</ParentComponent>
Expand Down
Loading