@@ -37,30 +37,53 @@ const form = {
37
37
38
38
describe ( "form util functions" , ( ) => {
39
39
describe ( "getFormHelpers" , ( ) => {
40
- const untouchedGoodResult = getFormHelpers < TestType > ( form , "untouchedGoodField" )
41
- const untouchedBadResult = getFormHelpers < TestType > ( form , "untouchedBadField" )
42
- const touchedGoodResult = getFormHelpers < TestType > ( form , "touchedGoodField" )
43
- const touchedBadResult = getFormHelpers < TestType > ( form , "touchedBadField" )
44
- it ( "populates the 'field props'" , ( ) => {
45
- expect ( untouchedGoodResult . name ) . toEqual ( "untouchedGoodField" )
46
- expect ( untouchedGoodResult . onBlur ) . toBeDefined ( )
47
- expect ( untouchedGoodResult . onChange ) . toBeDefined ( )
48
- expect ( untouchedGoodResult . value ) . toBeDefined ( )
40
+ describe ( "without API errors" , ( ) => {
41
+ const getFieldHelpers = getFormHelpers < TestType > ( form )
42
+ const untouchedGoodResult = getFieldHelpers ( "untouchedGoodField" )
43
+ const untouchedBadResult = getFieldHelpers ( "untouchedBadField" )
44
+ const touchedGoodResult = getFieldHelpers ( "touchedGoodField" )
45
+ const touchedBadResult = getFieldHelpers ( "touchedBadField" )
46
+ it ( "populates the 'field props'" , ( ) => {
47
+ expect ( untouchedGoodResult . name ) . toEqual ( "untouchedGoodField" )
48
+ expect ( untouchedGoodResult . onBlur ) . toBeDefined ( )
49
+ expect ( untouchedGoodResult . onChange ) . toBeDefined ( )
50
+ expect ( untouchedGoodResult . value ) . toBeDefined ( )
51
+ } )
52
+ it ( "sets the id to the name" , ( ) => {
53
+ expect ( untouchedGoodResult . id ) . toEqual ( "untouchedGoodField" )
54
+ } )
55
+ it ( "sets error to true if touched and invalid" , ( ) => {
56
+ expect ( untouchedGoodResult . error ) . toBeFalsy
57
+ expect ( untouchedBadResult . error ) . toBeFalsy
58
+ expect ( touchedGoodResult . error ) . toBeFalsy
59
+ expect ( touchedBadResult . error ) . toBeTruthy
60
+ } )
61
+ it ( "sets helperText to the error message if touched and invalid" , ( ) => {
62
+ expect ( untouchedGoodResult . helperText ) . toBeUndefined
63
+ expect ( untouchedBadResult . helperText ) . toBeUndefined
64
+ expect ( touchedGoodResult . helperText ) . toBeUndefined
65
+ expect ( touchedBadResult . helperText ) . toEqual ( "oops!" )
66
+ } )
49
67
} )
50
- it ( "sets the id to the name" , ( ) => {
51
- expect ( untouchedGoodResult . id ) . toEqual ( "untouchedGoodField" )
52
- } )
53
- it ( "sets error to true if touched and invalid" , ( ) => {
54
- expect ( untouchedGoodResult . error ) . toBeFalsy
55
- expect ( untouchedBadResult . error ) . toBeFalsy
56
- expect ( touchedGoodResult . error ) . toBeFalsy
57
- expect ( touchedBadResult . error ) . toBeTruthy
58
- } )
59
- it ( "sets helperText to the error message if touched and invalid" , ( ) => {
60
- expect ( untouchedGoodResult . helperText ) . toBeUndefined
61
- expect ( untouchedBadResult . helperText ) . toBeUndefined
62
- expect ( touchedGoodResult . helperText ) . toBeUndefined
63
- expect ( touchedBadResult . helperText ) . toEqual ( "oops!" )
68
+ describe ( "with API errors" , ( ) => {
69
+ it ( "shows an error if there is only an API error" , ( ) => {
70
+ const getFieldHelpers = getFormHelpers < TestType > ( form , { touchedGoodField : "API error!" } )
71
+ const result = getFieldHelpers ( "touchedGoodField" )
72
+ expect ( result . error ) . toBeTruthy
73
+ expect ( result . helperText ) . toEqual ( "API error!" )
74
+ } )
75
+ it ( "shows an error if there is only a validation error" , ( ) => {
76
+ const getFieldHelpers = getFormHelpers < TestType > ( form , { } )
77
+ const result = getFieldHelpers ( "touchedBadField" )
78
+ expect ( result . error ) . toBeTruthy
79
+ expect ( result . helperText ) . toEqual ( "oops!" )
80
+ } )
81
+ it ( "shows the API error if both are present" , ( ) => {
82
+ const getFieldHelpers = getFormHelpers < TestType > ( form , { touchedBadField : "API error!" } )
83
+ const result = getFieldHelpers ( "touchedBadField" )
84
+ expect ( result . error ) . toBeTruthy
85
+ expect ( result . helperText ) . toEqual ( "API error!" )
86
+ } )
64
87
} )
65
88
} )
66
89
0 commit comments