From b96ca1cf54dc31899bd2d8260f19635049f4ad66 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 31 Jan 2024 16:15:53 +0100 Subject: [PATCH] Run hbs on server tests --- .../src/jsRunner/tests/jsRunner.spec.ts | 38 +++++++++---- packages/string-templates/package.json | 3 +- .../string-templates/test/manifest.spec.js | 47 +--------------- packages/string-templates/test/utils.js | 55 ++++++++++++++++++- 4 files changed, 85 insertions(+), 58 deletions(-) diff --git a/packages/server/src/jsRunner/tests/jsRunner.spec.ts b/packages/server/src/jsRunner/tests/jsRunner.spec.ts index 730a92e99b..35c01a5a8a 100644 --- a/packages/server/src/jsRunner/tests/jsRunner.spec.ts +++ b/packages/server/src/jsRunner/tests/jsRunner.spec.ts @@ -1,21 +1,35 @@ +jest.mock("@budibase/handlebars-helpers/lib/math", () => { + const actual = jest.requireActual("@budibase/handlebars-helpers/lib/math") + + return { + ...actual, + random: () => 10, + } +}) +jest.mock("@budibase/handlebars-helpers/lib/uuid", () => { + const actual = jest.requireActual("@budibase/handlebars-helpers/lib/uuid") + + return { + ...actual, + uuid: () => "f34ebc66-93bd-4f7c-b79b-92b5569138bc", + } +}) + import { processStringSync, encodeJSBinding } from "@budibase/string-templates" -import TestConfiguration from "../../tests/utilities/TestConfiguration" +const { runJsHelpersTests } = require("@budibase/string-templates/test/utils") + +import tk from "timekeeper" +tk.freeze("2021-01-21T12:00:00") describe("jsRunner", () => { - const config = new TestConfiguration() - - beforeAll(async () => { - await config.init() - }) - const processJS = (js: string, context?: object) => { - return config.doInContext(config.getAppId(), async () => - processStringSync(encodeJSBinding(js), context || {}) - ) + return processStringSync(encodeJSBinding(js), context || {}) } - it("it can run a basic javascript", async () => { - const output = await processJS(`return 1 + 2`) + it("it can run a basic javascript", () => { + const output = processJS(`return 1 + 2`) expect(output).toBe(3) }) + + runJsHelpersTests() }) diff --git a/packages/string-templates/package.json b/packages/string-templates/package.json index 6d0e9298e5..c7a4e79c86 100644 --- a/packages/string-templates/package.json +++ b/packages/string-templates/package.json @@ -12,7 +12,8 @@ "import": "./dist/bundle.mjs" }, "./package.json": "./package.json", - "./index-helpers": "./dist/index-helpers.bundled.js" + "./index-helpers": "./dist/index-helpers.bundled.js", + "./test/utils": "./test/utils.js" }, "files": [ "dist", diff --git a/packages/string-templates/test/manifest.spec.js b/packages/string-templates/test/manifest.spec.js index 6dd5e8933d..fa8683960a 100644 --- a/packages/string-templates/test/manifest.spec.js +++ b/packages/string-templates/test/manifest.spec.js @@ -15,23 +15,13 @@ jest.mock("@budibase/handlebars-helpers/lib/uuid", () => { } }) -const { - processString, - convertToJS, - processStringSync, - encodeJSBinding, -} = require("../src/index.cjs") +const { processString } = require("../src/index.cjs") const tk = require("timekeeper") -const { getJsHelperList } = require("../src/helpers") -const { getParsedManifest } = require("./utils") +const { getParsedManifest, runJsHelpersTests } = require("./utils") tk.freeze("2021-01-21T12:00:00") -const processJS = (js, context) => { - return processStringSync(encodeJSBinding(js), context) -} - function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string } @@ -60,36 +50,5 @@ describe("manifest", () => { }) }) - describe("can be parsed and run as js", () => { - const jsHelpers = getJsHelperList() - const jsExamples = Object.keys(manifest).reduce((acc, v) => { - acc[v] = manifest[v].filter(([key]) => jsHelpers[key]) - return acc - }, {}) - - describe.each(Object.keys(jsExamples))("%s", collection => { - it.each( - jsExamples[collection].filter( - ([_, { requiresHbsBody }]) => !requiresHbsBody - ) - )("%s", async (_, { hbs, js }) => { - const context = { - double: i => i * 2, - isString: x => typeof x === "string", - } - - const arrays = hbs.match(/\[[^/\]]+\]/) - arrays?.forEach((arrayString, i) => { - hbs = hbs.replace(new RegExp(escapeRegExp(arrayString)), `array${i}`) - context[`array${i}`] = JSON.parse(arrayString.replace(/\'/g, '"')) - }) - - let convertedJs = convertToJS(hbs) - - let result = processJS(convertedJs, context) - result = result.replace(/ /g, " ") - expect(result).toEqual(js) - }) - }) - }) + runJsHelpersTests() }) diff --git a/packages/string-templates/test/utils.js b/packages/string-templates/test/utils.js index 1b9d030cc5..8b8943c776 100644 --- a/packages/string-templates/test/utils.js +++ b/packages/string-templates/test/utils.js @@ -1,4 +1,11 @@ const { getManifest } = require("../src") +const { getJsHelperList } = require("../src/helpers") + +const { + convertToJS, + processStringSync, + encodeJSBinding, +} = require("../src/index.cjs") function tryParseJson(str) { if (typeof str !== "string") { @@ -12,7 +19,7 @@ function tryParseJson(str) { } } -module.exports.getParsedManifest = () => { +const getParsedManifest = () => { const manifest = getManifest() const collections = Object.keys(manifest) const examples = collections.reduce((acc, collection) => { @@ -51,3 +58,49 @@ module.exports.getParsedManifest = () => { return examples } +module.exports.getParsedManifest = getParsedManifest + +module.exports.runJsHelpersTests = () => { + const manifest = getParsedManifest() + + const processJS = (js, context) => { + return processStringSync(encodeJSBinding(js), context) + } + + function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string + } + + describe("can be parsed and run as js", () => { + const jsHelpers = getJsHelperList() + const jsExamples = Object.keys(manifest).reduce((acc, v) => { + acc[v] = manifest[v].filter(([key]) => jsHelpers[key]) + return acc + }, {}) + + describe.each(Object.keys(jsExamples))("%s", collection => { + it.each( + jsExamples[collection].filter( + ([_, { requiresHbsBody }]) => !requiresHbsBody + ) + )("%s", async (_, { hbs, js }) => { + const context = { + double: i => i * 2, + isString: x => typeof x === "string", + } + + const arrays = hbs.match(/\[[^/\]]+\]/) + arrays?.forEach((arrayString, i) => { + hbs = hbs.replace(new RegExp(escapeRegExp(arrayString)), `array${i}`) + context[`array${i}`] = JSON.parse(arrayString.replace(/\'/g, '"')) + }) + + let convertedJs = convertToJS(hbs) + + let result = await processJS(convertedJs, context) + result = result.replace(/ /g, " ") + expect(result).toEqual(js) + }) + }) + }) +}