Run hbs on server tests
This commit is contained in:
parent
de4d7d95c3
commit
b96ca1cf54
|
@ -1,21 +1,35 @@
|
||||||
|
jest.mock("@budibase/handlebars-helpers/lib/math", () => {
|
||||||
|
const actual = jest.requireActual("@budibase/handlebars-helpers/lib/math")
|
||||||
|
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
random: () => 10,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
jest.mock("@budibase/handlebars-helpers/lib/uuid", () => {
|
||||||
|
const actual = jest.requireActual("@budibase/handlebars-helpers/lib/uuid")
|
||||||
|
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
uuid: () => "f34ebc66-93bd-4f7c-b79b-92b5569138bc",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
import { processStringSync, encodeJSBinding } from "@budibase/string-templates"
|
import { processStringSync, encodeJSBinding } from "@budibase/string-templates"
|
||||||
import TestConfiguration from "../../tests/utilities/TestConfiguration"
|
const { runJsHelpersTests } = require("@budibase/string-templates/test/utils")
|
||||||
|
|
||||||
|
import tk from "timekeeper"
|
||||||
|
tk.freeze("2021-01-21T12:00:00")
|
||||||
|
|
||||||
describe("jsRunner", () => {
|
describe("jsRunner", () => {
|
||||||
const config = new TestConfiguration()
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
await config.init()
|
|
||||||
})
|
|
||||||
|
|
||||||
const processJS = (js: string, context?: object) => {
|
const processJS = (js: string, context?: object) => {
|
||||||
return config.doInContext(config.getAppId(), async () =>
|
return processStringSync(encodeJSBinding(js), context || {})
|
||||||
processStringSync(encodeJSBinding(js), context || {})
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
it("it can run a basic javascript", async () => {
|
it("it can run a basic javascript", () => {
|
||||||
const output = await processJS(`return 1 + 2`)
|
const output = processJS(`return 1 + 2`)
|
||||||
expect(output).toBe(3)
|
expect(output).toBe(3)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
runJsHelpersTests()
|
||||||
})
|
})
|
||||||
|
|
|
@ -12,7 +12,8 @@
|
||||||
"import": "./dist/bundle.mjs"
|
"import": "./dist/bundle.mjs"
|
||||||
},
|
},
|
||||||
"./package.json": "./package.json",
|
"./package.json": "./package.json",
|
||||||
"./index-helpers": "./dist/index-helpers.bundled.js"
|
"./index-helpers": "./dist/index-helpers.bundled.js",
|
||||||
|
"./test/utils": "./test/utils.js"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
|
|
|
@ -15,23 +15,13 @@ jest.mock("@budibase/handlebars-helpers/lib/uuid", () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const {
|
const { processString } = require("../src/index.cjs")
|
||||||
processString,
|
|
||||||
convertToJS,
|
|
||||||
processStringSync,
|
|
||||||
encodeJSBinding,
|
|
||||||
} = require("../src/index.cjs")
|
|
||||||
|
|
||||||
const tk = require("timekeeper")
|
const tk = require("timekeeper")
|
||||||
const { getJsHelperList } = require("../src/helpers")
|
const { getParsedManifest, runJsHelpersTests } = require("./utils")
|
||||||
const { getParsedManifest } = require("./utils")
|
|
||||||
|
|
||||||
tk.freeze("2021-01-21T12:00:00")
|
tk.freeze("2021-01-21T12:00:00")
|
||||||
|
|
||||||
const processJS = (js, context) => {
|
|
||||||
return processStringSync(encodeJSBinding(js), context)
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeRegExp(string) {
|
function escapeRegExp(string) {
|
||||||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
|
||||||
}
|
}
|
||||||
|
@ -60,36 +50,5 @@ describe("manifest", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("can be parsed and run as js", () => {
|
runJsHelpersTests()
|
||||||
const jsHelpers = getJsHelperList()
|
|
||||||
const jsExamples = Object.keys(manifest).reduce((acc, v) => {
|
|
||||||
acc[v] = manifest[v].filter(([key]) => jsHelpers[key])
|
|
||||||
return acc
|
|
||||||
}, {})
|
|
||||||
|
|
||||||
describe.each(Object.keys(jsExamples))("%s", collection => {
|
|
||||||
it.each(
|
|
||||||
jsExamples[collection].filter(
|
|
||||||
([_, { requiresHbsBody }]) => !requiresHbsBody
|
|
||||||
)
|
|
||||||
)("%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)
|
|
||||||
|
|
||||||
let result = processJS(convertedJs, context)
|
|
||||||
result = result.replace(/ /g, " ")
|
|
||||||
expect(result).toEqual(js)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
const { getManifest } = require("../src")
|
const { getManifest } = require("../src")
|
||||||
|
const { getJsHelperList } = require("../src/helpers")
|
||||||
|
|
||||||
|
const {
|
||||||
|
convertToJS,
|
||||||
|
processStringSync,
|
||||||
|
encodeJSBinding,
|
||||||
|
} = require("../src/index.cjs")
|
||||||
|
|
||||||
function tryParseJson(str) {
|
function tryParseJson(str) {
|
||||||
if (typeof str !== "string") {
|
if (typeof str !== "string") {
|
||||||
|
@ -12,7 +19,7 @@ function tryParseJson(str) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.getParsedManifest = () => {
|
const getParsedManifest = () => {
|
||||||
const manifest = getManifest()
|
const manifest = getManifest()
|
||||||
const collections = Object.keys(manifest)
|
const collections = Object.keys(manifest)
|
||||||
const examples = collections.reduce((acc, collection) => {
|
const examples = collections.reduce((acc, collection) => {
|
||||||
|
@ -51,3 +58,49 @@ module.exports.getParsedManifest = () => {
|
||||||
|
|
||||||
return examples
|
return examples
|
||||||
}
|
}
|
||||||
|
module.exports.getParsedManifest = getParsedManifest
|
||||||
|
|
||||||
|
module.exports.runJsHelpersTests = () => {
|
||||||
|
const manifest = getParsedManifest()
|
||||||
|
|
||||||
|
const processJS = (js, context) => {
|
||||||
|
return processStringSync(encodeJSBinding(js), context)
|
||||||
|
}
|
||||||
|
|
||||||
|
function escapeRegExp(string) {
|
||||||
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("can be parsed and run as js", () => {
|
||||||
|
const jsHelpers = getJsHelperList()
|
||||||
|
const jsExamples = Object.keys(manifest).reduce((acc, v) => {
|
||||||
|
acc[v] = manifest[v].filter(([key]) => jsHelpers[key])
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
|
|
||||||
|
describe.each(Object.keys(jsExamples))("%s", collection => {
|
||||||
|
it.each(
|
||||||
|
jsExamples[collection].filter(
|
||||||
|
([_, { requiresHbsBody }]) => !requiresHbsBody
|
||||||
|
)
|
||||||
|
)("%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)
|
||||||
|
|
||||||
|
let result = await processJS(convertedJs, context)
|
||||||
|
result = result.replace(/ /g, " ")
|
||||||
|
expect(result).toEqual(js)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue