11import { mount } from 'enzyme' ;
2- import { render } from '@testing-library/react' ;
2+ import { fireEvent , render } from '@testing-library/react' ;
33import { resetWarned } from 'rc-util/lib/warning' ;
44import React from 'react' ;
55import type { FormInstance } from '../src' ;
@@ -218,15 +218,14 @@ describe('Form.Basic', () => {
218218
219219 it ( 'remove Field should trigger onMetaChange' , ( ) => {
220220 const onMetaChange = jest . fn ( ) ;
221- const wrapper = mount (
221+ const { unmount } = render (
222222 < Form >
223223 < Field name = "username" onMetaChange = { onMetaChange } >
224224 < Input />
225225 </ Field >
226226 </ Form > ,
227227 ) ;
228-
229- wrapper . unmount ( ) ;
228+ unmount ( ) ;
230229 expect ( onMetaChange ) . toHaveBeenCalledWith ( expect . objectContaining ( { destroy : true } ) ) ;
231230 } ) ;
232231 } ) ;
@@ -349,7 +348,7 @@ describe('Form.Basic', () => {
349348 const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
350349
351350 const form = React . createRef < FormInstance > ( ) ;
352- mount (
351+ render (
353352 < div >
354353 < Form ref = { form } />
355354 </ div > ,
@@ -497,7 +496,7 @@ describe('Form.Basic', () => {
497496 const triggerUpdate = jest . fn ( ) ;
498497 const formRef = React . createRef < FormInstance > ( ) ;
499498
500- const wrapper = mount (
499+ render (
501500 < div >
502501 < Form ref = { formRef } >
503502 < Field shouldUpdate = { ( prev , next ) => prev . value !== next . value } >
@@ -509,17 +508,17 @@ describe('Form.Basic', () => {
509508 </ Form >
510509 </ div > ,
511510 ) ;
512- wrapper . update ( ) ;
511+
513512 triggerUpdate . mockReset ( ) ;
514513
515514 // Not trigger render
516515 formRef . current . setFields ( [ { name : 'others' , value : 'no need to update' } ] ) ;
517- wrapper . update ( ) ;
516+
518517 expect ( triggerUpdate ) . not . toHaveBeenCalled ( ) ;
519518
520519 // Trigger render
521520 formRef . current . setFields ( [ { name : 'value' , value : 'should update' } ] ) ;
522- wrapper . update ( ) ;
521+
523522 expect ( triggerUpdate ) . toHaveBeenCalled ( ) ;
524523 } ) ;
525524 } ) ;
@@ -528,7 +527,7 @@ describe('Form.Basic', () => {
528527 let called1 = false ;
529528 let called2 = false ;
530529
531- mount (
530+ render (
532531 < Form >
533532 < Field name = "Light" >
534533 { ( _ , meta ) => {
@@ -602,7 +601,7 @@ describe('Form.Basic', () => {
602601 it ( 'warning if invalidate element' , ( ) => {
603602 resetWarned ( ) ;
604603 const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
605- mount (
604+ render (
606605 < div >
607606 < Form >
608607 { /* @ts -ignore */ }
@@ -624,14 +623,14 @@ describe('Form.Basic', () => {
624623 resetWarned ( ) ;
625624 const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
626625
627- const Test = ( ) => {
626+ const Test : React . FC = ( ) => {
628627 const [ form ] = useForm ( ) ;
629628 form . getFieldsValue ( ) ;
630629
631630 return < Form /> ;
632631 } ;
633632
634- mount ( < Test /> ) ;
633+ render ( < Test /> ) ;
635634
636635 jest . runAllTimers ( ) ;
637636 expect ( errorSpy ) . toHaveBeenCalledWith (
@@ -682,15 +681,15 @@ describe('Form.Basic', () => {
682681 } ;
683682 return < Input value = { value } onChange = { onInputChange } /> ;
684683 } ;
685- const wrapper = mount (
684+ const { container } = render (
686685 < Form >
687686 < Field name = "user" >
688687 < CustomInput />
689688 </ Field >
690689 </ Form > ,
691690 ) ;
692691 expect ( ( ) => {
693- wrapper . find ( 'Input' ) . simulate ( 'change' , { event : { target : { value : 'Light' } } } ) ;
692+ fireEvent . change ( container . querySelector ( 'input' ) , { target : { value : 'Light' } } ) ;
694693 } ) . not . toThrowError ( ) ;
695694 } ) ;
696695
@@ -739,11 +738,10 @@ describe('Form.Basic', () => {
739738 </ Form >
740739 ) ;
741740 } ;
742-
743- const wrapper = mount ( < Demo /> ) ;
744- expect ( wrapper . find ( 'input' ) . first ( ) . getDOMNode < HTMLInputElement > ( ) . value ) . toBe ( '11' ) ;
745- wrapper . find ( '.reset-btn' ) . first ( ) . simulate ( 'click' ) ;
746- expect ( wrapper . find ( 'input' ) . length ) . toBe ( 0 ) ;
741+ const { container } = render ( < Demo /> ) ;
742+ expect ( container . querySelector < HTMLInputElement > ( 'input' ) ?. value ) . toBe ( '11' ) ;
743+ fireEvent . click ( container . querySelector ( '.reset-btn' ) ) ;
744+ expect ( container . querySelectorAll < HTMLInputElement > ( 'input' ) . length ) . toBe ( 0 ) ;
747745 } ) ;
748746
749747 it ( 'setFieldsValue should work for multiple Select' , ( ) => {
@@ -767,8 +765,8 @@ describe('Form.Basic', () => {
767765 ) ;
768766 } ;
769767
770- const wrapper = mount ( < Demo /> ) ;
771- expect ( wrapper . find ( '.select-div' ) . text ( ) ) . toBe ( 'K1,K2' ) ;
768+ const { container } = render ( < Demo /> ) ;
769+ expect ( container . querySelector ( '.select-div' ) . textContent ) . toBe ( 'K1,K2' ) ;
772770 } ) ;
773771
774772 // https://github.com/ant-design/ant-design/issues/34768
@@ -793,16 +791,11 @@ describe('Form.Basic', () => {
793791 return node ;
794792 } ;
795793
796- const wrapper = mount ( < Demo /> ) ;
794+ const { container , rerender } = render ( < Demo /> ) ;
797795 refForm . setFieldsValue ( { name : 'bamboo' } ) ;
798- wrapper . update ( ) ;
799-
800- expect ( wrapper . find ( 'input' ) . prop ( 'value' ) ) . toEqual ( 'bamboo' ) ;
801-
802- wrapper . setProps ( { remount : true } ) ;
803- wrapper . update ( ) ;
804-
805- expect ( wrapper . find ( 'input' ) . prop ( 'value' ) ) . toEqual ( 'bamboo' ) ;
796+ expect ( container . querySelector < HTMLInputElement > ( 'input' ) . value ) . toBe ( 'bamboo' ) ;
797+ rerender ( < Demo remount /> ) ;
798+ expect ( container . querySelector < HTMLInputElement > ( 'input' ) . value ) . toBe ( 'bamboo' ) ;
806799 } ) ;
807800
808801 it ( 'setFieldValue' , ( ) => {
@@ -813,37 +806,29 @@ describe('Form.Basic', () => {
813806 < Form . List name = "list" >
814807 { fields =>
815808 fields . map ( field => (
816- < Field key = { field . key } { ... field } >
809+ < Field { ... field } key = { field . key } >
817810 < Input />
818811 </ Field >
819812 ) )
820813 }
821814 </ Form . List >
822-
823815 < Field name = { [ 'nest' , 'target' ] } initialValue = "nested" >
824816 < Input />
825817 </ Field >
826818 </ Form >
827819 ) ;
828820
829- const wrapper = mount ( < Demo /> ) ;
830- expect ( wrapper . find ( 'input' ) . map ( input => input . prop ( 'value' ) ) ) . toEqual ( [
831- 'bamboo' ,
832- 'little' ,
833- 'light' ,
834- 'nested' ,
835- ] ) ;
821+ const { container } = render ( < Demo /> ) ;
822+ expect (
823+ Array . from ( container . querySelectorAll < HTMLInputElement > ( 'input' ) ) . map ( input => input ?. value ) ,
824+ ) . toEqual ( [ 'bamboo' , 'little' , 'light' , 'nested' ] ) ;
836825
837826 // Set
838827 formRef . current . setFieldValue ( [ 'list' , 1 ] , 'tiny' ) ;
839828 formRef . current . setFieldValue ( [ 'nest' , 'target' ] , 'match' ) ;
840- wrapper . update ( ) ;
841829
842- expect ( wrapper . find ( 'input' ) . map ( input => input . prop ( 'value' ) ) ) . toEqual ( [
843- 'bamboo' ,
844- 'tiny' ,
845- 'light' ,
846- 'match' ,
847- ] ) ;
830+ expect (
831+ Array . from ( container . querySelectorAll < HTMLInputElement > ( 'input' ) ) . map ( input => input ?. value ) ,
832+ ) . toEqual ( [ 'bamboo' , 'tiny' , 'light' , 'match' ] ) ;
848833 } ) ;
849834} ) ;
0 commit comments