2024-02-21 23:43:12 +01:00
|
|
|
import { getManifest } from "../src"
|
|
|
|
import { getJsHelperList } from "../src/helpers"
|
2024-02-02 12:02:09 +01:00
|
|
|
|
2024-02-21 23:43:12 +01:00
|
|
|
import { convertToJS, processStringSync, encodeJSBinding } from "../src/index"
|
2024-02-02 12:02:09 +01:00
|
|
|
|
2024-03-15 09:41:32 +01:00
|
|
|
function tryParseJson(str: string) {
|
2024-02-02 12:02:09 +01:00
|
|
|
if (typeof str !== "string") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(str.replace(/'/g, '"'))
|
|
|
|
} catch (e) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 23:43:12 +01:00
|
|
|
type ExampleType = [
|
|
|
|
string,
|
|
|
|
{
|
|
|
|
hbs: string
|
|
|
|
js: string
|
|
|
|
requiresHbsBody: boolean
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
export const getParsedManifest = () => {
|
2024-03-15 09:41:32 +01:00
|
|
|
const manifest: any = getManifest()
|
2024-02-02 12:02:09 +01:00
|
|
|
const collections = Object.keys(manifest)
|
2024-02-21 23:43:12 +01:00
|
|
|
|
2024-02-02 12:02:09 +01:00
|
|
|
const examples = collections.reduce((acc, collection) => {
|
2024-02-21 23:43:12 +01:00
|
|
|
const functions = Object.entries<{
|
|
|
|
example: string
|
|
|
|
requiresBlock: boolean
|
|
|
|
}>(manifest[collection])
|
|
|
|
.filter(
|
2024-03-20 12:46:39 +01:00
|
|
|
([, details]) =>
|
2024-02-21 23:43:12 +01:00
|
|
|
details.example?.split("->").map(x => x.trim()).length > 1
|
|
|
|
)
|
|
|
|
.map(([name, details]): ExampleType => {
|
2024-02-02 12:02:09 +01:00
|
|
|
const example = details.example
|
|
|
|
let [hbs, js] = example.split("->").map(x => x.trim())
|
|
|
|
|
|
|
|
// Trim 's
|
|
|
|
js = js.replace(/^'|'$/g, "")
|
2024-02-21 23:43:12 +01:00
|
|
|
let parsedExpected: string
|
2024-02-02 12:02:09 +01:00
|
|
|
if ((parsedExpected = tryParseJson(js))) {
|
|
|
|
if (Array.isArray(parsedExpected)) {
|
|
|
|
if (typeof parsedExpected[0] === "object") {
|
|
|
|
js = JSON.stringify(parsedExpected)
|
|
|
|
} else {
|
|
|
|
js = parsedExpected.join(",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const requiresHbsBody = details.requiresBlock
|
|
|
|
return [name, { hbs, js, requiresHbsBody }]
|
|
|
|
})
|
|
|
|
|
2024-02-21 23:43:12 +01:00
|
|
|
if (functions.length) {
|
2024-02-02 12:02:09 +01:00
|
|
|
acc[collection] = functions
|
|
|
|
}
|
|
|
|
return acc
|
2024-02-21 23:43:12 +01:00
|
|
|
}, {} as Record<string, ExampleType[]>)
|
2024-02-02 12:02:09 +01:00
|
|
|
|
|
|
|
return examples
|
|
|
|
}
|
|
|
|
|
2024-02-21 23:43:12 +01:00
|
|
|
export const runJsHelpersTests = ({
|
|
|
|
funcWrap,
|
|
|
|
testsToSkip,
|
|
|
|
}: {
|
|
|
|
funcWrap?: any
|
|
|
|
testsToSkip?: any
|
|
|
|
} = {}) => {
|
2024-03-15 09:41:32 +01:00
|
|
|
funcWrap = funcWrap || ((delegate: () => any) => delegate())
|
2024-02-02 12:02:09 +01:00
|
|
|
const manifest = getParsedManifest()
|
|
|
|
|
2024-03-15 09:41:32 +01:00
|
|
|
const processJS = (js: string, context: object | undefined) => {
|
2024-02-02 12:02:09 +01:00
|
|
|
return funcWrap(() => processStringSync(encodeJSBinding(js), context))
|
|
|
|
}
|
|
|
|
|
2024-03-15 09:41:32 +01:00
|
|
|
function escapeRegExp(string: string) {
|
2024-02-02 12:02:09 +01:00
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("can be parsed and run as js", () => {
|
2024-02-21 23:43:12 +01:00
|
|
|
const jsHelpers = getJsHelperList()!
|
2024-02-02 12:02:09 +01:00
|
|
|
const jsExamples = Object.keys(manifest).reduce((acc, v) => {
|
|
|
|
acc[v] = manifest[v].filter(([key]) => jsHelpers[key])
|
|
|
|
return acc
|
2024-02-21 23:59:46 +01:00
|
|
|
}, {} as typeof manifest)
|
2024-02-02 12:02:09 +01:00
|
|
|
|
|
|
|
describe.each(Object.keys(jsExamples))("%s", collection => {
|
|
|
|
const examplesToRun = jsExamples[collection]
|
2024-03-20 12:46:39 +01:00
|
|
|
.filter(([, { requiresHbsBody }]) => !requiresHbsBody)
|
2024-02-02 12:02:09 +01:00
|
|
|
.filter(([key]) => !testsToSkip?.includes(key))
|
|
|
|
|
|
|
|
examplesToRun.length &&
|
|
|
|
it.each(examplesToRun)("%s", async (_, { hbs, js }) => {
|
2024-03-15 09:41:32 +01:00
|
|
|
const context: any = {
|
|
|
|
double: (i: number) => i * 2,
|
|
|
|
isString: (x: any) => typeof x === "string",
|
2024-02-02 12:02:09 +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, '"'))
|
|
|
|
})
|
|
|
|
|
|
|
|
let convertedJs = convertToJS(hbs)
|
|
|
|
|
|
|
|
let result = await processJS(convertedJs, context)
|
|
|
|
result = result.replace(/ /g, " ")
|
|
|
|
expect(result).toEqual(js)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|