@@ -7,55 +7,83 @@ import { Provider } from 'react-redux';
7
7
import { modelReducer , formReducer , Field } from '../src' ;
8
8
9
9
describe ( '<Field parser={...} />' , ( ) => {
10
- const store = createStore ( combineReducers ( {
11
- test : modelReducer ( 'test' , { foo : '' } ) ,
12
- testForm : formReducer ( 'test' , { foo : '' } ) ,
13
- } ) ) ;
10
+ context ( 'standard usage' , ( ) => {
11
+ const store = createStore ( combineReducers ( {
12
+ test : modelReducer ( 'test' , { foo : '' } ) ,
13
+ testForm : formReducer ( 'test' , { foo : '' } ) ,
14
+ } ) ) ;
14
15
15
- const parseValue = val => ( {
16
- data : val ,
17
- } ) ;
16
+ const parseValue = val => ( {
17
+ data : val ,
18
+ } ) ;
19
+
20
+ const field = TestUtils . renderIntoDocument (
21
+ < Provider store = { store } >
22
+ < Field
23
+ model = "test.foo"
24
+ parser = { parseValue }
25
+ validators = { {
26
+ isParsed : ( val ) => val
27
+ && val . data
28
+ && val . data === 'parse test' ,
29
+ } }
30
+ >
31
+ < input type = "text" />
32
+ </ Field >
33
+ </ Provider >
34
+ ) ;
35
+
36
+ const input = TestUtils . findRenderedDOMComponentWithTag ( field , 'input' ) ;
37
+
38
+ it ( 'should parse the changed values given a parser function' , ( ) => {
39
+ const expected = { data : 'foo' } ;
40
+
41
+ input . value = 'foo' ;
42
+
43
+ TestUtils . Simulate . change ( input ) ;
18
44
19
- const field = TestUtils . renderIntoDocument (
20
- < Provider store = { store } >
21
- < Field
22
- model = "test.foo"
23
- parser = { parseValue }
24
- validators = { {
25
- isParsed : ( val ) => val
26
- && val . data
27
- && val . data === 'parse test' ,
28
- } }
29
- >
30
- < input type = "text" />
31
- </ Field >
32
- </ Provider >
33
- ) ;
34
-
35
- const input = TestUtils . findRenderedDOMComponentWithTag ( field , 'input' ) ;
36
-
37
- it ( 'should parse the changed values given a parser function' , ( ) => {
38
- const expected = { data : 'foo' } ;
39
-
40
- input . value = 'foo' ;
41
-
42
- TestUtils . Simulate . change ( input ) ;
43
-
44
- assert . deepEqual (
45
- store . getState ( ) . test . foo ,
46
- expected ) ;
45
+ assert . deepEqual (
46
+ store . getState ( ) . test . foo ,
47
+ expected ) ;
48
+ } ) ;
49
+
50
+ it ( 'should parse before validation' , ( ) => {
51
+ input . value = 'parse test' ;
52
+
53
+ assert . isFalse (
54
+ store . getState ( ) . testForm . fields . foo . validity . isParsed ,
55
+ 'should not be valid yet' ) ;
56
+
57
+ TestUtils . Simulate . change ( input ) ;
58
+
59
+ assert . isTrue (
60
+ store . getState ( ) . testForm . fields . foo . validity . isParsed ) ;
61
+ } ) ;
47
62
} ) ;
48
63
49
- it ( 'should parse before validation' , ( ) => {
50
- input . value = 'parse test' ;
64
+ it ( 'should parse the initial value immediately' , ( ) => {
65
+ const store = createStore ( combineReducers ( {
66
+ test : modelReducer ( 'test' , { foo : 'initial' } ) ,
67
+ testForm : formReducer ( 'test' , { foo : 'initial' } ) ,
68
+ } ) ) ;
69
+
70
+ const parseValue = val => val . toUpperCase ( ) ;
71
+
72
+ const field = TestUtils . renderIntoDocument (
73
+ < Provider store = { store } >
74
+ < Field
75
+ model = "test.foo"
76
+ parser = { parseValue }
77
+ >
78
+ < input type = "text" />
79
+ </ Field >
80
+ </ Provider >
81
+ ) ;
51
82
52
- assert . isFalse (
53
- store . getState ( ) . testForm . fields . foo . validity . isParsed ,
54
- 'should not be valid yet' ) ;
83
+ const input = TestUtils . findRenderedDOMComponentWithTag ( field , 'input' ) ;
55
84
56
- TestUtils . Simulate . change ( input ) ;
85
+ assert . equal ( input . value , 'INITIAL' ) ;
57
86
58
- assert . isTrue (
59
- store . getState ( ) . testForm . fields . foo . validity . isParsed ) ;
87
+ assert . equal ( store . getState ( ) . test . foo , 'INITIAL' ) ;
60
88
} ) ;
61
89
} ) ;
0 commit comments