11import remove from './remove'
2+ import { getIn , setIn } from 'final-form'
23
34describe ( 'remove' , ( ) => {
45 it ( 'should call changeValue once' , ( ) => {
56 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+ }
726 const result = remove ( [ 'foo' , 0 ] , state , { changeValue } )
827 expect ( result ) . toBeUndefined ( )
928 expect ( changeValue ) . toHaveBeenCalled ( )
@@ -15,7 +34,15 @@ describe('remove', () => {
1534
1635 it ( 'should treat undefined like an empty array' , ( ) => {
1736 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 } )
1946 expect ( returnValue ) . toBeUndefined ( )
2047 const op = changeValue . mock . calls [ 0 ] [ 2 ]
2148 const result = op ( undefined )
@@ -25,14 +52,67 @@ describe('remove', () => {
2552
2653 it ( 'should remove value from the specified index, and return it' , ( ) => {
2754 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 } )
3391 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+ } )
37117 } )
38118} )
0 commit comments