Skip to content

Commit 8e30a83

Browse files
committed
fix: add tests
1 parent 6838513 commit 8e30a83

File tree

3 files changed

+26
-40
lines changed

3 files changed

+26
-40
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
'use strict';
2-
module.exports = require('@mongodb-js/mocha-config-compass');
2+
module.exports = require('@mongodb-js/mocha-config-compass/react');

packages/compass-context-menu/src/use-context-menu.spec.tsx

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,51 @@
11
import React from 'react';
2-
import {
3-
render,
4-
screen,
5-
cleanup,
6-
userEvent,
7-
} from '@mongodb-js/testing-library-compass';
2+
import { render, screen, userEvent } from '@mongodb-js/testing-library-compass';
83
import { expect } from 'chai';
94
import sinon from 'sinon';
105
import { useContextMenu } from './use-context-menu';
116
import { ContextMenuProvider } from './context-menu-provider';
127
import type { MenuItem } from './types';
138

9+
type TestMenuItem = MenuItem & { id: number };
10+
1411
describe('useContextMenu', function () {
15-
const TestMenu: React.FC<{ items: MenuItem[] }> = ({ items }) => (
12+
const TestMenu: React.FC<{ items: TestMenuItem[] }> = ({ items }) => (
1613
<div data-testid="test-menu">
1714
{items.map((item, idx) => (
18-
<div key={idx} data-testid={`menu-item-${item.label}`}>
15+
<div key={idx} data-testid={`menu-item-${item.id}`}>
1916
{item.label}
2017
</div>
2118
))}
2219
</div>
2320
);
2421

25-
const TestComponent = ({
26-
onRegister,
27-
}: {
28-
onRegister?: (ref: any) => void;
29-
}) => {
22+
const TestComponent = () => {
3023
const contextMenu = useContextMenu({ Menu: TestMenu });
31-
const items: MenuItem[] = [
24+
const items: TestMenuItem[] = [
3225
{
33-
label: 'Test Item',
26+
id: 1,
27+
label: 'Test A',
28+
onAction: () => {
29+
/* noop */
30+
},
31+
},
32+
{
33+
id: 2,
34+
label: 'Test B',
3435
onAction: () => {
3536
/* noop */
3637
},
3738
},
3839
];
3940
const ref = contextMenu.registerItems(items);
4041

41-
React.useEffect(() => {
42-
onRegister?.(ref);
43-
}, [ref, onRegister]);
44-
4542
return (
4643
<div data-testid="test-trigger" ref={ref}>
4744
Test Component
4845
</div>
4946
);
5047
};
5148

52-
afterEach(cleanup);
53-
5449
describe('when used outside provider', function () {
5550
it('throws an error', function () {
5651
expect(() => {
@@ -59,7 +54,7 @@ describe('useContextMenu', function () {
5954
});
6055
});
6156

62-
describe('when used inside provider', function () {
57+
describe('with valid provider', function () {
6358
beforeEach(() => {
6459
// Create the container for the context menu portal
6560
const container = document.createElement('div');
@@ -85,31 +80,22 @@ describe('useContextMenu', function () {
8580
expect(screen.getByTestId('test-trigger')).to.exist;
8681
});
8782

88-
it('registers context menu event listener', function () {
89-
const onRegister = sinon.spy();
90-
91-
render(
92-
<ContextMenuProvider>
93-
<TestComponent onRegister={onRegister} />
94-
</ContextMenuProvider>
95-
);
96-
97-
expect(onRegister).to.have.been.calledOnce;
98-
expect(onRegister.firstCall.args[0]).to.be.a('function');
99-
});
100-
10183
it('shows context menu on right click', function () {
10284
render(
10385
<ContextMenuProvider>
10486
<TestComponent />
10587
</ContextMenuProvider>
10688
);
10789

90+
expect(screen.queryByTestId('menu-item-1')).not.to.exist;
91+
expect(screen.queryByTestId('menu-item-2')).not.to.exist;
92+
10893
const trigger = screen.getByTestId('test-trigger');
10994
userEvent.click(trigger, { button: 2 });
11095

11196
// The menu should be rendered in the portal
112-
expect(screen.getByTestId('menu-item-Test Item')).to.exist;
97+
expect(screen.getByTestId('menu-item-1')).to.exist;
98+
expect(screen.getByTestId('menu-item-2')).to.exist;
11399
});
114100

115101
it('cleans up previous event listener when ref changes', function () {

packages/compass-context-menu/src/use-context-menu.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import type { MenuItem } from './types';
66
/**
77
* @returns an object with methods to {@link register} content for the menu and {@link close} the menu
88
*/
9-
export function useContextMenu({
9+
export function useContextMenu<T extends MenuItem>({
1010
Menu,
1111
}: {
1212
Menu: React.ComponentType<{
13-
items: MenuItem[];
13+
items: T[];
1414
}>;
1515
}) {
1616
// Get the close function from the ContextProvider
@@ -47,7 +47,7 @@ export function useContextMenu({
4747
}
4848
};
4949
},
50-
registerItems(items: MenuItem[]) {
50+
registerItems(items: T[]) {
5151
return this.register(() => <Menu items={items} />);
5252
},
5353
};

0 commit comments

Comments
 (0)