1
0
Fork 0
mirror of synced 2024-07-03 05:20:32 +12:00

Update string-templates tests

This commit is contained in:
Andrew Kingston 2021-04-07 10:56:06 +01:00
parent 7efcc29ba8
commit ff938e70b5
4 changed files with 41 additions and 35 deletions

View file

@ -4,19 +4,19 @@ const {
isValid, isValid,
makePropSafe, makePropSafe,
getManifest, getManifest,
} = require("../src/index") } = require("../src/index.cjs")
describe("Test that the string processing works correctly", () => { describe("Test that the string processing works correctly", () => {
it("should process a basic template string", async () => { it("should process a basic template string", async () => {
const output = await processString("templating is {{ adjective }}", { const output = await processString("templating is {{ adjective }}", {
adjective: "easy" adjective: "easy",
}) })
expect(output).toBe("templating is easy") expect(output).toBe("templating is easy")
}) })
it("should process a literal template", async () => { it("should process a literal template", async () => {
const output = await processString("derp is {{{ adjective }}}", { const output = await processString("derp is {{{ adjective }}}", {
adjective: "derp" adjective: "derp",
}) })
expect(output).toBe("derp is derp") expect(output).toBe("derp is derp")
}) })
@ -42,23 +42,29 @@ describe("Test that the string processing works correctly", () => {
describe("Test that the object processing works correctly", () => { describe("Test that the object processing works correctly", () => {
it("should be able to process an object with some template strings", async () => { it("should be able to process an object with some template strings", async () => {
const output = await processObject({ const output = await processObject(
first: "thing is {{ adjective }}", {
second: "thing is bad", first: "thing is {{ adjective }}",
third: "we are {{ adjective }} {{ noun }}", second: "thing is bad",
}, { third: "we are {{ adjective }} {{ noun }}",
adjective: "easy", },
noun: "people", {
}) adjective: "easy",
noun: "people",
}
)
expect(output.first).toBe("thing is easy") expect(output.first).toBe("thing is easy")
expect(output.second).toBe("thing is bad") expect(output.second).toBe("thing is bad")
expect(output.third).toBe("we are easy people") expect(output.third).toBe("we are easy people")
}) })
it("should be able to handle arrays of string templates", async () => { it("should be able to handle arrays of string templates", async () => {
const output = await processObject(["first {{ noun }}", "second {{ noun }}"], { const output = await processObject(
noun: "person" ["first {{ noun }}", "second {{ noun }}"],
}) {
noun: "person",
}
)
expect(output[0]).toBe("first person") expect(output[0]).toBe("first person")
expect(output[1]).toBe("second person") expect(output[1]).toBe("second person")
}) })
@ -107,6 +113,8 @@ describe("check manifest", () => {
it("should be able to retrieve the manifest", () => { it("should be able to retrieve the manifest", () => {
const manifest = getManifest() const manifest = getManifest()
expect(manifest.math).not.toBeNull() expect(manifest.math).not.toBeNull()
expect(manifest.math.abs.description).toBe("<p>Return the magnitude of <code>a</code>.</p>\n") expect(manifest.math.abs.description).toBe(
"<p>Return the magnitude of <code>a</code>.</p>\n"
)
}) })
}) })

View file

@ -1,18 +1,16 @@
const { const { processString } = require("../src/index.cjs")
processString,
} = require("../src/index")
describe("Handling context properties with spaces in their name", () => { describe("Handling context properties with spaces in their name", () => {
it("should allow through literal specifiers", async () => { it("should allow through literal specifiers", async () => {
const output = await processString("test {{ [one thing] }}", { const output = await processString("test {{ [one thing] }}", {
"one thing": 1 "one thing": 1,
}) })
expect(output).toBe("test 1") expect(output).toBe("test 1")
}) })
it("should convert to dot notation where required", async () => { it("should convert to dot notation where required", async () => {
const output = await processString("test {{ one[0] }}", { const output = await processString("test {{ one[0] }}", {
one: [2] one: [2],
}) })
expect(output).toBe("test 2") expect(output).toBe("test 2")
}) })
@ -27,8 +25,8 @@ describe("Handling context properties with spaces in their name", () => {
it("should be able to handle an object with layers that requires escaping", async () => { it("should be able to handle an object with layers that requires escaping", async () => {
const output = await processString("testcase {{ thing.[one case] }}", { const output = await processString("testcase {{ thing.[one case] }}", {
thing: { thing: {
"one case": 1 "one case": 1,
} },
}) })
expect(output).toBe("testcase 1") expect(output).toBe("testcase 1")
}) })
@ -39,24 +37,25 @@ describe("attempt some complex problems", () => {
const context = { const context = {
"New Repeater": { "New Repeater": {
"Get Actors": { "Get Actors": {
"first_name": "Bob", first_name: "Bob",
"last_name": "Bobert" last_name: "Bobert",
}, },
}, },
} }
const hbs = "{{ [New Repeater].[Get Actors].[first_name] }} {{ [New Repeater].[Get Actors].[last_name] }}" const hbs =
"{{ [New Repeater].[Get Actors].[first_name] }} {{ [New Repeater].[Get Actors].[last_name] }}"
const output = await processString(hbs, context) const output = await processString(hbs, context)
expect(output).toBe("Bob Bobert") expect(output).toBe("Bob Bobert")
}) })
it("should be able to process an odd string produced by builder", async () => { it("should be able to process an odd string produced by builder", async () => {
const context = { const context = {
"c306d140d7e854f388bae056db380a0eb": { c306d140d7e854f388bae056db380a0eb: {
"one prop": "test", "one prop": "test",
} },
} }
const hbs = "null{{ [c306d140d7e854f388bae056db380a0eb].[one prop] }}" const hbs = "null{{ [c306d140d7e854f388bae056db380a0eb].[one prop] }}"
const output = await processString(hbs, context) const output = await processString(hbs, context)
expect(output).toBe("nulltest") expect(output).toBe("nulltest")
}) })
}) })

View file

@ -1,4 +1,4 @@
const { processString, processObject, isValid } = require("../src/index") const { processString, processObject, isValid } = require("../src/index.cjs")
describe("test the custom helpers we have applied", () => { describe("test the custom helpers we have applied", () => {
it("should be able to use the object helper", async () => { it("should be able to use the object helper", async () => {

View file

@ -1,9 +1,8 @@
const { processString } = require("../src/index") const { processString } = require("../src/index.cjs")
describe("specific test case for whether or not full app template can still be rendered", () => { describe("specific test case for whether or not full app template can still be rendered", () => {
it("should be able to render the app template", async () => { it("should be able to render the app template", async () => {
const template = const template = `<!doctype html>
`<!doctype html>
<html> <html>
<head> <head>
{{{head}}} {{{head}}}
@ -16,7 +15,7 @@ describe("specific test case for whether or not full app template can still be r
const context = { const context = {
appId: "App1", appId: "App1",
head: "<title>App</title>", head: "<title>App</title>",
body: "<body><p>App things</p></body>" body: "<body><p>App things</p></body>",
} }
const output = await processString(template, context) const output = await processString(template, context)
expect(output).toBe(`<!doctype html> expect(output).toBe(`<!doctype html>
@ -30,4 +29,4 @@ describe("specific test case for whether or not full app template can still be r
<body><p>App things</p></body> <body><p>App things</p></body>
</html>`) </html>`)
}) })
}) })