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-19 13:45:23 +01:00
|
|
|
const fs = require("fs")
|
|
|
|
const { processString } = require("../src/index.cjs")
|
|
|
|
|
2024-01-19 14:05:35 +01:00
|
|
|
const tk = require("timekeeper")
|
|
|
|
tk.freeze("2021-01-21T12:00:00")
|
|
|
|
|
2024-01-19 13:45:23 +01:00
|
|
|
const manifest = JSON.parse(
|
|
|
|
fs.readFileSync(require.resolve("../manifest.json"), "utf8")
|
|
|
|
)
|
|
|
|
|
|
|
|
const examples = Object.keys(manifest).flatMap(collection =>
|
|
|
|
Object.keys(manifest[collection]).map(fnc => [collection, fnc])
|
|
|
|
)
|
|
|
|
|
2024-01-22 14:57:35 +01:00
|
|
|
function escapeRegExp(string) {
|
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
|
|
|
|
}
|
|
|
|
|
2024-01-19 13:45:23 +01:00
|
|
|
describe("manifest", () => {
|
|
|
|
describe("examples are valid", () => {
|
|
|
|
it.each(examples)("%s - %s", async (collection, func) => {
|
|
|
|
const example = manifest[collection][func].example
|
|
|
|
|
2024-01-22 12:16:52 +01:00
|
|
|
let [hbs, js] = example.split("->").map(x => x.trim())
|
2024-01-22 14:57:35 +01:00
|
|
|
|
2024-01-22 15:30:15 +01:00
|
|
|
const context = {
|
|
|
|
double: i => i * 2,
|
|
|
|
}
|
2024-01-22 14:57:35 +01:00
|
|
|
|
|
|
|
const arrays = hbs.match(/\[[^/\]]+\]/)
|
|
|
|
arrays.forEach((arrayString, i) => {
|
|
|
|
hbs = hbs.replace(new RegExp(escapeRegExp(arrayString)), `array${i}`)
|
2024-01-22 15:30:15 +01:00
|
|
|
context[`array${i}`] = JSON.parse(arrayString.replace(/\'/g, '"'))
|
2024-01-22 14:57:35 +01:00
|
|
|
})
|
2024-01-22 12:24:03 +01:00
|
|
|
|
|
|
|
if (js === undefined) {
|
|
|
|
// The function has no return value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-22 14:57:35 +01:00
|
|
|
const result = await processString(hbs, context)
|
2024-01-22 15:30:15 +01:00
|
|
|
try {
|
|
|
|
let parsedExpected
|
|
|
|
if (
|
|
|
|
Array.isArray((parsedExpected = JSON.parse(js.replace(/\'/g, '"'))))
|
|
|
|
) {
|
|
|
|
js = parsedExpected.join(",")
|
|
|
|
}
|
|
|
|
} catch {}
|
2024-01-22 14:57:35 +01:00
|
|
|
expect(result).toEqual(js)
|
2024-01-19 13:45:23 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|