From 335228e8782f9a9e7044c9869250c05d1c2817ba Mon Sep 17 00:00:00 2001 From: Michael Drury Date: Mon, 19 Feb 2024 21:08:26 +0000 Subject: [PATCH] isolated VM test cases. --- .../src/jsRunner/tests/isolatedVM.spec.ts | 53 +++++++++++++++++++ .../src/jsRunner/tests/jsRunner.spec.ts | 21 ++++++++ 2 files changed, 74 insertions(+) diff --git a/packages/server/src/jsRunner/tests/isolatedVM.spec.ts b/packages/server/src/jsRunner/tests/isolatedVM.spec.ts index 0bd400c8d7..fd26246de5 100644 --- a/packages/server/src/jsRunner/tests/isolatedVM.spec.ts +++ b/packages/server/src/jsRunner/tests/isolatedVM.spec.ts @@ -30,4 +30,57 @@ describe("Test isolated vm directly", () => { }) expect(result).toBe("

dddd

\n") }) + + it("handle a mapping case", async () => { + const context = { + data: { + data: { + searchProducts: { + results: [ + { imageLinks: ["_S/"] } + ] + } + } + } + } + const result = await compare(` + const dataUnnested = data.data.searchProducts.results + const emptyLink = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRRC3hpq0MXqXssA28Lm5NrzcOYAyr--q3xyg&usqp=CAU" + let pImage = emptyLink + let sImage = emptyLink + let uImage = emptyLink + let lImage = emptyLink + let b1Image = emptyLink + let b2Image = emptyLink + + const dataTransformed = dataUnnested.map(x=> { + let imageLinks = x.imageLinks + for (let i = 0; i < imageLinks.length; i++){ + if(imageLinks[i].includes("_P/") || imageLinks[i].includes("_p/")){ + pImage = imageLinks[i] + } else if (imageLinks[i].includes("_S/") || imageLinks[i].includes("_s/")){ + sImage = imageLinks[i] + } else if (imageLinks[i].includes("_U/") || imageLinks[i].includes("_u/")){ + uImage = imageLinks[i] + } else if (imageLinks[i].includes("_L/") || imageLinks[i].includes("_l/")){ + lImage = imageLinks[i] + } else if (imageLinks[i].includes("_B/") || imageLinks[i].includes("_b/")){ + b1Image = imageLinks[i] + } else if (imageLinks[i].includes("_B2/") || imageLinks[i].includes("_b2/")){ + b2Image = imageLinks[i] + } + } + + const arrangedLinks = [pImage, sImage, uImage, lImage, b1Image, b2Image] + x.imageLinks = arrangedLinks + + return x + }) + + return dataTransformed + `, context) + expect(result).toBeDefined() + expect(result.length).toBe(1) + expect(result[0].imageLinks.length).toBe(6) + }) }) diff --git a/packages/server/src/jsRunner/tests/jsRunner.spec.ts b/packages/server/src/jsRunner/tests/jsRunner.spec.ts index 9bb9052eda..6dc6fd0887 100644 --- a/packages/server/src/jsRunner/tests/jsRunner.spec.ts +++ b/packages/server/src/jsRunner/tests/jsRunner.spec.ts @@ -144,5 +144,26 @@ describe("jsRunner (using isolated-vm)", () => { expect(result).toBeDefined() expect(result).toBe(3) }) + + it("should handle test case 4", async () => { + const context = { + "Time Sheets": ["a", "b"] + } + const result = await processJS(` + let hours = 0 + if (($("[Time Sheets]") != null) == true){ + for (i = 0; i < $("[Time Sheets]").length; i++){ + let hoursLogged = "Time Sheets." + i + ".Hours" + hours += $(hoursLogged) + } + return hours + } + if (($("[Time Sheets]") != null) == false){ + return hours + } + `, context) + expect(result).toBeDefined() + expect(result).toBe("0ab") + }) }) })