@@ -6,6 +6,7 @@ const data = () => ({
6
6
...original
7
7
} )
8
8
const node = ( ) => ( {
9
+ filter : jest . fn ( ) ,
9
10
children : [ ] ,
10
11
filters : [ ] ,
11
12
options : { } ,
@@ -35,54 +36,214 @@ describe('src/serialiser', () => {
35
36
} )
36
37
} )
37
38
38
- describe . skip ( 'yank' , ( ) => {
39
- it ( 'should apply filters by running filter method' , ( ) => {
39
+ describe ( 'yank' , ( ) => {
40
+ const setup = ( { options = { } , children = [ ] } = { } ) => {
40
41
const serialiser = new Serialiser ( data ( ) , ast ( ) )
41
- const value = data ( )
42
- const parent = 'test-parent'
43
- const curr = {
44
- ...node ( )
42
+ const value = 'test-value'
43
+ const set = 'test-set'
44
+ const child = {
45
+ value : true
46
+ }
47
+ const root = {
48
+ options,
49
+ children,
50
+ filter ( ) {
51
+ return this
52
+ } ,
53
+ key : {
54
+ path : [ ]
55
+ }
45
56
}
46
57
47
- serialiser . filter = jest
58
+ serialiser . get = jest
59
+ . fn ( )
60
+ . mockReturnValue ( value )
61
+ serialiser . set = jest
48
62
. fn ( )
49
- . mockReturnValue ( curr )
63
+ . mockReturnValue ( set )
64
+ serialiser . dig = jest
65
+ . fn ( )
66
+ . mockReturnValue ( child )
50
67
51
- serialiser . yank ( curr , value , parent )
68
+ return {
69
+ serialiser,
70
+ value,
71
+ child,
72
+ root
73
+ }
74
+ }
75
+
76
+ it ( 'should assign to parent if extract is truthy' , ( ) => {
77
+ const { serialiser, root } = setup ( {
78
+ options : {
79
+ extract : true
80
+ } ,
81
+ children : [
82
+ 'test-child'
83
+ ]
84
+ } )
85
+ const parent = { }
86
+
87
+ serialiser . yank ( root , data , parent )
52
88
53
- expect ( serialiser . filter ) . toBeCalledWith ( {
54
- data : value ,
55
- node : curr ,
56
- parent : { }
89
+ expect ( parent . value ) . toEqual ( true )
90
+ expect ( serialiser . set ) . not . toBeCalled ( )
91
+ } )
92
+
93
+ it ( 'should set all children to the parent' , ( ) => {
94
+ const { serialiser, root, child } = setup ( {
95
+ children : [
96
+ 'test-child'
97
+ ]
57
98
} )
99
+ const parent = { }
100
+
101
+ serialiser . yank ( root , data , parent )
102
+
103
+ const { length } = Object . keys ( parent )
104
+
105
+ expect ( serialiser . set ) . toBeCalledWith ( parent , root . key , child )
106
+ expect ( length ) . toEqual ( 0 )
58
107
} )
59
108
60
- it ( 'should descend through all children' , ( ) => {
61
- const serialiser = new Serialiser ( data ( ) , ast ( ) )
109
+ it ( 'should set value on the parent' , ( ) => {
110
+ const { serialiser, root, value } = setup ( )
111
+ const parent = { }
62
112
63
- serialiser . filter = jest . fn ( )
113
+ serialiser . yank ( root , data , parent )
64
114
65
- serialiser . yank ( )
115
+ expect ( serialiser . set ) . toBeCalledWith ( parent , root . key , value )
66
116
} )
67
117
} )
68
118
69
119
describe ( 'dig' , ( ) => {
120
+ it ( 'should return null if data is falsey and is nullable' , ( ) => {
121
+ const serialiser = new Serialiser ( data ( ) , ast ( ) )
122
+ const result = serialiser . dig ( undefined , [ ] )
70
123
71
- } )
124
+ expect ( result ) . toBeNull ( )
125
+ } )
126
+
127
+ it ( 'should return result' , ( ) => {
128
+ const serialiser = new Serialiser ( data ( ) , ast ( ) )
129
+ const value = 'test-value'
72
130
73
- describe ( 'filter' , ( ) => {
131
+ serialiser . yank = jest . fn ( )
132
+
133
+ const result = serialiser . dig ( value , [ ] )
134
+
135
+ expect ( result ) . toEqual ( { } )
136
+ } )
74
137
138
+ it ( 'should call yank for each child' , ( ) => {
139
+ const serialiser = new Serialiser ( data ( ) , ast ( ) )
140
+ const value = 'test-value'
141
+ const children = [
142
+ {
143
+ options : { }
144
+ } ,
145
+ {
146
+ options : { }
147
+ }
148
+ ]
149
+
150
+ serialiser . yank = jest . fn ( )
151
+ serialiser . dig ( value , children )
152
+
153
+ expect ( serialiser . yank ) . toBeCalledTimes ( children . length )
154
+ expect ( serialiser . yank ) . toBeCalledWith ( children [ 0 ] , value , { } )
155
+ } )
75
156
} )
76
157
77
158
describe ( 'serialise' , ( ) => {
159
+ it ( 'should call yank for each item in the AST' , ( ) => {
160
+ const value = 'test-value'
161
+ const ast = [
162
+ 'test-1' ,
163
+ 'test-2'
164
+ ]
165
+ const data = {
166
+ value
167
+ }
168
+ const serialiser = new Serialiser ( data , ast )
169
+
170
+ serialiser . yank = jest . fn ( )
171
+ serialiser . serialise ( )
78
172
173
+ expect ( serialiser . yank ) . toBeCalledTimes ( ast . length )
174
+ } )
175
+
176
+ it ( 'should return result' , ( ) => {
177
+ const serialiser = new Serialiser ( data ( ) , ast ( ) )
178
+ const value = 'test-result'
179
+
180
+ serialiser . result = value
181
+ serialiser . yank = jest . fn ( )
182
+
183
+ const result = serialiser . serialise ( )
184
+
185
+ expect ( result ) . toEqual ( value )
186
+ } )
79
187
} )
80
188
81
189
describe ( 'get' , ( ) => {
190
+ const path = [
191
+ 'nested' ,
192
+ 'data' ,
193
+ 'items' ,
194
+ 'one'
195
+ ]
196
+
197
+ it ( 'should retrieve a value from a given path' , ( ) => {
198
+ const source = data ( )
199
+ const serialiser = new Serialiser ( source , ast ( ) )
200
+ const result = serialiser . get ( source , path )
82
201
202
+ expect ( result ) . toEqual ( 'one' )
203
+ } )
204
+
205
+ it ( 'should run exec() if available in options' , ( ) => {
206
+ const source = data ( )
207
+ const serialiser = new Serialiser ( source , ast ( ) )
208
+ const value = 'test-value'
209
+ const options = {
210
+ exec : jest
211
+ . fn ( )
212
+ . mockReturnValue ( value )
213
+ }
214
+ const result = serialiser . get ( source , path , options )
215
+
216
+ expect ( result ) . toEqual ( value )
217
+ } )
218
+
219
+ it ( 'should return null if options.nullable is truthy and result is not' , ( ) => {
220
+ const source = data ( )
221
+ const serialiser = new Serialiser ( source , ast ( ) )
222
+ const path = [
223
+ 'foo' ,
224
+ 'bar'
225
+ ]
226
+ const options = {
227
+ nullable : true
228
+ }
229
+ const result = serialiser . get ( source , path , options )
230
+
231
+ expect ( result ) . toBeNull ( )
232
+ } )
83
233
} )
84
234
85
235
describe ( 'set' , ( ) => {
236
+ it ( 'should set the value at the given key location' , ( ) => {
237
+ const serialiser = new Serialiser ( data ( ) , ast ( ) )
238
+ const parent = { }
239
+ const key = {
240
+ name : 'foo'
241
+ }
242
+ const value = 'test-value'
243
+
244
+ serialiser . set ( parent , key , value )
86
245
246
+ expect ( parent . foo ) . toEqual ( value )
247
+ } )
87
248
} )
88
249
} )
0 commit comments