11import { component$ } from '@builder.io/qwik' ;
2- import { Popover } from './popover' ;
2+ import { Popover , PopoverProps } from './popover' ;
33import { PopoverContent } from './popover-content' ;
44import { PopoverTrigger } from './popover-trigger' ;
55
6- const PopoverComponent = component$ ( ( ) => {
6+ const PopoverComponent = component$ ( ( props : PopoverProps ) => {
77 return (
8- < Popover >
8+ < Popover { ... props } >
99 < PopoverContent > popover content</ PopoverContent >
1010 < PopoverTrigger > trigger text</ PopoverTrigger >
1111 </ Popover >
1212 ) ;
1313} ) ;
1414
1515describe ( 'Popover' , ( ) => {
16+ function clickOnTrigger ( ) {
17+ cy . findByRole ( 'button' ) . click ( ) ;
18+ }
19+
20+ function clickOutside ( ) {
21+ cy . get ( 'body' ) . click ( 'bottomRight' ) ;
22+ }
23+
24+ function assertOpen ( ) {
25+ cy . findByRole ( 'dialog' )
26+ . should ( 'have.class' , 'open' )
27+ . should ( 'not.have.class' , 'close' ) ;
28+ }
29+
30+ function assertClosed ( ) {
31+ cy . findByRole ( 'dialog' ) . should ( 'not.have.class' , 'open' ) ;
32+ }
33+
34+ function hoverOnTrigger ( ) {
35+ cy . findByRole ( 'button' ) . trigger ( 'mouseover' ) ;
36+ }
37+
1638 it ( 'INIT' , ( ) => {
1739 cy . mount ( < PopoverComponent /> ) ;
1840
@@ -23,16 +45,69 @@ describe('Popover', () => {
2345 cy . mount ( < PopoverComponent /> ) ;
2446
2547 cy . findByRole ( 'button' ) . should ( 'contain' , 'trigger text' ) ;
26- cy . findByRole ( 'dialog' ) . should ( 'not.have.class' , 'open' ) ;
48+ cy . findByRole ( 'dialog' ) . should ( 'contain' , 'popover content' ) ;
49+ assertClosed ( ) ;
2750 } ) ;
2851
29- it ( 'should open the popover on click' , ( ) => {
52+ it ( 'should render the component with content being open when isOpen is set to true' , ( ) => {
53+ cy . mount ( < PopoverComponent isOpen /> ) ;
54+
55+ assertOpen ( ) ;
56+ } ) ;
57+
58+ it ( 'should open the content when clicking on trigger' , ( ) => {
3059 cy . mount ( < PopoverComponent /> ) ;
3160
32- cy . findByRole ( 'button' ) . click ( ) ;
61+ assertClosed ( ) ;
3362
34- cy . findByRole ( 'dialog' )
35- . should ( 'have.class' , 'open' )
36- . should ( 'contain' , 'popover content' ) ;
63+ clickOnTrigger ( ) ;
64+
65+ assertOpen ( ) ;
66+ } ) ;
67+
68+ it ( 'should close the content when clicking outside' , ( ) => {
69+ cy . mount ( < PopoverComponent /> ) ;
70+
71+ clickOnTrigger ( ) ;
72+
73+ assertOpen ( ) ;
74+
75+ clickOutside ( ) ;
76+
77+ assertClosed ( ) ;
78+ } ) ;
79+
80+ it ( 'should not close the content when clicking outside and disabledClickOutside is set to true' , ( ) => {
81+ cy . mount ( < PopoverComponent disableClickOutSide /> ) ;
82+
83+ clickOnTrigger ( ) ;
84+
85+ assertOpen ( ) ;
86+
87+ clickOutside ( ) ;
88+
89+ assertOpen ( ) ;
90+ } ) ;
91+
92+ it ( 'should open the content when hovering over trigger' , ( ) => {
93+ cy . mount ( < PopoverComponent triggerEvent = "mouseOver" /> ) ;
94+
95+ assertClosed ( ) ;
96+
97+ hoverOnTrigger ( ) ;
98+
99+ assertOpen ( ) ;
100+ } ) ;
101+
102+ it ( 'should close the content when hovering over trigger and exiting' , ( ) => {
103+ cy . mount ( < PopoverComponent triggerEvent = "mouseOver" /> ) ;
104+
105+ hoverOnTrigger ( ) ;
106+
107+ assertOpen ( ) ;
108+
109+ clickOutside ( ) ;
110+
111+ assertClosed ( ) ;
37112 } ) ;
38113} ) ;
0 commit comments