Total Complexity | 4 |
Complexity/F | 1 |
Lines of Code | 387 |
Function Count | 4 |
Duplicated Lines | 376 |
Ratio | 97.16 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | /* eslint-enable describe it sinon */ |
||
3 | import expect from 'expect'; |
||
4 | import { |
||
5 | treeToFlatList |
||
6 | } from './../../src/util/treeToFlatList'; |
||
7 | |||
8 | describe('the treeFlatList utility', () => { |
||
9 | |||
10 | it('Should turn a tree into a flat list with no identifier', () => { |
||
11 | |||
12 | const data = { |
||
13 | root: { |
||
14 | id: -1, |
||
15 | children: [ |
||
16 | { |
||
17 | id: 1, |
||
18 | parentId: -1, |
||
19 | children: [ |
||
20 | { |
||
21 | id: 11, |
||
22 | parentId: 1 |
||
23 | }, |
||
24 | { |
||
25 | id: 12, |
||
26 | parentId: 1, |
||
27 | children: [ |
||
28 | { |
||
29 | id: 121, |
||
30 | parentId: 12, |
||
31 | children: [ |
||
32 | { |
||
33 | id: 1211, |
||
34 | parentId: 121 |
||
35 | } |
||
36 | ] |
||
37 | } |
||
38 | ] |
||
39 | } |
||
40 | ] |
||
41 | }, |
||
42 | { |
||
43 | id: 2, |
||
44 | parentId: -1, |
||
45 | children: [ |
||
46 | { |
||
47 | id: 21, |
||
48 | parentId: 2 |
||
49 | } |
||
50 | ] |
||
51 | } |
||
52 | ] |
||
53 | } |
||
54 | }; |
||
55 | |||
56 | expect( |
||
57 | treeToFlatList(data) |
||
58 | ).toEqual([ |
||
59 | { |
||
60 | _id: -1, |
||
61 | _parentId: 'root', |
||
62 | _depth: 0, |
||
63 | _leaf: false, |
||
64 | _hideChildren: undefined, |
||
65 | _hasChildren: true, |
||
66 | _isExpanded: true |
||
67 | }, |
||
68 | { |
||
69 | _id: 1, |
||
70 | _parentId: -1, |
||
71 | _depth: 1, |
||
72 | _leaf: false, |
||
73 | _hideChildren: undefined, |
||
74 | _hasChildren: true, |
||
75 | _isExpanded: true |
||
76 | }, |
||
77 | { |
||
78 | _id: 11, |
||
79 | _parentId: 1, |
||
80 | _depth: 2, |
||
81 | _leaf: true, |
||
82 | _hideChildren: undefined, |
||
83 | _hasChildren: undefined, |
||
84 | _isExpanded: undefined |
||
85 | }, |
||
86 | { |
||
87 | _id: 12, |
||
88 | _parentId: 1, |
||
89 | _depth: 2, |
||
90 | _leaf: false, |
||
91 | _hideChildren: undefined, |
||
92 | _hasChildren: true, |
||
93 | _isExpanded: true |
||
94 | }, |
||
95 | { |
||
96 | _id: 121, |
||
97 | _parentId: 12, |
||
98 | _depth: 3, |
||
99 | _leaf: false, |
||
100 | _hideChildren: undefined, |
||
101 | _hasChildren: true, |
||
102 | _isExpanded: true |
||
103 | }, |
||
104 | { |
||
105 | _id: 1211, |
||
106 | _parentId: 121, |
||
107 | _depth: 4, |
||
108 | _leaf: true, |
||
109 | _hideChildren: undefined, |
||
110 | _hasChildren: undefined, |
||
111 | _isExpanded: undefined |
||
112 | }, |
||
113 | { |
||
114 | _id: 2, |
||
115 | _parentId: -1, |
||
116 | _depth: 1, |
||
117 | _leaf: false, |
||
118 | _hideChildren: undefined, |
||
119 | _hasChildren: true, |
||
120 | _isExpanded: true |
||
121 | }, |
||
122 | { |
||
123 | _id: 21, |
||
124 | _parentId: 2, |
||
125 | _depth: 2, |
||
126 | _leaf: true, |
||
127 | _hideChildren: undefined, |
||
128 | _hasChildren: undefined, |
||
129 | _isExpanded: undefined |
||
130 | } |
||
131 | ]); |
||
132 | |||
133 | }); |
||
134 | |||
135 | it('Should turn a tree into a flat list with identifiers', () => { |
||
136 | |||
137 | const data = { |
||
138 | topLevel: { |
||
139 | id: -1, |
||
140 | childz: [ |
||
141 | { |
||
142 | id: 1, |
||
143 | parentId: -1, |
||
144 | childz: [ |
||
145 | { |
||
146 | id: 11, |
||
147 | parentId: 1 |
||
148 | }, |
||
149 | { |
||
150 | id: 12, |
||
151 | parentId: 1, |
||
152 | childz: [ |
||
153 | { |
||
154 | id: 121, |
||
155 | parentId: 12, |
||
156 | childz: [ |
||
157 | { |
||
158 | id: 1211, |
||
159 | parentId: 121 |
||
160 | } |
||
161 | ] |
||
162 | } |
||
163 | ] |
||
164 | } |
||
165 | ] |
||
166 | }, |
||
167 | { |
||
168 | id: 2, |
||
169 | parentId: -1, |
||
170 | childz: [ |
||
171 | { |
||
172 | id: 21, |
||
173 | parentId: 2 |
||
174 | } |
||
175 | ] |
||
176 | } |
||
177 | ] |
||
178 | } |
||
179 | }; |
||
180 | |||
181 | expect( |
||
182 | treeToFlatList(data, 'topLevel', 'childz') |
||
183 | ).toEqual([ |
||
184 | { |
||
185 | _id: -1, |
||
186 | _parentId: 'root', |
||
187 | _depth: 0, |
||
188 | _leaf: false, |
||
189 | _hideChildren: undefined, |
||
190 | _hasChildren: true, |
||
191 | _isExpanded: true |
||
192 | }, |
||
193 | { |
||
194 | _id: 1, |
||
195 | _parentId: -1, |
||
196 | _depth: 1, |
||
197 | _leaf: false, |
||
198 | _hideChildren: undefined, |
||
199 | _hasChildren: true, |
||
200 | _isExpanded: true |
||
201 | }, |
||
202 | { |
||
203 | _id: 11, |
||
204 | _parentId: 1, |
||
205 | _depth: 2, |
||
206 | _leaf: true, |
||
207 | _hideChildren: undefined, |
||
208 | _hasChildren: undefined, |
||
209 | _isExpanded: undefined |
||
210 | }, |
||
211 | { |
||
212 | _id: 12, |
||
213 | _parentId: 1, |
||
214 | _depth: 2, |
||
215 | _leaf: false, |
||
216 | _hideChildren: undefined, |
||
217 | _hasChildren: true, |
||
218 | _isExpanded: true |
||
219 | }, |
||
220 | { |
||
221 | _id: 121, |
||
222 | _parentId: 12, |
||
223 | _depth: 3, |
||
224 | _leaf: false, |
||
225 | _hideChildren: undefined, |
||
226 | _hasChildren: true, |
||
227 | _isExpanded: true |
||
228 | }, |
||
229 | { |
||
230 | _id: 1211, |
||
231 | _parentId: 121, |
||
232 | _depth: 4, |
||
233 | _leaf: true, |
||
234 | _hideChildren: undefined, |
||
235 | _hasChildren: undefined, |
||
236 | _isExpanded: undefined |
||
237 | }, |
||
238 | { |
||
239 | _id: 2, |
||
240 | _parentId: -1, |
||
241 | _depth: 1, |
||
242 | _leaf: false, |
||
243 | _hideChildren: undefined, |
||
244 | _hasChildren: true, |
||
245 | _isExpanded: true |
||
246 | }, |
||
247 | { |
||
248 | _id: 21, |
||
249 | _parentId: 2, |
||
250 | _depth: 2, |
||
251 | _leaf: true, |
||
252 | _hideChildren: undefined, |
||
253 | _hasChildren: undefined, |
||
254 | _isExpanded: undefined |
||
255 | } |
||
256 | ]); |
||
257 | |||
258 | }); |
||
259 | |||
260 | it('Should pass data along with constructed nodes', () => { |
||
261 | |||
262 | const data = { |
||
263 | topLevel: { |
||
264 | id: -1, |
||
265 | childz: [ |
||
266 | { |
||
267 | id: 1, |
||
268 | specialData: 'someProp', |
||
269 | parentId: -1, |
||
270 | childz: [ |
||
271 | { |
||
272 | id: 11, |
||
273 | parentId: 1 |
||
274 | }, |
||
275 | { |
||
276 | id: 12, |
||
277 | parentId: 1, |
||
278 | childz: [ |
||
279 | { |
||
280 | id: 121, |
||
281 | parentId: 12, |
||
282 | childz: [ |
||
283 | { |
||
284 | id: 1211, |
||
285 | parentId: 121 |
||
286 | } |
||
287 | ] |
||
288 | } |
||
289 | ] |
||
290 | } |
||
291 | ] |
||
292 | }, |
||
293 | { |
||
294 | id: 2, |
||
295 | parentId: -1, |
||
296 | code: 'banana', |
||
297 | childz: [ |
||
298 | { |
||
299 | id: 21, |
||
300 | parentId: 2 |
||
301 | } |
||
302 | ] |
||
303 | } |
||
304 | ] |
||
305 | } |
||
306 | }; |
||
307 | |||
308 | expect( |
||
309 | treeToFlatList(data, 'topLevel', 'childz') |
||
310 | ).toEqual([ |
||
311 | { |
||
312 | _id: -1, |
||
313 | _parentId: 'root', |
||
314 | _depth: 0, |
||
315 | _leaf: false, |
||
316 | _hideChildren: undefined, |
||
317 | _hasChildren: true, |
||
318 | _isExpanded: true |
||
319 | }, |
||
320 | { |
||
321 | _id: 1, |
||
322 | specialData: 'someProp', |
||
323 | _parentId: -1, |
||
324 | _depth: 1, |
||
325 | _leaf: false, |
||
326 | _hideChildren: undefined, |
||
327 | _hasChildren: true, |
||
328 | _isExpanded: true |
||
329 | }, |
||
330 | { |
||
331 | _id: 11, |
||
332 | _parentId: 1, |
||
333 | _depth: 2, |
||
334 | _leaf: true, |
||
335 | _hideChildren: undefined, |
||
336 | _hasChildren: undefined, |
||
337 | _isExpanded: undefined |
||
338 | }, |
||
339 | { |
||
340 | _id: 12, |
||
341 | _parentId: 1, |
||
342 | _depth: 2, |
||
343 | _leaf: false, |
||
344 | _hideChildren: undefined, |
||
345 | _hasChildren: true, |
||
346 | _isExpanded: true |
||
347 | }, |
||
348 | { |
||
349 | _id: 121, |
||
350 | _parentId: 12, |
||
351 | _depth: 3, |
||
352 | _leaf: false, |
||
353 | _hideChildren: undefined, |
||
354 | _hasChildren: true, |
||
355 | _isExpanded: true |
||
356 | }, |
||
357 | { |
||
358 | _id: 1211, |
||
359 | _parentId: 121, |
||
360 | _depth: 4, |
||
361 | _leaf: true, |
||
362 | _hideChildren: undefined, |
||
363 | _hasChildren: undefined, |
||
364 | _isExpanded: undefined |
||
365 | }, |
||
366 | { |
||
367 | _id: 2, |
||
368 | _parentId: -1, |
||
369 | code: 'banana', |
||
370 | _depth: 1, |
||
371 | _leaf: false, |
||
372 | _hideChildren: undefined, |
||
373 | _hasChildren: true, |
||
374 | _isExpanded: true |
||
375 | }, |
||
376 | { |
||
377 | _id: 21, |
||
378 | _parentId: 2, |
||
379 | _depth: 2, |
||
380 | _leaf: true, |
||
381 | _hideChildren: undefined, |
||
382 | _hasChildren: undefined, |
||
383 | _isExpanded: undefined |
||
384 | } |
||
385 | ]); |
||
386 | |||
387 | }); |
||
388 | |||
389 | }); |
||
390 |