1
1
import remove from './remove'
2
+ import { getIn , setIn } from 'final-form'
2
3
3
4
describe ( 'remove' , ( ) => {
4
5
it ( 'should call changeValue once' , ( ) => {
5
6
const changeValue = jest . fn ( )
6
- const state = { }
7
+ const state = {
8
+ formState : {
9
+ values : {
10
+ foo : [ 'one' , 'two' ]
11
+ }
12
+ } ,
13
+ fields : {
14
+ 'foo[0]' : {
15
+ name : 'foo[0]' ,
16
+ touched : true ,
17
+ error : 'First Error'
18
+ } ,
19
+ 'foo[1]' : {
20
+ name : 'foo[1]' ,
21
+ touched : false ,
22
+ error : 'Second Error'
23
+ }
24
+ }
25
+ }
7
26
const result = remove ( [ 'foo' , 0 ] , state , { changeValue } )
8
27
expect ( result ) . toBeUndefined ( )
9
28
expect ( changeValue ) . toHaveBeenCalled ( )
@@ -15,7 +34,15 @@ describe('remove', () => {
15
34
16
35
it ( 'should treat undefined like an empty array' , ( ) => {
17
36
const changeValue = jest . fn ( )
18
- const returnValue = remove ( [ 'foo' , 1 ] , { } , { changeValue } )
37
+ const state = {
38
+ formState : {
39
+ values : {
40
+ foo : undefined
41
+ }
42
+ } ,
43
+ fields : { }
44
+ }
45
+ const returnValue = remove ( [ 'foo' , 1 ] , state , { changeValue } )
19
46
expect ( returnValue ) . toBeUndefined ( )
20
47
const op = changeValue . mock . calls [ 0 ] [ 2 ]
21
48
const result = op ( undefined )
@@ -25,14 +52,67 @@ describe('remove', () => {
25
52
26
53
it ( 'should remove value from the specified index, and return it' , ( ) => {
27
54
const array = [ 'a' , 'b' , 'c' , 'd' ]
28
- let result
29
- const changeValue = jest . fn ( ( args , state , op ) => {
30
- result = op ( array )
31
- } )
32
- const returnValue = remove ( [ 'foo' , 1 ] , { } , { changeValue } )
55
+ // implementation of changeValue taken directly from Final Form
56
+ const changeValue = ( state , name , mutate ) => {
57
+ const before = getIn ( state . formState . values , name )
58
+ const after = mutate ( before )
59
+ state . formState . values = setIn ( state . formState . values , name , after ) || { }
60
+ }
61
+ const state = {
62
+ formState : {
63
+ values : {
64
+ foo : array
65
+ }
66
+ } ,
67
+ fields : {
68
+ 'foo[0]' : {
69
+ name : 'foo[0]' ,
70
+ touched : true ,
71
+ error : 'A Error'
72
+ } ,
73
+ 'foo[1]' : {
74
+ name : 'foo[1]' ,
75
+ touched : false ,
76
+ error : 'B Error'
77
+ } ,
78
+ 'foo[2]' : {
79
+ name : 'foo[2]' ,
80
+ touched : true ,
81
+ error : 'C Error'
82
+ } ,
83
+ 'foo[3]' : {
84
+ name : 'foo[3]' ,
85
+ touched : false ,
86
+ error : 'D Error'
87
+ }
88
+ }
89
+ }
90
+ const returnValue = remove ( [ 'foo' , 1 ] , state , { changeValue } )
33
91
expect ( returnValue ) . toBe ( 'b' )
34
- expect ( result ) . not . toBe ( array ) // copied
35
- expect ( Array . isArray ( result ) ) . toBe ( true )
36
- expect ( result ) . toEqual ( [ 'a' , 'c' , 'd' ] )
92
+ expect ( state . formState . values . foo ) . not . toBe ( array ) // copied
93
+ expect ( state ) . toEqual ( {
94
+ formState : {
95
+ values : {
96
+ foo : [ 'a' , 'c' , 'd' ]
97
+ }
98
+ } ,
99
+ fields : {
100
+ 'foo[0]' : {
101
+ name : 'foo[0]' ,
102
+ touched : true ,
103
+ error : 'A Error'
104
+ } ,
105
+ 'foo[1]' : {
106
+ name : 'foo[1]' ,
107
+ touched : true ,
108
+ error : 'C Error'
109
+ } ,
110
+ 'foo[2]' : {
111
+ name : 'foo[2]' ,
112
+ touched : false ,
113
+ error : 'D Error'
114
+ }
115
+ }
116
+ } )
37
117
} )
38
118
} )
0 commit comments