From 56ed9a477f0947a623139f28bb2b5a8dce51e457 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 25 Mar 2024 14:17:45 +0100 Subject: [PATCH] Add tests --- packages/string-templates/test/basic.spec.ts | 66 ++++++++++++++------ 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/packages/string-templates/test/basic.spec.ts b/packages/string-templates/test/basic.spec.ts index a9cf909aad..ddea54c2bf 100644 --- a/packages/string-templates/test/basic.spec.ts +++ b/packages/string-templates/test/basic.spec.ts @@ -127,31 +127,59 @@ describe("Test that the object processing works correctly", () => { }) describe("check arrays", () => { - it.each([ - [0, "1"], - [1, "2"], - ])("should handle an array of primitive types", async (index, expected) => { - const json = [1, 2, 3] - const output = await processString(`{{ testing.${index} }}`, { - testing: json, + describe("index with square brackets", () => { + it.each([ + [0, "1"], + [1, "2"], + ])("should handle an array of primitive types", async (index, expected) => { + const json = [1, 2, 3] + const output = await processString(`{{ testing.[${index}] }}`, { + testing: json, + }) + expect(output).toEqual(expected) + }) + + it("should handle an array of objects", async () => { + const json = [{ value: 1 }, { value: 2 }, { value: 3 }] + const output = await processString("{{ testing.[1] }}", { + testing: json, + }) + expect(output).toEqual('{"value":2}') + }) + + it("should handle nesting properties in an array of objects", async () => { + const json = [{ value: 1 }, { value: 2 }, { value: 3 }] + const output = await processString("{{ testing.[1].value }}", { + testing: json, + }) + expect(output).toEqual("2") }) - expect(output).toEqual(expected) }) - it("should handle an array of object types", async () => { - const json = [{ value: 1 }, { value: 2 }, { value: 3 }] - const output = await processString("{{ testing.0 }}", { - testing: json, + describe("index without square brackets", () => { + it("should not handle an array of primitive types", async () => { + const json = [1, 2, 3] + const output = await processString(`{{ testing.1 }}`, { + testing: json, + }) + expect(output).toEqual("{{ testing.1 }}") }) - expect(output).toEqual({ value: 1 }) - }) - it("should handle nesting properties in an array of object types", async () => { - const json = [{ value: 1 }, { value: 2 }, { value: 3 }] - const output = await processString("{{ testing.0.value }}", { - testing: json, + it("should not handle an array of objects", async () => { + const json = [{ value: 1 }, { value: 2 }, { value: 3 }] + const output = await processString("{{ testing.1 }}", { + testing: json, + }) + expect(output).toEqual("{{ testing.1 }}") + }) + + it("should handle nesting properties in an array of object types", async () => { + const json = [{ value: 1 }, { value: 2 }, { value: 3 }] + const output = await processString("{{ testing.1.value }}", { + testing: json, + }) + expect(output).toEqual("2") }) - expect(output).toEqual("1") }) })