Add js tests (not all working)
This commit is contained in:
parent
b36c8ad476
commit
ceb7fc04f4
|
@ -16,11 +16,21 @@ jest.mock("@budibase/handlebars-helpers/lib/uuid", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
const fs = require("fs")
|
const fs = require("fs")
|
||||||
const { processString } = require("../src/index.cjs")
|
const {
|
||||||
|
processString,
|
||||||
|
convertToJS,
|
||||||
|
processStringSync,
|
||||||
|
encodeJSBinding,
|
||||||
|
} = require("../src/index.cjs")
|
||||||
|
|
||||||
const tk = require("timekeeper")
|
const tk = require("timekeeper")
|
||||||
|
|
||||||
tk.freeze("2021-01-21T12:00:00")
|
tk.freeze("2021-01-21T12:00:00")
|
||||||
|
|
||||||
|
const processJS = (js, context) => {
|
||||||
|
return processStringSync(encodeJSBinding(js), context)
|
||||||
|
}
|
||||||
|
|
||||||
const manifest = JSON.parse(
|
const manifest = JSON.parse(
|
||||||
fs.readFileSync(require.resolve("../manifest.json"), "utf8")
|
fs.readFileSync(require.resolve("../manifest.json"), "utf8")
|
||||||
)
|
)
|
||||||
|
@ -95,4 +105,28 @@ describe("manifest", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("can be parsed and run as js", () => {
|
||||||
|
describe.each(Object.keys(examples))("%s", collection => {
|
||||||
|
it.each(examples[collection])("%s", async (_, hbs, js) => {
|
||||||
|
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)
|
||||||
|
convertedJs = convertedJs.replace(/\n/g, "\n")
|
||||||
|
|
||||||
|
let result = processJS(convertedJs, context)
|
||||||
|
result = result.replace(/ /g, " ")
|
||||||
|
expect(result).toEqual(js)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue