Skip to content

Commit 3a175f0

Browse files
committed
Fix broken input data and filename for test
1 parent f81c6f9 commit 3a175f0

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
'use strict';
2+
3+
const getDescendants = require('../get-descendants');
4+
5+
describe('getDescendants', () => {
6+
it('should return descendants of an entity', () => {
7+
const items = [
8+
{
9+
id: 1,
10+
attributes: {
11+
order: 0,
12+
title: 'Item one',
13+
url: '/one',
14+
target: null,
15+
parent: null,
16+
},
17+
},
18+
{
19+
id: 2,
20+
attributes: {
21+
order: 1,
22+
title: 'Item two',
23+
url: '/two',
24+
target: null,
25+
parent: null,
26+
},
27+
},
28+
{
29+
id: 3,
30+
attributes: {
31+
order: 0,
32+
title: 'Item three',
33+
url: '/three',
34+
target: null,
35+
parent: {
36+
data: {
37+
id: 2,
38+
},
39+
},
40+
},
41+
},
42+
{
43+
id: 4,
44+
attributes: {
45+
order: 0,
46+
title: 'Item four',
47+
url: '/four',
48+
target: null,
49+
parent: {
50+
data: {
51+
id: 3,
52+
},
53+
},
54+
},
55+
},
56+
];
57+
const expected = [
58+
{
59+
id: 3,
60+
attributes: {
61+
order: 0,
62+
title: 'Item three',
63+
url: '/three',
64+
target: null,
65+
parent: {
66+
data: {
67+
id: 2,
68+
},
69+
},
70+
children: {
71+
data: [
72+
{
73+
id: 4,
74+
attributes: {
75+
order: 0,
76+
title: 'Item four',
77+
url: '/four',
78+
target: null,
79+
parent: {
80+
data: {
81+
id: 3,
82+
},
83+
},
84+
children: {
85+
data: [],
86+
},
87+
},
88+
},
89+
],
90+
},
91+
},
92+
},
93+
];
94+
const result = getDescendants(items, 2);
95+
96+
expect(result).toEqual(expected);
97+
});
98+
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
const get = require('lodash/get');
4+
const has = require('lodash/has');
5+
const omit = require('lodash/omit');
6+
7+
const sortByOrder = require('./sort-by-order');
8+
const getDescendants = require('./get-descendants');
9+
10+
const removeParentData = (items) =>
11+
items.reduce((acc, item) => {
12+
const sanitizedItem = omit(item, 'attributes.parent');
13+
sanitizedItem.attributes.children.data = removeParentData(item.attributes.children.data);
14+
15+
return [...acc, sanitizedItem];
16+
}, []);
17+
18+
const serializeEntity = (data, keepParentData) => {
19+
const items = get(data, 'attributes.items.data', []);
20+
21+
// Do nothing if there are no items to serialize.
22+
if (!items.length) {
23+
return data;
24+
}
25+
26+
const rootItems = items.filter((item) => !has(item, 'attributes.parent.data.id'));
27+
28+
// Assign ordered and nested items to root items.
29+
const nestedItems = rootItems.reduce((acc, item) => {
30+
const rootItem = {
31+
...item,
32+
attributes: {
33+
...item.attributes,
34+
children: {
35+
data: getDescendants(items, item.id),
36+
},
37+
},
38+
};
39+
40+
return [...acc, rootItem];
41+
}, []);
42+
43+
const sanitizedItems = !keepParentData ? removeParentData(nestedItems) : nestedItems;
44+
45+
return {
46+
...data,
47+
attributes: {
48+
...data.attributes,
49+
items: {
50+
data: sortByOrder(sanitizedItems),
51+
},
52+
},
53+
};
54+
};
55+
56+
const serializeNestedMenu = (res, keepParentData) => {
57+
const data = get(res, 'data');
58+
let sanitizedData;
59+
60+
if (Array.isArray(data)) {
61+
sanitizedData = data.map((_data) => serializeEntity(_data, keepParentData));
62+
} else {
63+
sanitizedData = serializeEntity(data, keepParentData);
64+
}
65+
66+
return {
67+
...res,
68+
data: sanitizedData,
69+
};
70+
};
71+
72+
module.exports = serializeNestedMenu;

0 commit comments

Comments
 (0)