@ -1,5 +1,4 @@
import React from 'react' ;
import { mount } from 'enzyme' ;
import { act } from 'react-dom/test-utils' ;
import { render , fireEvent } from '@testing-library/react' ;
import Form from '..' ;
@ -9,16 +8,15 @@ import { sleep } from '../../../tests/utils';
describe ( 'Form.List' , ( ) => {
async function change ( wrapper , index , value ) {
wrapper . find ( Input ) . at ( index ) . simulate ( 'change' , { target : { value } } ) ;
fireEvent . change ( wrapper . getElementsByClassName ( 'ant-input' ) [ index ] , { target : { value } } ) ;
await sleep ( ) ;
wrapper . update ( ) ;
}
function testList ( name , renderField ) {
it ( name , async ( ) => {
jest . useFakeTimers ( ) ;
const wrapper = mount (
const { container } = render (
< Form >
< Form . List name = "list" >
{ ( fields , { add , remove } ) => (
@ -47,37 +45,35 @@ describe('Form.List', () => {
function operate ( className ) {
act ( ( ) => {
wrapper . find ( className ) . last ( ) . simulate ( 'click' ) ;
fireEvent . click ( container . querySelector ( className ) ) ;
jest . runAllTimers ( ) ;
} ) ;
wrapper . update ( ) ;
}
operate ( '.add' ) ;
expect ( wrapper . find ( Input ) . length ) . toBe ( 1 ) ;
expect ( container . getElementsByClassName ( 'ant-input' ) . length ) . toBe ( 1 ) ;
operate ( '.add' ) ;
expect ( wrapper . find ( Input ) . length ) . toBe ( 2 ) ;
expect ( container . getElementsByClassName ( 'ant-input' ) . length ) . toBe ( 2 ) ;
operate ( '.add' ) ;
expect ( wrapper . find ( Input ) . length ) . toBe ( 3 ) ;
expect ( container . getElementsByClassName ( 'ant-input' ) . length ) . toBe ( 3 ) ;
await change ( wrapp er, 2 , '' ) ;
await change ( contain er, 2 , '' ) ;
for ( let i = 0 ; i < 10 ; i += 1 ) {
act ( ( ) => {
jest . runAllTimers ( ) ;
} ) ;
}
wrapper . update ( ) ;
expect ( wrapper . find ( '.ant-form-item-explain div' ) . length ) . toBe ( 1 ) ;
expect ( container . getElementsByClassName ( 'ant-form-item-explain' ) . length ) . toBe ( 1 ) ;
operate ( '.remove-0' ) ;
expect ( wrapper . find ( Input ) . length ) . toBe ( 2 ) ;
expect ( wrapper . find ( '. ant-form-item-explain div ' ) . length ) . toBe ( 1 ) ;
expect ( container . getElementsByClassName ( 'ant-input' ) . length ) . toBe ( 2 ) ;
expect ( container . getElementsByClassName ( 'ant-form-item-explain' ) . length ) . toBe ( 1 ) ;
operate ( '.remove-1' ) ;
expect ( wrapper . find ( Input ) . length ) . toBe ( 1 ) ;
expect ( wrapper . find ( '. ant-form-item-explain div ' ) . length ) . toBe ( 0 ) ;
expect ( container . getElementsByClassName ( 'ant-input' ) . length ) . toBe ( 1 ) ;
expect ( container . getElementsByClassName ( 'ant-form-item-explain' ) . length ) . toBe ( 0 ) ;
jest . useRealTimers ( ) ;
} ) ;
@ -99,14 +95,12 @@ describe('Form.List', () => {
it ( 'correct onFinish values' , async ( ) => {
async function click ( wrapper , className ) {
wrapper . find ( className ) . last ( ) . simulate ( 'click' ) ;
await sleep ( ) ;
wrapper . update ( ) ;
fireEvent . click ( wrapper . querySelector ( className ) ) ;
}
const onFinish = jest . fn ( ) . mockImplementation ( ( ) => { } ) ;
const wrapper = mount (
const { container } = render (
< Form
onFinish = { v => {
if ( typeof v . list [ 0 ] === 'object' ) {
@ -139,22 +133,22 @@ describe('Form.List', () => {
< / F o r m > ,
) ;
await click ( wrapp er, '.add' ) ;
await change ( wrapp er, 0 , 'input1' ) ;
wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
await click ( contain er, '.add' ) ;
await change ( contain er, 0 , 'input1' ) ;
fireEvent . submit ( container . querySelector ( 'form' ) ) ;
await sleep ( ) ;
expect ( onFinish ) . toHaveBeenLastCalledWith ( { list : [ 'input1' ] } ) ;
await click ( wrapp er, '.add' ) ;
await change ( wrapp er, 1 , 'input2' ) ;
await click ( wrapp er, '.add' ) ;
await change ( wrapp er, 2 , 'input3' ) ;
wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
await click ( contain er, '.add' ) ;
await change ( contain er, 1 , 'input2' ) ;
await click ( contain er, '.add' ) ;
await change ( contain er, 2 , 'input3' ) ;
fireEvent . submit ( container . querySelector ( 'form' ) ) ;
await sleep ( ) ;
expect ( onFinish ) . toHaveBeenLastCalledWith ( { list : [ 'input1' , 'input2' , 'input3' ] } ) ;
await click ( wrapp er, '.remove' ) ; // will remove first input
wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
await click ( contain er, '.remove' ) ; // will remove first input
fireEvent . submit ( container . querySelector ( 'form' ) ) ;
await sleep ( ) ;
expect ( onFinish ) . toHaveBeenLastCalledWith ( { list : [ 'input2' , 'input3' ] } ) ;
} ) ;
@ -163,7 +157,7 @@ describe('Form.List', () => {
jest . useFakeTimers ( ) ;
let operation ;
const wrapper = mount (
const { container } = render (
< Form >
< Form . List
name = "list"
@ -190,22 +184,21 @@ describe('Form.List', () => {
operation . add ( ) ;
await sleep ( 100 ) ;
jest . runAllTimers ( ) ;
wrapper . update ( ) ;
} ) ;
}
await addItem ( ) ;
expect ( wrapper . find ( '.ant-form-item-explain div' ) . text ( ) ) . toEqual ( 'At least 2' ) ;
expect ( container . querySelector ( '.ant-form-item-explain div' ) . innerHTML ) . toEqual ( 'At least 2' ) ;
await addItem ( ) ;
expect ( wrapper . find ( '. ant-form-item-explain div' ) ) . toHaveLength ( 0 ) ;
expect ( container . getElementsByClassName ( 'ant-form-item-explain div' ) ) . toHaveLength ( 0 ) ;
jest . useRealTimers ( ) ;
} ) ;
it ( 'should render empty without errors' , ( ) => {
const wrapper = mount ( < Form . ErrorList / > ) ;
expect ( wrapper . render ( ) ) . toMatchSnapshot ( ) ;
const { container } = render ( < Form . ErrorList / > ) ;
expect ( container . firstChild ) . toMatchSnapshot ( ) ;
} ) ;
it ( 'no warning when reset in validate' , async ( ) => {