1- import React from 'react' ;
2- import ContactForm from './ContactForm' ;
3- import { shallow , mount } from 'enzyme' ;
1+ import React from 'react'
2+ import ContactForm from './ContactForm'
3+ import { shallow , mount } from 'enzyme'
44
55const formFields = [ {
66 id : 'name' ,
@@ -11,58 +11,58 @@ const formFields = [{
1111} , {
1212 id : 'message' ,
1313 value : 'Hello world!'
14- } ] ;
14+ } ]
1515
1616const mockFormData = {
1717 name : 'John' ,
1818 email : 'someone@some123domain.com' ,
1919 message : 'Hello world!'
20- } ;
20+ }
2121
2222it ( 'should render without crashing' , ( ) => {
23- shallow ( < ContactForm /> ) ;
24- } ) ;
23+ shallow ( < ContactForm /> )
24+ } )
2525
2626it ( 'should contain all inputs' , ( ) => {
27- const component = shallow ( < ContactForm /> ) ;
28- expect ( component . find ( 'input' ) . length ) . toEqual ( 2 ) ;
29- expect ( component . find ( 'textArea' ) . length ) . toEqual ( 1 ) ;
30- expect ( component . find ( 'button' ) . length ) . toEqual ( 2 ) ;
31- } ) ;
27+ const component = shallow ( < ContactForm /> )
28+ expect ( component . find ( 'input' ) . length ) . toEqual ( 2 )
29+ expect ( component . find ( 'textArea' ) . length ) . toEqual ( 1 )
30+ expect ( component . find ( 'button' ) . length ) . toEqual ( 2 )
31+ } )
3232
3333describe ( 'all inputs should update on change' , ( ) => {
3434 formFields . forEach ( ( formField ) => {
3535 it ( `input "${ formField . id } " should update the state` , ( ) => {
36- const component = mount ( < ContactForm /> ) ;
36+ const component = mount ( < ContactForm /> )
3737 const mockChangeEvent = {
3838 target : { ...formField }
39- } ;
40- component . find ( `#${ formField . id } ` ) . at ( 0 ) . simulate ( "change" , mockChangeEvent ) ;
41- expect ( component . state ( ) [ formField . id ] ) . toEqual ( mockChangeEvent . target . value ) ;
42- } ) ;
43- } ) ;
44- } ) ;
39+ }
40+ component . find ( `#${ formField . id } ` ) . at ( 0 ) . simulate ( "change" , mockChangeEvent )
41+ expect ( component . state ( ) [ formField . id ] ) . toEqual ( mockChangeEvent . target . value )
42+ } )
43+ } )
44+ } )
4545
4646it ( 'form submit should call handleFormSubmit function' , ( ) => {
47- const component = mount ( < ContactForm /> ) ;
47+ const component = mount ( < ContactForm /> )
4848 const mockEvent = {
4949 preventDefault : jest . fn ( )
50- } ;
51- spyOn ( component . nodes [ 0 ] , 'handleFormSubmit' ) . and . callThrough ( ) ;
52- component . setState ( { ...mockFormData } ) ;
53- component . find ( 'form' ) . at ( 0 ) . simulate ( "submit" , mockEvent ) ;
54- expect ( component . nodes [ 0 ] . handleFormSubmit ) . toBeCalled ( ) ;
55- expect ( mockEvent . preventDefault ) . toBeCalled ( ) ;
56- } ) ;
50+ }
51+ spyOn ( component . nodes [ 0 ] , 'handleFormSubmit' ) . and . callThrough ( )
52+ component . setState ( { ...mockFormData } )
53+ component . find ( 'form' ) . at ( 0 ) . simulate ( "submit" , mockEvent )
54+ expect ( component . nodes [ 0 ] . handleFormSubmit ) . toBeCalled ( )
55+ expect ( mockEvent . preventDefault ) . toBeCalled ( )
56+ } )
5757
5858it ( 'reset button should clear the form' , ( ) => {
59- const component = mount ( < ContactForm /> ) ;
59+ const component = mount ( < ContactForm /> )
6060 const mockEvent = {
6161 preventDefault : jest . fn ( )
62- } ;
63- spyOn ( component . nodes [ 0 ] , 'resetForm' ) . and . callThrough ( ) ;
64- component . setState ( { ...mockFormData } ) ;
65- component . find ( 'button' ) . at ( 1 ) . simulate ( "click" , mockEvent ) ;
66- expect ( component . nodes [ 0 ] . resetForm ) . toBeCalled ( ) ;
67- expect ( component . state ( ) ) . not . toEqual ( mockFormData ) ;
68- } ) ;
62+ }
63+ spyOn ( component . nodes [ 0 ] , 'resetForm' ) . and . callThrough ( )
64+ component . setState ( { ...mockFormData } )
65+ component . find ( 'button' ) . at ( 1 ) . simulate ( "click" , mockEvent )
66+ expect ( component . nodes [ 0 ] . resetForm ) . toBeCalled ( )
67+ expect ( component . state ( ) ) . not . toEqual ( mockFormData )
68+ } )
0 commit comments