11import 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' ;
83import { expect } from 'chai' ;
94import sinon from 'sinon' ;
105import { useContextMenu } from './use-context-menu' ;
116import { ContextMenuProvider } from './context-menu-provider' ;
127import type { MenuItem } from './types' ;
138
9+ type TestMenuItem = MenuItem & { id : number } ;
10+
1411describe ( '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 ( ) {
0 commit comments