2024-02-22 11:43:12 +13:00
|
|
|
import vm from "vm"
|
2024-02-10 01:28:37 +13:00
|
|
|
|
2024-01-22 22:40:26 +13:00
|
|
|
jest.mock("@budibase/handlebars-helpers/lib/math", () => {
|
|
|
|
const actual = jest.requireActual("@budibase/handlebars-helpers/lib/math")
|
|
|
|
|
|
|
|
return {
|
|
|
|
...actual,
|
|
|
|
random: () => 10,
|
|
|
|
}
|
|
|
|
})
|
2024-01-23 10:08:55 +13: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 22:40:26 +13:00
|
|
|
|
2024-02-22 11:43:12 +13:00
|
|
|
import { processString, setJSRunner } from "../src/index"
|
2024-01-20 01:45:23 +13:00
|
|
|
|
2024-02-22 11:43:12 +13:00
|
|
|
import tk from "timekeeper"
|
|
|
|
import { getParsedManifest, runJsHelpersTests } from "./utils"
|
2024-01-26 04:15:20 +13:00
|
|
|
|
2024-01-20 02:05:35 +13:00
|
|
|
tk.freeze("2021-01-21T12:00:00")
|
|
|
|
|
2024-03-15 21:41:32 +13:00
|
|
|
function escapeRegExp(string: string) {
|
2024-01-23 02:57:35 +13:00
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
|
|
|
|
}
|
|
|
|
|
2024-02-02 22:51:46 +13:00
|
|
|
describe("manifest", () => {
|
2024-02-03 00:02:09 +13:00
|
|
|
const manifest = getParsedManifest()
|
|
|
|
|
2024-02-10 01:28:37 +13:00
|
|
|
beforeAll(() => {
|
|
|
|
setJSRunner((js, context) => {
|
|
|
|
return vm.runInNewContext(js, context, { timeout: 1000 })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-01-20 01:45:23 +13:00
|
|
|
describe("examples are valid", () => {
|
2024-02-03 00:02:09 +13:00
|
|
|
describe.each(Object.keys(manifest))("%s", collection => {
|
|
|
|
it.each(manifest[collection])("%s", async (_, { hbs, js }) => {
|
2024-03-15 21:41:32 +13:00
|
|
|
const context: any = {
|
|
|
|
double: (i: number) => i * 2,
|
|
|
|
isString: (x: any) => typeof x === "string",
|
2024-01-23 10:06:40 +13:00
|
|
|
}
|
2024-01-23 02:57:35 +13:00
|
|
|
|
2024-01-23 10:06:40 +13:00
|
|
|
const arrays = hbs.match(/\[[^/\]]+\]/)
|
|
|
|
arrays?.forEach((arrayString, i) => {
|
|
|
|
hbs = hbs.replace(new RegExp(escapeRegExp(arrayString)), `array${i}`)
|
2024-03-20 00:14:16 +13:00
|
|
|
context[`array${i}`] = JSON.parse(arrayString.replace(/'/g, '"'))
|
2024-01-23 10:06:40 +13:00
|
|
|
})
|
2024-01-23 00:24:03 +13:00
|
|
|
|
2024-01-23 10:06:40 +13:00
|
|
|
let result = await processString(hbs, context)
|
|
|
|
result = result.replace(/ /g, " ")
|
|
|
|
expect(result).toEqual(js)
|
|
|
|
})
|
2024-01-20 01:45:23 +13:00
|
|
|
})
|
|
|
|
})
|
2024-01-26 04:15:20 +13:00
|
|
|
|
2024-02-03 00:02:09 +13:00
|
|
|
runJsHelpersTests()
|
2024-01-20 01:45:23 +13:00
|
|
|
})
|