11import * as React from 'react'
2- import { render , fireEvent } from '@testing-library/react'
2+ import { render , screen , fireEvent , waitFor } from '@testing-library/react'
33import '@testing-library/jest-dom'
44import { Popup } from '../popup'
55
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 ( )
109} )
1110
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 > )
1713
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 ( )
2716
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 ( )
3220} )
3321
3422test ( 'should not render overlay when overlay prop is false' , ( ) => {
@@ -91,6 +79,14 @@ test('pop description', () => {
9179 expect ( title ) . toHaveTextContent ( '副标题' )
9280} )
9381
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+
9490test ( 'should render close icon when using closeable prop' , ( ) => {
9591 const { container } = render ( < Popup visible closeable /> )
9692 const closeIcon = container . querySelector (
@@ -145,11 +141,15 @@ test('event click-title-right icon and keep overlay test ', () => {
145141 expect ( overlay2 ) . toBeNull ( )
146142} )
147143
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 ( ) => {
149145 const onOpen = vi . fn ( )
150146 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 ( ) )
153153} )
154154
155155test ( 'event click-overlay test' , async ( ) => {
@@ -171,3 +171,38 @@ test('pop destroyOnClose', () => {
171171 fireEvent . click ( overlay )
172172 expect ( onClose ) . toBeCalled ( )
173173} )
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