1
1
import * as React from 'react'
2
- import { render , fireEvent } from '@testing-library/react'
2
+ import { render , screen , fireEvent , waitFor } from '@testing-library/react'
3
3
import '@testing-library/jest-dom'
4
4
import { Popup } from '../popup'
5
5
6
- test ( 'should change z-index when using z-index prop' , ( ) => {
7
- const { container } = render ( < Popup visible zIndex = { 99 } /> )
8
- const element = container . querySelector ( '.nut-popup' ) as HTMLElement
9
- expect ( element . style . zIndex ) . toEqual ( '99' )
6
+ test ( 'renders without crashing' , ( ) => {
7
+ render ( < Popup visible > Test Content</ Popup > )
8
+ expect ( screen . getByText ( 'Test Content' ) ) . toBeInTheDocument ( )
10
9
} )
11
10
12
- test ( 'prop overlay-class test' , async ( ) => {
13
- const { container } = render ( < Popup visible overlayClassName = "testclas" /> )
14
- const overlay = container . querySelector ( '.nut-overlay' ) as HTMLElement
15
- expect ( overlay ) . toHaveClass ( 'testclas' )
16
- } )
11
+ test ( 'opens and closes correctly' , ( ) => {
12
+ const { rerender } = render ( < Popup visible = { false } > Test Content</ Popup > )
17
13
18
- test ( 'prop overlay-style test' , async ( ) => {
19
- const { container } = render (
20
- < Popup visible overlayStyle = { { color : 'red' } } />
21
- )
22
- const overlay = container . querySelector ( '.nut-overlay' ) as HTMLElement
23
- expect ( overlay ) . toHaveStyle ( {
24
- color : 'red' ,
25
- } )
26
- } )
14
+ // Initially, it should not be visible
15
+ expect ( screen . queryByText ( 'Test Content' ) ) . not . toBeInTheDocument ( )
27
16
28
- test ( 'should lock scroll when showed' , async ( ) => {
29
- const { rerender } = render ( < Popup visible = { false } /> )
30
- rerender ( < Popup visible /> )
31
- expect ( document . body . classList . contains ( 'nut-overflow-hidden' ) ) . toBe ( true )
17
+ // Rerender with visible true
18
+ rerender ( < Popup visible > Test Content</ Popup > )
19
+ expect ( screen . getByText ( 'Test Content' ) ) . toBeInTheDocument ( )
32
20
} )
33
21
34
22
test ( 'should not render overlay when overlay prop is false' , ( ) => {
@@ -91,6 +79,14 @@ test('pop description', () => {
91
79
expect ( title ) . toHaveTextContent ( '副标题' )
92
80
} )
93
81
82
+ test ( 'pop minHeight' , ( ) => {
83
+ const { container } = render (
84
+ < Popup minHeight = "30%" visible position = "bottom" />
85
+ )
86
+ const node = container . querySelector ( '.nut-popup' ) as HTMLElement
87
+ expect ( node ) . toHaveStyle ( { minHeight : '30%' } )
88
+ } )
89
+
94
90
test ( 'should render close icon when using closeable prop' , ( ) => {
95
91
const { container } = render ( < Popup visible closeable /> )
96
92
const closeIcon = container . querySelector (
@@ -145,11 +141,15 @@ test('event click-title-right icon and keep overlay test ', () => {
145
141
expect ( overlay2 ) . toBeNull ( )
146
142
} )
147
143
148
- test ( 'should emit open event when prop visible is set to true' , ( ) => {
144
+ test ( 'should emit open event when prop visible is set to true' , async ( ) => {
149
145
const onOpen = vi . fn ( )
150
146
const { rerender } = render ( < Popup visible = { false } onOpen = { onOpen } /> )
151
- rerender ( < Popup visible onOpen = { onOpen } /> )
152
- expect ( onOpen ) . toBeCalled ( )
147
+ rerender (
148
+ < Popup visible onOpen = { onOpen } closeOnOverlayClick >
149
+ test
150
+ </ Popup >
151
+ )
152
+ await waitFor ( ( ) => expect ( onOpen ) . toBeCalled ( ) )
153
153
} )
154
154
155
155
test ( 'event click-overlay test' , async ( ) => {
@@ -171,3 +171,38 @@ test('pop destroyOnClose', () => {
171
171
fireEvent . click ( overlay )
172
172
expect ( onClose ) . toBeCalled ( )
173
173
} )
174
+
175
+ test ( 'handles touch events correctly' , ( ) => {
176
+ const handleTouchStart = vi . fn ( )
177
+ const handleTouchMove = vi . fn ( )
178
+ const handleTouchEnd = vi . fn ( )
179
+
180
+ render (
181
+ < Popup
182
+ visible
183
+ resizable
184
+ position = "bottom"
185
+ // minHeight="400px"
186
+ onTouchStart = { handleTouchStart }
187
+ onTouchMove = { handleTouchMove }
188
+ onTouchEnd = { handleTouchEnd }
189
+ >
190
+ Test Content
191
+ </ Popup >
192
+ )
193
+
194
+ const popup = document . body . querySelector ( '.nut-popup' ) as HTMLElement
195
+
196
+ // Simulate touch events
197
+ fireEvent . touchStart ( popup , { touches : [ { pageY : 400 } ] } )
198
+ expect ( handleTouchStart ) . toHaveBeenCalled ( )
199
+
200
+ fireEvent . touchMove ( popup , { touches : [ { pageY : 50 } ] } )
201
+ expect ( handleTouchMove ) . toHaveBeenCalled ( )
202
+
203
+ fireEvent . touchMove ( popup , { touches : [ { pageY : 450 } ] } )
204
+ expect ( handleTouchMove ) . toHaveBeenCalled ( )
205
+
206
+ fireEvent . touchEnd ( popup )
207
+ expect ( handleTouchEnd ) . toHaveBeenCalled ( )
208
+ } )
0 commit comments