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-01-19 13:45:23 +01:00
|
|
|
const fs = require("fs")
|
2024-01-25 16:15:20 +01:00
|
|
|
const {
|
|
|
|
processString,
|
|
|
|
convertToJS,
|
|
|
|
processStringSync,
|
|
|
|
encodeJSBinding,
|
|
|
|
} = require("../src/index.cjs")
|
2024-01-19 13:45:23 +01:00
|
|
|
|
2024-01-19 14:05:35 +01:00
|
|
|
const tk = require("timekeeper")
|
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-25 16:15:20 +01:00
|
|
|
const processJS = (js, context) => {
|
|
|
|
return processStringSync(encodeJSBinding(js), context)
|
|
|
|
}
|
|
|
|
|
2024-01-19 13:45:23 +01:00
|
|
|
const manifest = JSON.parse(
|
|
|
|
fs.readFileSync(require.resolve("../manifest.json"), "utf8")
|
|
|
|
)
|
|
|
|
|
2024-01-22 22:06:40 +01:00
|
|
|
const collections = Object.keys(manifest)
|
2024-01-22 22:35:35 +01:00
|
|
|
const examples = collections.reduce((acc, collection) => {
|
2024-01-25 15:56:02 +01:00
|
|
|
const functions = Object.entries(manifest[collection])
|
|
|
|
.filter(([_, details]) => details.example)
|
|
|
|
.map(([name, details]) => {
|
|
|
|
const example = details.example
|
|
|
|
let [hbs, js] = example.split("->").map(x => x.trim())
|
|
|
|
if (!js) {
|
|
|
|
// The function has no return value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim 's
|
|
|
|
js = js.replace(/^\'|\'$/g, "")
|
|
|
|
if ((parsedExpected = tryParseJson(js))) {
|
|
|
|
if (Array.isArray(parsedExpected)) {
|
|
|
|
if (typeof parsedExpected[0] === "object") {
|
|
|
|
js = JSON.stringify(parsedExpected)
|
|
|
|
} else {
|
|
|
|
js = parsedExpected.join(",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-30 15:49:18 +01:00
|
|
|
const requiresHbsBody = details.requiresBlock
|
|
|
|
return [name, { hbs, js, requiresHbsBody }]
|
2024-01-25 15:56:02 +01:00
|
|
|
})
|
|
|
|
.filter(x => !!x)
|
|
|
|
|
|
|
|
if (Object.keys(functions).length) {
|
2024-01-22 22:35:35 +01:00
|
|
|
acc[collection] = functions
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, {})
|
2024-01-19 13:45:23 +01:00
|
|
|
|
2024-01-22 14:57:35 +01:00
|
|
|
function escapeRegExp(string) {
|
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
|
|
|
|
}
|
|
|
|
|
2024-01-23 19:40:31 +01:00
|
|
|
function tryParseJson(str) {
|
|
|
|
if (typeof str !== "string") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(str.replace(/\'/g, '"'))
|
|
|
|
} catch (e) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-19 13:45:23 +01:00
|
|
|
describe("manifest", () => {
|
|
|
|
describe("examples are valid", () => {
|
2024-01-22 22:35:35 +01:00
|
|
|
describe.each(Object.keys(examples))("%s", collection => {
|
2024-01-26 13:37:28 +01:00
|
|
|
it.each(examples[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
|
|
|
|
|
|
|
describe("can be parsed and run as js", () => {
|
|
|
|
describe.each(Object.keys(examples))("%s", collection => {
|
2024-01-30 10:21:33 +01:00
|
|
|
it.each(
|
2024-01-30 15:49:18 +01:00
|
|
|
examples[collection].filter(
|
|
|
|
([_, { requiresHbsBody }]) => !requiresHbsBody
|
|
|
|
)
|
2024-01-30 10:21:33 +01:00
|
|
|
)("%s", async (_, { hbs, js }) => {
|
2024-01-25 16:15:20 +01:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2024-01-19 13:45:23 +01:00
|
|
|
})
|