@@ -4,6 +4,7 @@ import '@testing-library/jest-dom'
44import { Tips , Close } from '@nutui/icons-react'
55import Popover from '../index'
66import Button from '@/packages/button'
7+ import { FullPosition } from '@/types'
78
89const itemList = [
910 {
@@ -27,6 +28,10 @@ const itemListOne = [
2728 icon : < Tips /> ,
2829 action : {
2930 icon : < Close /> ,
31+ onClick : ( e : any ) => {
32+ console . log ( 'onclick 1' )
33+ e . stopPropagation ( )
34+ } ,
3035 } ,
3136 } ,
3237]
@@ -51,9 +56,7 @@ const itemListDisabled = [
5156test ( 'render popover content' , async ( ) => {
5257 const { container } = render (
5358 < Popover visible list = { itemList } >
54- < Button type = "primary" shape = "square" >
55- 基础用法
56- </ Button >
59+ < Button type = "primary" > 基础用法</ Button >
5760 </ Popover >
5861 )
5962 const content = document . querySelectorAll ( '.nut-popover-content' ) [ 0 ]
@@ -76,9 +79,7 @@ test('render popover content dark', async () => {
7679test ( 'render popover position' , async ( ) => {
7780 render (
7881 < Popover visible list = { itemList } location = "bottom-left" >
79- < Button type = "primary" shape = "square" >
80- 基础用法
81- </ Button >
82+ < Button type = "primary" > 基础用法</ Button >
8283 </ Popover >
8384 )
8485 const content = document . querySelectorAll ( '.nut-popover-content' ) [ 0 ]
@@ -87,66 +88,43 @@ test('render popover position', async () => {
8788 )
8889} )
8990
90- test ( 'render popover position2 ' , async ( ) => {
91- const { container } = render (
91+ test ( 'render popover position with arrowOffset ' , async ( ) => {
92+ const { rerender } = render (
9293 < Popover visible list = { itemList } location = "bottom-left" arrowOffset = { 20 } >
93- < Button type = "primary" shape = "square" >
94- 基础用法
95- </ Button >
94+ < Button type = "primary" > 基础用法</ Button >
9695 </ Popover >
9796 )
98- const content = document . querySelectorAll (
99- '.nut-popover-arrow'
100- ) [ 0 ] as HTMLElement
101- expect ( content ) . toHaveAttribute ( 'style' , 'left: 36px;' )
102- } )
10397
104- test ( 'render popover position22' , async ( ) => {
105- const { container } = render (
106- < Popover visible list = { itemList } arrowOffset = { 20 } >
107- < Button type = "primary" shape = "square" >
108- 基础用法
109- </ Button >
110- </ Popover >
111- )
112- const content = document . querySelectorAll (
113- '.nut-popover-arrow'
114- ) [ 0 ] as HTMLElement
115- expect ( content ) . toHaveAttribute ( 'style' , 'left: calc(50% + 20px);' )
116- } )
98+ const checkArrowStyles = ( location : FullPosition , expectedStyles : string ) => {
99+ rerender (
100+ < Popover visible list = { itemList } location = { location } arrowOffset = { 20 } >
101+ < Button type = "primary" > 基础用法</ Button >
102+ </ Popover >
103+ )
104+ content = document . querySelectorAll ( '.nut-popover-arrow' ) [ 0 ]
105+ expect ( content ) . toHaveAttribute ( 'style' , expectedStyles )
106+ }
117107
118- test ( 'render popover position3' , async ( ) => {
119- const { container } = render (
120- < Popover visible list = { itemList } location = "left-top" arrowOffset = { 20 } >
121- < Button type = "primary" shape = "square" >
122- 基础用法
123- </ Button >
124- </ Popover >
125- )
126- const content = document . querySelectorAll (
127- '.nut-popover-arrow'
128- ) [ 0 ] as HTMLElement
129- expect ( content ) . toHaveAttribute ( 'style' , 'top: -4px;' )
130- } )
108+ let content = document . querySelectorAll ( '.nut-popover-arrow' ) [ 0 ]
109+ expect ( content ) . toHaveAttribute ( 'style' , 'left: 36px;' )
131110
132- test ( 'render popover position33' , async ( ) => {
133- const { container } = render (
134- < Popover visible list = { itemList } location = "left" arrowOffset = { 20 } >
135- < Button type = "primary" shape = "square" >
136- 基础用法
137- </ Button >
138- </ Popover >
139- )
140- const content = document . querySelectorAll (
141- '.nut-popover-arrow'
142- ) [ 0 ] as HTMLElement
143- expect ( content ) . toHaveAttribute ( 'style' , 'top: calc(50% - 20px);' )
111+ checkArrowStyles ( 'bottom' , 'left: calc(50% + 20px);' )
112+ checkArrowStyles ( 'bottom-right' , 'right: -4px;' )
113+ checkArrowStyles ( 'left' , 'top: calc(50% - 20px);' )
114+ checkArrowStyles ( 'left-bottom' , 'bottom: 36px;' )
115+ checkArrowStyles ( 'left-top' , 'top: -4px;' )
116+ checkArrowStyles ( 'right' , 'top: calc(50% - 20px);' )
117+ checkArrowStyles ( 'right-bottom' , 'bottom: 36px;' )
118+ checkArrowStyles ( 'right-top' , 'top: -4px;' )
119+ checkArrowStyles ( 'top-right' , 'right: -4px;' )
120+ checkArrowStyles ( 'top-left' , 'left: 36px;' )
121+ checkArrowStyles ( 'top' , 'left: calc(50% + 20px);' )
144122} )
145123
146124test ( 'render position fixed ' , async ( ) => {
147125 const close = vi . fn ( )
148126 const click = vi . fn ( )
149- const { container , getByTestId } = render (
127+ const { getByTestId } = render (
150128 < div
151129 style = { {
152130 height : '200px' ,
@@ -166,7 +144,7 @@ test('render position fixed ', async () => {
166144 onClick = { click }
167145 onClose = { close }
168146 >
169- < Button data-testid = "a" type = "primary" shape = "square" >
147+ < Button data-testid = "a" type = "primary" >
170148 position: fixed
171149 </ Button >
172150 </ Popover >
@@ -186,11 +164,9 @@ test('render position fixed ', async () => {
186164
187165test ( 'should emit onchoose event when clicking the action' , async ( ) => {
188166 const choose = vi . fn ( )
189- const { container } = render (
167+ render (
190168 < Popover visible list = { itemList } onSelect = { choose } >
191- < Button type = "primary" shape = "square" >
192- 明朗风格
193- </ Button >
169+ < Button type = "primary" > 明朗风格</ Button >
194170 </ Popover >
195171 )
196172 const contentItem = document . querySelectorAll ( '.nut-popover-item' ) [ 0 ]
@@ -201,22 +177,21 @@ test('should emit onchoose event when clicking the action', async () => {
201177
202178test ( 'should not emit select event when the action is disabled' , async ( ) => {
203179 const choose = vi . fn ( )
204- const { container } = render (
180+ render (
205181 < Popover visible list = { itemListDisabled } onSelect = { choose } >
206- < Button type = "primary" shape = "square" >
207- 明朗风格
208- </ Button >
182+ < Button type = "primary" > 明朗风格</ Button >
209183 </ Popover >
210184 )
211185 const contentItem = document . querySelectorAll ( '.nut-popover-item' ) [ 0 ]
212186 fireEvent . click ( contentItem )
213187 await waitFor ( ( ) => expect ( choose ) . not . toBeCalled ( ) )
214188} )
215189
216- test ( 'target id' , async ( ) => {
217- const choose = vi . fn ( )
190+ test ( 'click event' , async ( ) => {
218191 const close = vi . fn ( )
219- const { container, getByTestId } = render (
192+ const close1 = vi . fn ( )
193+ const open = vi . fn ( )
194+ const { getByTestId, rerender } = render (
220195 < div >
221196 < Popover visible targetId = "popid" list = { itemListOne } />
222197 < Button type = "primary" id = "popid" >
@@ -228,4 +203,32 @@ test('target id', async () => {
228203 </ div >
229204 )
230205 fireEvent . click ( getByTestId ( 'closeid' ) )
206+ await waitFor ( ( ) =>
207+ expect ( document . querySelectorAll ( '.nut-popover' ) [ 0 ] ) . toHaveStyle ( {
208+ visibility : 'hidden' ,
209+ } )
210+ )
211+
212+ rerender (
213+ < >
214+ < Popover
215+ visible
216+ targetId = "popid"
217+ list = { itemListOne }
218+ location = "left-bottom"
219+ onOpen = { open }
220+ onClose = { close1 }
221+ data-testid = "popoverid"
222+ />
223+ < Button type = "primary" id = "popid" data-testid = "popid" >
224+ 自定义目标元素
225+ </ Button >
226+ </ >
227+ )
228+ fireEvent . click ( getByTestId ( 'popid' ) )
229+ await waitFor ( ( ) => {
230+ expect ( document . querySelectorAll ( '.nut-popover' ) [ 0 ] ) . toHaveStyle ( {
231+ visibility : 'hidden' ,
232+ } )
233+ } )
231234} )
0 commit comments