All files / src/jest-serializer CategoryTreeSerializer.js

100% Statements 18/18
100% Branches 7/7
100% Functions 7/7
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28658x   658x 89x 115x 115x     658x 39x 39x 48x 43x   115x 39x 7x   39x     6085x   658x        
const { asTree } = require('treeify');
 
const arrayToObject = (val, edges) =>
  val.reduce((tree, node) => {
    tree[node] = edges[node] ? arrayToObject(edges[node], edges) : undefined;
    return tree;
  }, {});
 
const serializeCategoryTree = ct => {
  const tree = arrayToObject(ct.rootIds, ct.edges);
  const assignedKeys = Object.keys(ct.edges)
    .filter(key => key in ct.nodes)
    .reduce((acc, key) => [...acc, ...ct.edges[key]], [])
    .concat(ct.rootIds);
  const danglingKeys = Object.keys(ct.nodes).filter(key => !assignedKeys.includes(key));
  if (danglingKeys.length) {
    tree.dangling = arrayToObject(danglingKeys, ct.edges);
  }
  return asTree(tree);
};
 
const test = val => val && val.rootIds && val.edges && val.nodes;
 
module.exports = {
  print: serializeCategoryTree,
  test: test,
};