1
1
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' ;
8
3
import { expect } from 'chai' ;
9
4
import sinon from 'sinon' ;
10
5
import { useContextMenu } from './use-context-menu' ;
11
6
import { ContextMenuProvider } from './context-menu-provider' ;
12
7
import type { MenuItem } from './types' ;
13
8
9
+ type TestMenuItem = MenuItem & { id : number } ;
10
+
14
11
describe ( 'useContextMenu' , function ( ) {
15
- const TestMenu : React . FC < { items : MenuItem [ ] } > = ( { items } ) => (
12
+ const TestMenu : React . FC < { items : TestMenuItem [ ] } > = ( { items } ) => (
16
13
< div data-testid = "test-menu" >
17
14
{ items . map ( ( item , idx ) => (
18
- < div key = { idx } data-testid = { `menu-item-${ item . label } ` } >
15
+ < div key = { idx } data-testid = { `menu-item-${ item . id } ` } >
19
16
{ item . label }
20
17
</ div >
21
18
) ) }
22
19
</ div >
23
20
) ;
24
21
25
- const TestComponent = ( {
26
- onRegister,
27
- } : {
28
- onRegister ?: ( ref : any ) => void ;
29
- } ) => {
22
+ const TestComponent = ( ) => {
30
23
const contextMenu = useContextMenu ( { Menu : TestMenu } ) ;
31
- const items : MenuItem [ ] = [
24
+ const items : TestMenuItem [ ] = [
32
25
{
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' ,
34
35
onAction : ( ) => {
35
36
/* noop */
36
37
} ,
37
38
} ,
38
39
] ;
39
40
const ref = contextMenu . registerItems ( items ) ;
40
41
41
- React . useEffect ( ( ) => {
42
- onRegister ?.( ref ) ;
43
- } , [ ref , onRegister ] ) ;
44
-
45
42
return (
46
43
< div data-testid = "test-trigger" ref = { ref } >
47
44
Test Component
48
45
</ div >
49
46
) ;
50
47
} ;
51
48
52
- afterEach ( cleanup ) ;
53
-
54
49
describe ( 'when used outside provider' , function ( ) {
55
50
it ( 'throws an error' , function ( ) {
56
51
expect ( ( ) => {
@@ -59,7 +54,7 @@ describe('useContextMenu', function () {
59
54
} ) ;
60
55
} ) ;
61
56
62
- describe ( 'when used inside provider' , function ( ) {
57
+ describe ( 'with valid provider' , function ( ) {
63
58
beforeEach ( ( ) => {
64
59
// Create the container for the context menu portal
65
60
const container = document . createElement ( 'div' ) ;
@@ -85,31 +80,22 @@ describe('useContextMenu', function () {
85
80
expect ( screen . getByTestId ( 'test-trigger' ) ) . to . exist ;
86
81
} ) ;
87
82
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
-
101
83
it ( 'shows context menu on right click' , function ( ) {
102
84
render (
103
85
< ContextMenuProvider >
104
86
< TestComponent />
105
87
</ ContextMenuProvider >
106
88
) ;
107
89
90
+ expect ( screen . queryByTestId ( 'menu-item-1' ) ) . not . to . exist ;
91
+ expect ( screen . queryByTestId ( 'menu-item-2' ) ) . not . to . exist ;
92
+
108
93
const trigger = screen . getByTestId ( 'test-trigger' ) ;
109
94
userEvent . click ( trigger , { button : 2 } ) ;
110
95
111
96
// 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 ;
113
99
} ) ;
114
100
115
101
it ( 'cleans up previous event listener when ref changes' , function ( ) {
0 commit comments