2021-01-19 18:29:38 +01:00
|
|
|
const {
|
|
|
|
processObject,
|
|
|
|
processString,
|
2021-01-22 18:57:38 +01:00
|
|
|
isValid,
|
|
|
|
makePropSafe,
|
2021-01-29 15:35:37 +01:00
|
|
|
getManifest,
|
2021-04-07 11:56:06 +02:00
|
|
|
} = require("../src/index.cjs")
|
2021-01-19 18:29:38 +01:00
|
|
|
|
|
|
|
describe("Test that the string processing works correctly", () => {
|
2021-01-20 14:32:15 +01:00
|
|
|
it("should process a basic template string", async () => {
|
|
|
|
const output = await processString("templating is {{ adjective }}", {
|
2021-04-07 11:56:06 +02:00
|
|
|
adjective: "easy",
|
2021-01-19 18:29:38 +01:00
|
|
|
})
|
|
|
|
expect(output).toBe("templating is easy")
|
|
|
|
})
|
|
|
|
|
2021-01-21 13:08:57 +01:00
|
|
|
it("should process a literal template", async () => {
|
|
|
|
const output = await processString("derp is {{{ adjective }}}", {
|
2021-04-07 11:56:06 +02:00
|
|
|
adjective: "derp",
|
2021-01-21 13:08:57 +01:00
|
|
|
})
|
|
|
|
expect(output).toBe("derp is derp")
|
|
|
|
})
|
|
|
|
|
2021-01-20 14:32:15 +01:00
|
|
|
it("should fail gracefully when wrong type passed in", async () => {
|
2021-01-19 18:29:38 +01:00
|
|
|
let error = null
|
|
|
|
try {
|
2021-01-20 14:32:15 +01:00
|
|
|
await processString(null, null)
|
2021-01-19 18:29:38 +01:00
|
|
|
} catch (err) {
|
|
|
|
error = err
|
|
|
|
}
|
|
|
|
expect(error).not.toBeNull()
|
|
|
|
})
|
2021-01-21 14:48:23 +01:00
|
|
|
|
|
|
|
it("confirm that null properties are handled correctly", async () => {
|
|
|
|
const output = await processString("hello {{ name }} I am {{ name2 }}", {
|
|
|
|
name: undefined,
|
|
|
|
name2: null,
|
|
|
|
})
|
|
|
|
expect(output).toBe("hello I am ")
|
|
|
|
})
|
2021-01-19 18:29:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("Test that the object processing works correctly", () => {
|
2021-01-20 14:32:15 +01:00
|
|
|
it("should be able to process an object with some template strings", async () => {
|
2021-04-07 11:56:06 +02:00
|
|
|
const output = await processObject(
|
|
|
|
{
|
|
|
|
first: "thing is {{ adjective }}",
|
|
|
|
second: "thing is bad",
|
|
|
|
third: "we are {{ adjective }} {{ noun }}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adjective: "easy",
|
|
|
|
noun: "people",
|
|
|
|
}
|
|
|
|
)
|
2021-01-19 18:29:38 +01:00
|
|
|
expect(output.first).toBe("thing is easy")
|
|
|
|
expect(output.second).toBe("thing is bad")
|
|
|
|
expect(output.third).toBe("we are easy people")
|
|
|
|
})
|
|
|
|
|
2021-01-20 14:32:15 +01:00
|
|
|
it("should be able to handle arrays of string templates", async () => {
|
2021-04-07 11:56:06 +02:00
|
|
|
const output = await processObject(
|
|
|
|
["first {{ noun }}", "second {{ noun }}"],
|
|
|
|
{
|
|
|
|
noun: "person",
|
|
|
|
}
|
|
|
|
)
|
2021-01-19 18:29:38 +01:00
|
|
|
expect(output[0]).toBe("first person")
|
|
|
|
expect(output[1]).toBe("second person")
|
|
|
|
})
|
|
|
|
|
2021-01-20 14:32:15 +01:00
|
|
|
it("should fail gracefully when object passed in has cycles", async () => {
|
2021-01-19 18:29:38 +01:00
|
|
|
let error = null
|
|
|
|
try {
|
|
|
|
const innerObj = { a: "thing {{ a }}" }
|
|
|
|
innerObj.b = innerObj
|
2021-01-20 14:32:15 +01:00
|
|
|
await processObject(innerObj, { a: 1 })
|
2021-01-19 18:29:38 +01:00
|
|
|
} catch (err) {
|
|
|
|
error = err
|
|
|
|
}
|
|
|
|
expect(error).not.toBeNull()
|
|
|
|
})
|
|
|
|
|
2021-01-20 14:32:15 +01:00
|
|
|
it("should fail gracefully when wrong type is passed in", async () => {
|
2021-01-19 18:29:38 +01:00
|
|
|
let error = null
|
|
|
|
try {
|
2021-01-20 14:32:15 +01:00
|
|
|
await processObject(null, null)
|
2021-01-19 18:29:38 +01:00
|
|
|
} catch (err) {
|
|
|
|
error = err
|
|
|
|
}
|
|
|
|
expect(error).not.toBeNull()
|
|
|
|
})
|
2021-01-22 18:57:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("check the utility functions", () => {
|
|
|
|
it("should return false for an invalid template string", () => {
|
|
|
|
const valid = isValid("{{ table1.thing prop }}")
|
|
|
|
expect(valid).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should state this template is valid", () => {
|
|
|
|
const valid = isValid("{{ thing }}")
|
|
|
|
expect(valid).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should make a property safe", () => {
|
|
|
|
const property = makePropSafe("thing")
|
|
|
|
expect(property).toEqual("[thing]")
|
|
|
|
})
|
2021-01-29 15:35:37 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("check manifest", () => {
|
|
|
|
it("should be able to retrieve the manifest", () => {
|
|
|
|
const manifest = getManifest()
|
|
|
|
expect(manifest.math).not.toBeNull()
|
2021-04-07 11:56:06 +02:00
|
|
|
expect(manifest.math.abs.description).toBe(
|
|
|
|
"<p>Return the magnitude of <code>a</code>.</p>\n"
|
|
|
|
)
|
2021-01-29 15:35:37 +01:00
|
|
|
})
|
2021-04-07 11:56:06 +02:00
|
|
|
})
|