1
1
import { component$ } from '@builder.io/qwik' ;
2
- import { Popover } from './popover' ;
2
+ import { Popover , PopoverProps } from './popover' ;
3
3
import { PopoverContent } from './popover-content' ;
4
4
import { PopoverTrigger } from './popover-trigger' ;
5
5
6
- const PopoverComponent = component$ ( ( ) => {
6
+ const PopoverComponent = component$ ( ( props : PopoverProps ) => {
7
7
return (
8
- < Popover >
8
+ < Popover { ... props } >
9
9
< PopoverContent > popover content</ PopoverContent >
10
10
< PopoverTrigger > trigger text</ PopoverTrigger >
11
11
</ Popover >
12
12
) ;
13
13
} ) ;
14
14
15
15
describe ( '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
+
16
38
it ( 'INIT' , ( ) => {
17
39
cy . mount ( < PopoverComponent /> ) ;
18
40
@@ -23,16 +45,69 @@ describe('Popover', () => {
23
45
cy . mount ( < PopoverComponent /> ) ;
24
46
25
47
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 ( ) ;
27
50
} ) ;
28
51
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' , ( ) => {
30
59
cy . mount ( < PopoverComponent /> ) ;
31
60
32
- cy . findByRole ( 'button' ) . click ( ) ;
61
+ assertClosed ( ) ;
33
62
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 ( ) ;
37
112
} ) ;
38
113
} ) ;
0 commit comments