budibase/packages/string-templates/test/manifest.spec.js

97 lines
2.5 KiB
JavaScript
Raw Normal View History

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")
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")
)
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) => {
const functions = Object.keys(manifest[collection]).filter(
2024-01-23 14:28:52 +01:00
fnc => manifest[collection][fnc].example
2024-01-22 22:35:35 +01:00
)
if (functions.length) {
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 => {
it.each(examples[collection])("%s", async func => {
2024-01-22 22:06:40 +01:00
const example = manifest[collection][func].example
2024-01-19 13:45:23 +01:00
2024-01-22 22:06:40 +01:00
let [hbs, js] = example.split("->").map(x => x.trim())
2024-01-22 14:57:35 +01:00
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
if (js === undefined) {
// The function has no return value
return
2024-01-22 15:30:15 +01:00
}
2024-01-22 22:06:40 +01:00
let result = await processString(hbs, context)
// Trim 's
js = js.replace(/^\'|\'$/g, "")
2024-01-23 19:40:31 +01:00
if ((parsedExpected = tryParseJson(js))) {
if (Array.isArray(parsedExpected)) {
2024-01-23 16:00:07 +01:00
if (typeof parsedExpected[0] === "object") {
js = JSON.stringify(parsedExpected)
} else {
js = parsedExpected.join(",")
}
2024-01-22 22:06:40 +01:00
}
2024-01-23 19:40:31 +01:00
}
2024-01-22 22:06:40 +01:00
result = result.replace(/ /g, " ")
expect(result).toEqual(js)
})
2024-01-19 13:45:23 +01:00
})
})
})