@@ -270,8 +270,9 @@ describe('PopOver', () => {
270270 const toggle = getByTestId ( 'popover-toggle' ) ;
271271 click ( toggle ) ;
272272 const option1 = getByTestId ( 'option 1' ) ;
273+ const menu = getByTestId ( 'menu' ) ;
273274 act ( ( ) => {
274- fireEvent . keyDown ( document . activeElement || document . body , {
275+ fireEvent . keyDown ( menu , {
275276 key : 'ArrowDown' ,
276277 keyCode : 40 ,
277278 which : 40 ,
@@ -314,4 +315,132 @@ describe('PopOver', () => {
314315 } ) ;
315316 await waitFor ( ( ) => expect ( menu ) . not . toBeInTheDocument ( ) ) ;
316317 } ) ;
318+
319+ describe ( 'handlePopOverClick' , ( ) => {
320+ it ( 'should call onPopOverClick with false when popover is opened (isOpen was false)' , async ( ) => {
321+ const onPopOverClick = jest . fn ( ) ;
322+ const component = (
323+ < PopOver
324+ renderButton = { popOverButton }
325+ children = { testChild }
326+ a11yLabel = "test-label"
327+ onPopOverClick = { onPopOverClick }
328+ />
329+ ) ;
330+ const { getByTestId } = renderWithTheme ( lightTheme , component ) ;
331+ const toggle = getByTestId ( 'popover-toggle' ) ;
332+
333+ click ( toggle ) ;
334+
335+ await waitFor ( ( ) => {
336+ expect ( onPopOverClick ) . toHaveBeenCalledWith ( false ) ;
337+ expect ( onPopOverClick ) . toHaveBeenCalledTimes ( 1 ) ;
338+ } ) ;
339+ } ) ;
340+
341+ it ( 'should call onPopOverClick with true when popover is closed (isOpen was true)' , async ( ) => {
342+ const onPopOverClick = jest . fn ( ) ;
343+ const component = (
344+ < PopOver
345+ renderButton = { popOverButton }
346+ children = { testChild }
347+ a11yLabel = "test-label"
348+ onPopOverClick = { onPopOverClick }
349+ />
350+ ) ;
351+ const { getByTestId } = renderWithTheme ( lightTheme , component ) ;
352+ const toggle = getByTestId ( 'popover-toggle' ) ;
353+
354+ // First click to open
355+ click ( toggle ) ;
356+ // Second click to close
357+ click ( toggle ) ;
358+
359+ await waitFor ( ( ) => {
360+ expect ( onPopOverClick ) . toHaveBeenNthCalledWith ( 1 , false ) ; // First call when opening
361+ expect ( onPopOverClick ) . toHaveBeenNthCalledWith ( 2 , true ) ; // Second call when closing
362+ expect ( onPopOverClick ) . toHaveBeenCalledTimes ( 2 ) ;
363+ } ) ;
364+ } ) ;
365+
366+ it ( 'should toggle isOpen state without calling onPopOverClick when callback is not provided' , ( ) => {
367+ const component = (
368+ < PopOver
369+ renderButton = { popOverButton }
370+ children = { testChild }
371+ a11yLabel = "test-label"
372+ />
373+ ) ;
374+ const { getByTestId, queryByTestId } = renderWithTheme ( lightTheme , component ) ;
375+ const toggle = getByTestId ( 'popover-toggle' ) ;
376+
377+ // Initially closed
378+ expect ( queryByTestId ( 'menu' ) ) . not . toBeInTheDocument ( ) ;
379+
380+ // Click to open
381+ click ( toggle ) ;
382+ expect ( queryByTestId ( 'menu' ) ) . toBeInTheDocument ( ) ;
383+
384+ // Click to close
385+ click ( toggle ) ;
386+ expect ( queryByTestId ( 'menu' ) ) . not . toBeInTheDocument ( ) ;
387+ } ) ;
388+
389+ it ( 'should properly toggle state multiple times and call onPopOverClick with correct values' , async ( ) => {
390+ const onPopOverClick = jest . fn ( ) ;
391+ const component = (
392+ < PopOver
393+ renderButton = { popOverButton }
394+ children = { testChild }
395+ a11yLabel = "test-label"
396+ onPopOverClick = { onPopOverClick }
397+ />
398+ ) ;
399+ const { getByTestId, queryByTestId } = renderWithTheme ( lightTheme , component ) ;
400+ const toggle = getByTestId ( 'popover-toggle' ) ;
401+
402+ // Initial state: closed
403+ expect ( queryByTestId ( 'menu' ) ) . not . toBeInTheDocument ( ) ;
404+
405+ // First click: open
406+ click ( toggle ) ;
407+ await waitFor ( ( ) => {
408+ expect ( queryByTestId ( 'menu' ) ) . toBeInTheDocument ( ) ;
409+ expect ( onPopOverClick ) . toHaveBeenNthCalledWith ( 1 , false ) ;
410+ } ) ;
411+
412+ // Second click: close
413+ click ( toggle ) ;
414+ await waitFor ( ( ) => {
415+ expect ( queryByTestId ( 'menu' ) ) . not . toBeInTheDocument ( ) ;
416+ expect ( onPopOverClick ) . toHaveBeenNthCalledWith ( 2 , true ) ;
417+ } ) ;
418+
419+ // Third click: open again
420+ click ( toggle ) ;
421+ await waitFor ( ( ) => {
422+ expect ( queryByTestId ( 'menu' ) ) . toBeInTheDocument ( ) ;
423+ expect ( onPopOverClick ) . toHaveBeenNthCalledWith ( 3 , false ) ;
424+ expect ( onPopOverClick ) . toHaveBeenCalledTimes ( 3 ) ;
425+ } ) ;
426+ } ) ;
427+
428+ it ( 'should not throw error when onPopOverClick is undefined' , ( ) => {
429+ const component = (
430+ < PopOver
431+ renderButton = { popOverButton }
432+ children = { testChild }
433+ a11yLabel = "test-label"
434+ onPopOverClick = { undefined }
435+ />
436+ ) ;
437+ const { getByTestId } = renderWithTheme ( lightTheme , component ) ;
438+ const toggle = getByTestId ( 'popover-toggle' ) ;
439+
440+ expect ( ( ) => {
441+ click ( toggle ) ;
442+ } ) . not . toThrow ( ) ;
443+ } ) ;
444+ } ) ;
445+
317446} ) ;
0 commit comments