From bcd9bd43a6fb76fdb9c236375b0d361ae490d66f Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Tue, 29 Oct 2024 16:19:27 +0100 Subject: [PATCH] fix: add tests for deduplicate orgunits --- .../deduplicate-org-unit-roots.test.ts | 109 ++++++++++++++++++ .../organisation-unit-tree.js | 4 +- 2 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 components/organisation-unit-tree/src/organisation-unit-tree/deduplicate-org-unit-roots.test.ts diff --git a/components/organisation-unit-tree/src/organisation-unit-tree/deduplicate-org-unit-roots.test.ts b/components/organisation-unit-tree/src/organisation-unit-tree/deduplicate-org-unit-roots.test.ts new file mode 100644 index 000000000..9a289b873 --- /dev/null +++ b/components/organisation-unit-tree/src/organisation-unit-tree/deduplicate-org-unit-roots.test.ts @@ -0,0 +1,109 @@ +import { deduplicateOrgUnitRoots } from './deduplicate-org-unit-roots.js' + + +const unitToPath = { + sierra: '/ImspTQPwCqd', + tanzania: '/N5hLlID8ihI', + ethiopia: '/LK1v9z1Jt3k', + 'sierra/bo': '/ImspTQPwCqd/O6uvpzGd5pu', + 'sierra/bo/bargbe': '/ImspTQPwCqd/O6uvpzGd5pu/dGheVylzol6', + 'sierra/bo/bargbe/barlie': '/ImspTQPwCqd/O6uvpzGd5pu/dGheVylzol6/y5hLlID8ihI', + 'sierra/bargbe/barlie': '/ImspTQPwCqd/dGheVylzol6/y5hLlID8ihI', + 'sierra/bargbe/barlie/ngalu': '/ImspTQPwCqd/O6uvpzGd5pu/dGheVylzol6/y5hLlID8ihI/Aj5v9z1Jt3k', + 'sierra/bo/baoma': '/ImspTQPwCqd/O6uvpzGd5pu/vWbkYPRmKyS', + 'sierra/bo/baoma/faabu': '/ImspTQPwCqd/O6uvpzGd5pu/vWbkYPRmKyS/ZpE2POxvl9P', + 'sierra/bo/badjia': '/ImspTQPwCqd/O6uvpzGd5pu/YuQRtpLP10I', + 'sierra/bo/badjia/ngelehun': '/ImspTQPwCqd/O6uvpzGd5pu/YuQRtpLP10I/DiszpKrYNg8', + 'sierra/bombali': '/ImspTQPwCqd/fdc6uOvgoji', +} + +describe('findMinimumRootUnits', () => { + it('should return a single root unit when there is only one unit', () => { + const units = [{ path: unitToPath.sierra, level: 1 }] + const result = deduplicateOrgUnitRoots(units) + expect(result).toEqual([{ path: unitToPath.sierra, level: 1 }]) + // should not mutate the input + expect(units).toStrictEqual(units) + }) + + it('should return two root units when there are two sibling units', () => { + const units = [ + { path: unitToPath['sierra/bo'], level: 2 }, + { path: unitToPath['sierra/bombali'], level: 2 }, + ] + + const result = deduplicateOrgUnitRoots(units) + expect(result).toEqual([ + { path: unitToPath['sierra/bo'], level: 2 }, + { path: unitToPath['sierra/bombali'], level: 2 }, + ]) + }) + + it('should return only the root unit when one unit is a child of another', () => { + const units = [ + { path: unitToPath['sierra'], level: 1 }, + { path: unitToPath['sierra/bo'], level: 2 }, + ] + const result = deduplicateOrgUnitRoots(units) + expect(result).toEqual([{ path: unitToPath['sierra'], level: 1 }]) + }) + + it('should return only the root unit when one unit is a deep child of another', () => { + const units = [ + { path: unitToPath['sierra'], level: 1 }, + { path: unitToPath['sierra/bo/badjia/ngelehun'], level: 4 }, + ] + const result = deduplicateOrgUnitRoots(units) + expect(result).toEqual([{ path: unitToPath['sierra'], level: 1 }]) + }) + + it('should return multiple root units when paths do not overlap', () => { + const units = [ + { path: unitToPath['sierra'], level: 1 }, + { path: unitToPath['tanzania'], level: 1 }, + { path: unitToPath['ethiopia'], level: 1 }, + ] + const result = deduplicateOrgUnitRoots(units) + expect(result).toEqual([ + { path: unitToPath['sierra'], level: 1 }, + { path: unitToPath['tanzania'], level: 1 }, + { path: unitToPath['ethiopia'], level: 1 }, + ]) + }) + + it('should return the correct root units when there is a mix of roots and children', () => { + const units = [ + { path: unitToPath['sierra/bo/badjia/ngelehun'], level: 4 }, + { path: unitToPath['sierra/bo/baoma/faabu'], level: 4 }, + { path: unitToPath['sierra/bo/baoma'], level: 3 }, + { path: unitToPath['sierra/bo/bargbe'], level: 3 }, + ] + const result = deduplicateOrgUnitRoots(units) + expect(result).toEqual([ + { path: unitToPath['sierra/bo/baoma'], level: 3 }, + { path: unitToPath['sierra/bo/bargbe'], level: 3 }, + { path: unitToPath['sierra/bo/badjia/ngelehun'], level: 4 }, + ]) + }) + + it('should return the root units when multiple nested children exist', () => { + const units = [ + { path: unitToPath['sierra/bo'], level: 2 }, + { path: unitToPath['sierra/bo/badjia'], level: 3 }, + { path: unitToPath['sierra/bo/baoma'], level: 3 }, + { path: unitToPath['sierra/bargbe/barlie'], level: 3 }, + { path: unitToPath['sierra/bargbe/barlie/ngalu'], level: 4 }, + ] + const result = deduplicateOrgUnitRoots(units) + expect(result).toEqual([ + { path: unitToPath['sierra/bo'], level: 2 }, + { path: unitToPath['sierra/bargbe/barlie'], level: 3 }, + ]) + }) + + it('should handle empty input and return an empty array', () => { + const units = [] + const result = deduplicateOrgUnitRoots(units) + expect(result).toEqual([]) + }) +}) diff --git a/components/organisation-unit-tree/src/organisation-unit-tree/organisation-unit-tree.js b/components/organisation-unit-tree/src/organisation-unit-tree/organisation-unit-tree.js index c99ae26d5..af4c63c91 100644 --- a/components/organisation-unit-tree/src/organisation-unit-tree/organisation-unit-tree.js +++ b/components/organisation-unit-tree/src/organisation-unit-tree/organisation-unit-tree.js @@ -3,14 +3,14 @@ import PropTypes from 'prop-types' import React, { useEffect, useMemo, useState } from 'react' import { OrganisationUnitNode } from '../organisation-unit-node/index.js' import { orgUnitPathPropType } from '../prop-types.js' +import { deduplicateOrgUnitRoots } from './deduplicate-org-unit-roots.js' import { defaultRenderNodeLabel } from './default-render-node-label/index.js' -import { filterRootIds, findMinimumRootUnits } from './filter-root-ids.js' +import { filterRootIds } from './filter-root-ids.js' import { OrganisationUnitTreeRootError } from './organisation-unit-tree-root-error.js' import { OrganisationUnitTreeRootLoading } from './organisation-unit-tree-root-loading.js' import { useExpanded } from './use-expanded/index.js' import { useForceReload } from './use-force-reload.js' import { useRootOrgData } from './use-root-org-data/index.js' -import { deduplicateOrgUnitRoots } from './deduplicate-org-unit-roots.js' // A stable object to reference const staticArray = []