From 7dc20624ff5e6d1942ebcf775e6d47e69e490fa8 Mon Sep 17 00:00:00 2001 From: Sebastien Vanvelthem Date: Tue, 27 Aug 2024 17:17:36 +0200 Subject: [PATCH] test(treeu): add more tests --- .../__tests__/flat-tree-ws-mapper.test.ts | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/packages/treeu/src/mapper/__tests__/flat-tree-ws-mapper.test.ts b/packages/treeu/src/mapper/__tests__/flat-tree-ws-mapper.test.ts index 46838c71a..5f541d8fd 100644 --- a/packages/treeu/src/mapper/__tests__/flat-tree-ws-mapper.test.ts +++ b/packages/treeu/src/mapper/__tests__/flat-tree-ws-mapper.test.ts @@ -186,6 +186,71 @@ describe('FlatTreeWsMapper', () => { }); }); + it('should work with object (Record) and custom separator', () => { + // inspired from prime-react onSelect event + type PrimeReactSelected = { + checked: boolean; + partialChecked: boolean; + }; + const selectedTreeNodes: Record = { + root1: { checked: false, partialChecked: true }, + root1___leaf1: { checked: true, partialChecked: false }, + root2: { checked: true, partialChecked: false }, + root2___leaf1: { checked: true, partialChecked: false }, + root2___leaf2: { checked: true, partialChecked: false }, + }; + const mapper = new FlatTreeWsMapper(); + const treeResult = mapper.toTreeNodesOrThrow(selectedTreeNodes, { + separator: '___', + }); + + const expected = [ + { + id: 'root1', + parentId: null, + value: { checked: false, partialChecked: true }, + children: [ + { + id: 'root1___leaf1', + parentId: 'root1', + value: { checked: true, partialChecked: false }, + children: [], + }, + ], + }, + { + id: 'root2', + parentId: null, + value: { checked: true, partialChecked: false }, + children: [ + { + id: 'root2___leaf1', + parentId: 'root2', + value: { checked: true, partialChecked: false }, + children: [], + }, + { + id: 'root2___leaf2', + parentId: 'root2', + value: { checked: true, partialChecked: false }, + children: [], + }, + ], + }, + ] as const satisfies TreeNode[]; + + expect(treeResult).toStrictEqual(expected); + + const flattened = mapper.fromTreeNodesOrThrow(treeResult, { + method: 'breadth-first', + }); + + const mapToObject = (map: Map) => + Object.fromEntries(map.entries()); + + expect(mapToObject(flattened)).toStrictEqual(selectedTreeNodes); + }); + it.skip('should be insensitive to path order', () => { const reversedPaths = new Map([...validFlatTreeWs].reverse()); const treeResult = new FlatTreeWsMapper().toTreeNodes(reversedPaths, {