From b72ab885babf1a097172129ddbbd74834e786631 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodrigues Date: Thu, 18 Mar 2021 15:29:51 +0000 Subject: [PATCH] Created depth map util tests createDepthMap excludes rows that does not have selectionKey field present (the primary key) --- .../__tests__/createDepthMap.spec.ts | 68 +++++++++++++++++++ src/nestedRows/createDepthMap.ts | 16 +++-- 2 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 src/nestedRows/__tests__/createDepthMap.spec.ts diff --git a/src/nestedRows/__tests__/createDepthMap.spec.ts b/src/nestedRows/__tests__/createDepthMap.spec.ts new file mode 100644 index 0000000..676ff20 --- /dev/null +++ b/src/nestedRows/__tests__/createDepthMap.spec.ts @@ -0,0 +1,68 @@ +import { createDepthMap } from '../createDepthMap' +import { Row } from '../../types' +import { DepthMap } from '../depth-map.interface' + +describe('createDepthMap', () => { + it('should return empty', () => { + expect(createDepthMap([], 'id')).toEqual({}) + }) + + it('should return empty if selectionKey does not find the row', () => { + const rows: Row[] = [{ + id: '1234', + }] + expect(createDepthMap(rows, 'nonexistingid')).toEqual({}) + }) + + it('should return only one row if it contains a valid selectionKey field', () => { + const rows: Row[] = [{ + id: '1234', + }, { + _id: '555' + }] + expect(createDepthMap(rows, '_id')).toEqual({ "555": 1 }) + }) + + it('should create correctly the depths', () => { + const rows: Row[] = [{ + id: '1', + __children: [{ + id: '11', + __children: [{ + id: '111' + }] + }] + }, { + id: '2', + __children: [{ + id: '22' + }] + }] + const expected: DepthMap = { + 1: 1, + 11: 2, + 111: 3, + 2: 1, + 22: 2 + } + expect(createDepthMap(rows, 'id')).toEqual(expected) + }) + + it('should use a default depth if passed', () => { + const rows: Row[] = [{ + id: 1 + }] + expect(createDepthMap(rows, 'id', 5)).toEqual({ 1: 5 }) + }) + + it('should use a default depth map if provided', () => { + const rows: Row[] = [{ + id: 1 + }] + const expected: DepthMap = { + 22: 50, + 1: 1 + } + expect(createDepthMap(rows, 'id', 1, { 22: 50 })).toEqual(expected) + }) +}) \ No newline at end of file diff --git a/src/nestedRows/createDepthMap.ts b/src/nestedRows/createDepthMap.ts index 795abc8..b6dd614 100644 --- a/src/nestedRows/createDepthMap.ts +++ b/src/nestedRows/createDepthMap.ts @@ -5,8 +5,9 @@ import { DepthMap } from './depth-map.interface' * Recursively creates a map with the row ids -> depth level * @param rows * @param selectionKey - * @param depth - * @param map + * @param depth If provided will be used as the 0-th level depth + * @param map If a map is provided by default it will be mutated so make sure you create a + * new copy of it in case you want to avoid mutation */ export const createDepthMap = ( rows: Row[], @@ -15,10 +16,13 @@ export const createDepthMap = ( map = {} as DepthMap, ): DepthMap => { rows.forEach(e => { - // eslint-disable-next-line no-param-reassign - map[e[selectionKey]] = depth - if (e.__children) { - createDepthMap(e.__children, selectionKey, depth + 1, map) + const id = e[selectionKey] + if (id) { + // eslint-disable-next-line no-param-reassign + map[id] = depth + if (e.__children) { + createDepthMap(e.__children, selectionKey, depth + 1, map) + } } })