2024-02-09 13:28:37 +01:00
|
|
|
const vm = require("vm")
|
|
|
|
|
2024-01-22 10:40:26 +01:00
|
|
|
jest.mock("@budibase/handlebars-helpers/lib/math", () => {
|
|
|
|
const actual = jest.requireActual("@budibase/handlebars-helpers/lib/math")
|
|
|
|
|
|
|
|
return {
|
|
|
|
...actual,
|
|
|
|
random: () => 10,
|
|
|
|
}
|
|
|
|
})
|
2024-01-22 22:08:55 +01:00
|
|
|
jest.mock("@budibase/handlebars-helpers/lib/uuid", () => {
|
|
|
|
const actual = jest.requireActual("@budibase/handlebars-helpers/lib/uuid")
|
|
|
|
|
|
|
|
return {
|
|
|
|
...actual,
|
|
|
|
uuid: () => "f34ebc66-93bd-4f7c-b79b-92b5569138bc",
|
|
|
|
}
|
|
|
|
})
|
2024-01-22 10:40:26 +01:00
|
|
|
|
2024-02-21 23:11:38 +01:00
|
|
|
const { processString, setJSRunner } = require("../src/index")
|
2024-01-19 13:45:23 +01:00
|
|
|
|
2024-01-19 14:05:35 +01:00
|
|
|
const tk = require("timekeeper")
|
2024-02-02 12:02:09 +01:00
|
|
|
const { getParsedManifest, runJsHelpersTests } = require("./utils")
|
2024-01-25 16:15:20 +01:00
|
|
|
|
2024-01-19 14:05:35 +01:00
|
|
|
tk.freeze("2021-01-21T12:00:00")
|
|
|
|
|
2024-01-22 14:57:35 +01:00
|
|
|
function escapeRegExp(string) {
|
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
|
|
|
|
}
|
|
|
|
|
2024-02-02 10:51:46 +01:00
|
|
|
describe("manifest", () => {
|
2024-02-02 12:02:09 +01:00
|
|
|
const manifest = getParsedManifest()
|
|
|
|
|
2024-02-09 13:28:37 +01:00
|
|
|
beforeAll(() => {
|
|
|
|
setJSRunner((js, context) => {
|
|
|
|
return vm.runInNewContext(js, context, { timeout: 1000 })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-01-19 13:45:23 +01:00
|
|
|
describe("examples are valid", () => {
|
2024-02-02 12:02:09 +01:00
|
|
|
describe.each(Object.keys(manifest))("%s", collection => {
|
|
|
|
it.each(manifest[collection])("%s", async (_, { hbs, js }) => {
|
2024-01-22 22:06:40 +01:00
|
|
|
const context = {
|
|
|
|
double: i => i * 2,
|
2024-01-23 14:28:58 +01:00
|
|
|
isString: x => typeof x === "string",
|
2024-01-22 22:06:40 +01:00
|
|
|
}
|
2024-01-22 14:57:35 +01:00
|
|
|
|
2024-01-22 22:06:40 +01:00
|
|
|
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, '"'))
|
|
|
|
})
|
2024-01-22 12:24:03 +01:00
|
|
|
|
2024-01-22 22:06:40 +01:00
|
|
|
let result = await processString(hbs, context)
|
|
|
|
result = result.replace(/ /g, " ")
|
|
|
|
expect(result).toEqual(js)
|
|
|
|
})
|
2024-01-19 13:45:23 +01:00
|
|
|
})
|
|
|
|
})
|
2024-01-25 16:15:20 +01:00
|
|
|
|
2024-02-02 12:02:09 +01:00
|
|
|
runJsHelpersTests()
|
2024-01-19 13:45:23 +01:00
|
|
|
})
|