33 */
44
55import React from 'react' ;
6+ import { describe , it } from 'mocha' ;
67import { expect } from 'chai' ;
78import { shallow , render , mount } from 'enzyme' ;
89import './jsdom' ;
@@ -36,6 +37,7 @@ describe('Test to create a basic form', () => {
3637
3738
3839describe ( 'Test Form change validation' , ( ) => {
40+
3941 it ( 'The form is validated correctly' , ( ) => {
4042 const app = mount (
4143 < TextApp2
@@ -60,14 +62,82 @@ describe('Test Form change validation', () => {
6062 input . get ( 0 ) . value = 'example#example.com' ;
6163 input . simulate ( 'change' ) ;
6264 expect ( label . text ( ) ) . to . equal ( 'Please enter a valid email address.' ) ;
65+ expect ( app . node . validate ( ) ) . to . equal ( false ) ;
6366 expect ( app . node . isAllValid ) . to . equal ( false ) ;
6467 // Change
6568 input . get ( 0 ) . value = 'example@example.com' ;
6669 input . simulate ( 'change' ) ;
6770 expect ( label . text ( ) ) . to . be . empty ;
6871 expect ( input . props ( ) . className ) . to . contains ( 'valid-success' ) ;
72+ expect ( app . node . validate ( ) ) . to . equal ( true ) ;
6973 expect ( app . node . isAllValid ) . to . equal ( true ) ;
7074 } ) ;
7175
72- // it('Initialize the parameter by function', () => {});
76+ it ( 'API is executed correctly' , ( ) => {
77+ const app = mount (
78+ < TextApp2
79+ classNames = { {
80+ static : 'form-control' ,
81+ success : 'valid-success' ,
82+ error : 'valid-error' ,
83+ } }
84+ values = { { email : '' } }
85+ /> ,
86+ ) ;
87+ // init
88+ app . node . init ( { email2 : '' } ) ;
89+ expect ( app . node . formValues ) . to . have . property ( 'email2' ) ;
90+ expect ( app . node . validateByNames ( 'email2' ) ) . to . equal ( true ) ;
91+ // add fields
92+ app . node . addFields ( { email3 : { value : '123#123.com' } } ) ;
93+ expect ( app . node . formValues ) . to . have . property ( 'email3' ) ;
94+ expect ( app . node . validate ( ) ) . to . equal ( false ) ;
95+ expect ( app . node . validateByNames ( 'email3' ) ) . to . equal ( true ) ;
96+ // add schemas
97+ app . node . addSchemas ( { email4 : { rules : 'isEmail' } } ) ;
98+ app . node . addFields ( { email4 : { value : '123#123.com' } } ) ;
99+ expect ( app . node . validate ( ) ) . to . equal ( false ) ;
100+ expect ( app . node . validateByNames ( 'email4' ) ) . to . equal ( false ) ;
101+ // change values
102+ app . node . changeValues ( { email : '123@123.com' , email4 : '123@123.com' } ) ;
103+ expect ( app . node . validateByNames ( 'email' ) ) . to . equal ( true ) ;
104+ expect ( app . node . validate ( ) ) . to . equal ( true ) ;
105+ } ) ;
106+
107+ } ) ;
108+
109+
110+ describe ( 'Test all types of forms' , ( ) => {
111+
112+ it ( 'The all types of form is validated correctly' , ( ) => {
113+ const app = mount (
114+ < TextApp3
115+ classNames = { {
116+ static : 'form-control' ,
117+ success : 'valid-success' ,
118+ error : 'valid-error' ,
119+ } }
120+ values = { {
121+ hobby : [ '1' ] ,
122+ sex : '' ,
123+ city : '' ,
124+ remarks : '' ,
125+ } }
126+ /> ,
127+ ) ;
128+ app . find ( '#hobby2' ) . simulate ( 'change' ) ;
129+ expect ( app . node . formValues . hobby . indexOf ( '2' ) !== - 1 ) . to . equal ( true ) ;
130+ app . find ( '#hobby3' ) . simulate ( 'change' ) ;
131+ expect ( app . node . formValues . hobby . indexOf ( '3' ) !== - 1 ) . to . equal ( true ) ;
132+ app . find ( '#sex2' ) . simulate ( 'change' ) ;
133+ expect ( app . node . formValues . sex ) . to . equal ( '2' ) ;
134+ app . find ( 'select' ) . get ( '0' ) . value = '1' ;
135+ app . find ( 'select' ) . simulate ( 'change' ) ;
136+ expect ( app . node . formValues . city ) . to . equal ( '1' ) ;
137+ app . find ( '#remarks' ) . get ( '0' ) . value = 'abc' ;
138+ app . find ( '#remarks' ) . simulate ( 'change' ) ;
139+ expect ( app . node . formValues . remarks ) . to . equal ( 'abc' ) ;
140+ expect ( app . node . validate ( ) ) . to . equal ( true ) ;
141+ } ) ;
142+
73143} ) ;
0 commit comments