From e1795a0ad18732c52d227f2a00ea396ede45280c Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 1 Feb 2025 14:18:54 +0100 Subject: [PATCH] test(server/utils): add tests for toMap --- src/services/utils.spec.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/services/utils.spec.ts b/src/services/utils.spec.ts index ad5c0841c..db4b94165 100644 --- a/src/services/utils.spec.ts +++ b/src/services/utils.spec.ts @@ -423,7 +423,28 @@ describe("#normalize", () => { }); -describe.todo("#toMap", () => {}); +describe("#toMap", () => { + it("should return an instace of Map, with the correct size and keys, when supplied with a list and existing keys", () => { + const testList = [{title: "test", propA: "text", propB: 123 }, {title: "test2", propA: "prop2", propB: 456 }]; + const result = utils.toMap(testList, "title"); + expect(result).toBeInstanceOf(Map); + expect(result.size).toBe(2); + expect(Array.from(result.keys())).toStrictEqual(["test", "test2"]); + }); + it("should return an instace of Map, with an empty size, when the supplied list does not contain the supplied key", () => { + const testList = [{title: "test", propA: "text", propB: 123 }, {title: "test2", propA: "prop2", propB: 456 }]; + //@ts-expect-error - key is non-existing on supplied list type + const result = utils.toMap(testList, "nonExistingKey"); + expect(result).toBeInstanceOf(Map); + expect(result.size).toBe(0); + }); + it.fails("should correctly handle duplicate keys? (currently it will overwrite the entry, so returned size will be 1 instead of 2)", () => { + const testList = [{title: "testDupeTitle", propA: "text", propB: 123 }, {title: "testDupeTitle", propA: "prop2", propB: 456 }]; + const result = utils.toMap(testList, "title"); + expect(result).toBeInstanceOf(Map); + expect(result.size).toBe(2); + }); +}); describe("#envToBoolean", () => { const testCases: TestCase[] = [