1
- import ContactFormComponent from '../../app/ContactFormComponent'
1
+ import ContactFormComponent , { renderTextInput } from '../../app/ContactFormComponent'
2
2
import React from 'react'
3
3
4
4
// See README for discussion of chai, enzyme, and sinon
@@ -13,17 +13,16 @@ chai.use(chaiEnzyme())
13
13
// really has nothing to do with Redux-Form at this point. We can pass in our
14
14
// own props (e.g. `submitting`) and make sure our form renders as we expect.
15
15
16
- describe ( "ContactFormComponent" , ( ) => {
16
+ describe . only ( "ContactFormComponent" , ( ) => {
17
17
let subject = null
18
- let submitting , touched , error , resetForm , onSave , onSaveResponse
18
+ let submitting , touched , error , reset , onSave , onSaveResponse
19
19
beforeEach ( ( ) => {
20
20
submitting = false
21
21
touched = false
22
22
error = null
23
- resetForm = sinon . spy ( )
23
+ reset = sinon . spy ( )
24
24
onSaveResponse = Promise . resolve ( )
25
- onSave = sinon . stub ( )
26
- onSave . returns ( onSaveResponse )
25
+ onSave = sinon . stub ( ) . returns ( onSaveResponse )
27
26
} )
28
27
const buildSubject = ( ) => {
29
28
const props = {
@@ -40,42 +39,52 @@ describe("ContactFormComponent", () => {
40
39
}
41
40
} ,
42
41
handleSubmit : fn => fn ,
43
- resetForm
42
+ reset
44
43
}
45
44
return shallow ( < ContactFormComponent { ...props } /> )
46
45
}
47
46
48
47
// Here we show we can test asychronous actions triggered by our form.
49
- it ( "calls resetForm after onSave" , ( done ) => {
48
+ it ( "calls reset after onSave" , ( ) => {
50
49
subject = buildSubject ( )
51
50
subject . find ( 'form' ) . simulate ( 'submit' )
52
51
expect ( onSave . callCount ) . to . equal ( 1 )
53
- onSaveResponse . then ( ( ) => {
54
- expect ( resetForm . callCount ) . to . equal ( 1 )
55
- done ( )
52
+ return onSaveResponse . then ( ( ) => {
53
+ expect ( reset . callCount ) . to . equal ( 1 )
56
54
} )
57
55
} )
58
56
59
57
// This is a very simle test, making sure that if we pass in a certain
60
58
// prop value, our form renders appropriately.
61
59
context ( "when submitting" , ( ) => {
62
- it ( "shows a spinner while submitting" , ( ) => {
60
+ it ( "shows a wait message while submitting" , ( ) => {
63
61
submitting = true
64
62
subject = buildSubject ( )
65
- const icon = subject . find ( 'button[type="submit"]' ) . find ( 'i' )
66
- expect ( icon ) . to . have . className ( 'glyphicon-refresh glyphicon-spin ')
63
+ const icon = subject . find ( 'button[type="submit"]' )
64
+ expect ( icon ) . to . have . text ( 'Submitting (takes 1 s) ')
67
65
} )
68
66
} )
69
67
70
- // Again, we show that what we render is based on the props we send it. If
71
- // we set the props to include an error state, our form should render that
72
- // fact.
68
+ } )
69
+
70
+ // renderTextInput is a stateless functional component, aka just a method that
71
+ // returns a React element, so it's trivial to test. We export it from the
72
+ // ContactFormComponent file. It could be named whatever we want, but it
73
+ // should be named based on what type of input component it creates, since you
74
+ // would create a similar function for each type of component you need to
75
+ // render. If you look in renderTextInput, it outputs an <input/>, but you
76
+ // would need a different method if you wanted to output a <select/>,
77
+ // <textarea>, etc.
78
+ describe . only ( 'renderTextInput' , ( ) => {
79
+ let subject
73
80
context ( "when in an error state" , ( ) => {
74
81
it ( "renders an error message for the input" , ( ) => {
75
- touched = true
76
- error = "Required"
77
- subject = buildSubject ( )
78
- const firstNameHelpBlock = subject . find ( '.help-block' )
82
+ const input = { name : 'firstName' , value : '' }
83
+ const label = 'First name'
84
+ const meta = { touched : true , error : 'Required' }
85
+ const element = renderTextInput ( { input, label, meta } )
86
+ subject = shallow ( element )
87
+ const firstNameHelpBlock = subject . find ( '.help-block' ) . first ( )
79
88
expect ( firstNameHelpBlock ) . to . exist
80
89
expect ( firstNameHelpBlock . text ( ) ) . to . equal ( 'Required' )
81
90
} )
0 commit comments