From ceb7fc04f404fbceed429febdab94e8393998870 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 25 Jan 2024 16:15:20 +0100 Subject: [PATCH] Add js tests (not all working) --- .../string-templates/test/manifest.spec.js | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/string-templates/test/manifest.spec.js b/packages/string-templates/test/manifest.spec.js index be3b055bf4..c48bc9c9fc 100644 --- a/packages/string-templates/test/manifest.spec.js +++ b/packages/string-templates/test/manifest.spec.js @@ -16,11 +16,21 @@ jest.mock("@budibase/handlebars-helpers/lib/uuid", () => { }) const fs = require("fs") -const { processString } = require("../src/index.cjs") +const { + processString, + convertToJS, + processStringSync, + encodeJSBinding, +} = require("../src/index.cjs") const tk = require("timekeeper") + tk.freeze("2021-01-21T12:00:00") +const processJS = (js, context) => { + return processStringSync(encodeJSBinding(js), context) +} + const manifest = JSON.parse( fs.readFileSync(require.resolve("../manifest.json"), "utf8") ) @@ -95,4 +105,28 @@ describe("manifest", () => { }) }) }) + + describe("can be parsed and run as js", () => { + describe.each(Object.keys(examples))("%s", collection => { + it.each(examples[collection])("%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) + convertedJs = convertedJs.replace(/\n/g, "\n") + + let result = processJS(convertedJs, context) + result = result.replace(/ /g, " ") + expect(result).toEqual(js) + }) + }) + }) })