2021-04-07 11:56:06 +02:00
|
|
|
const { processString } = require("../src/index.cjs")
|
2021-01-19 19:44:29 +01:00
|
|
|
|
|
|
|
describe("Handling context properties with spaces in their name", () => {
|
2021-01-20 19:12:16 +01:00
|
|
|
it("should allow through literal specifiers", async () => {
|
2021-02-02 19:01:41 +01:00
|
|
|
const output = await processString("test {{ [one thing] }}", {
|
2021-04-07 11:56:06 +02:00
|
|
|
"one thing": 1,
|
2021-01-20 19:12:16 +01:00
|
|
|
})
|
|
|
|
expect(output).toBe("test 1")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should convert to dot notation where required", async () => {
|
2021-02-02 19:01:41 +01:00
|
|
|
const output = await processString("test {{ one[0] }}", {
|
2021-04-07 11:56:06 +02:00
|
|
|
one: [2],
|
2021-01-20 19:12:16 +01:00
|
|
|
})
|
|
|
|
expect(output).toBe("test 2")
|
|
|
|
})
|
|
|
|
|
2021-01-20 14:32:15 +01:00
|
|
|
it("should be able to handle a property with a space in its name", async () => {
|
2021-01-22 18:57:38 +01:00
|
|
|
const output = await processString("hello my name is {{ [person name] }}", {
|
2021-01-19 19:44:29 +01:00
|
|
|
"person name": "Mike",
|
|
|
|
})
|
|
|
|
expect(output).toBe("hello my name is Mike")
|
|
|
|
})
|
|
|
|
|
2021-01-20 14:32:15 +01:00
|
|
|
it("should be able to handle an object with layers that requires escaping", async () => {
|
2021-02-02 19:01:41 +01:00
|
|
|
const output = await processString("testcase {{ thing.[one case] }}", {
|
|
|
|
thing: {
|
2021-04-07 11:56:06 +02:00
|
|
|
"one case": 1,
|
|
|
|
},
|
2021-01-19 19:44:29 +01:00
|
|
|
})
|
|
|
|
expect(output).toBe("testcase 1")
|
|
|
|
})
|
2022-05-04 18:36:30 +02:00
|
|
|
|
|
|
|
it("should allow the use of a", async () => {
|
|
|
|
const output = await processString("{{ a }}", { a: 1 })
|
|
|
|
expect(output).toEqual("1")
|
|
|
|
})
|
2021-01-21 13:08:57 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("attempt some complex problems", () => {
|
|
|
|
it("should be able to handle a very complex handlebars statement", async () => {
|
|
|
|
const context = {
|
|
|
|
"New Repeater": {
|
|
|
|
"Get Actors": {
|
2021-04-07 11:56:06 +02:00
|
|
|
first_name: "Bob",
|
|
|
|
last_name: "Bobert",
|
2021-01-21 13:08:57 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-04-07 11:56:06 +02:00
|
|
|
const hbs =
|
|
|
|
"{{ [New Repeater].[Get Actors].[first_name] }} {{ [New Repeater].[Get Actors].[last_name] }}"
|
2021-01-21 13:08:57 +01:00
|
|
|
const output = await processString(hbs, context)
|
|
|
|
expect(output).toBe("Bob Bobert")
|
|
|
|
})
|
2021-01-22 18:57:38 +01:00
|
|
|
|
|
|
|
it("should be able to process an odd string produced by builder", async () => {
|
|
|
|
const context = {
|
2021-04-07 11:56:06 +02:00
|
|
|
c306d140d7e854f388bae056db380a0eb: {
|
2021-02-02 19:01:41 +01:00
|
|
|
"one prop": "test",
|
2021-04-07 11:56:06 +02:00
|
|
|
},
|
2021-01-22 18:57:38 +01:00
|
|
|
}
|
2021-02-02 19:01:41 +01:00
|
|
|
const hbs = "null{{ [c306d140d7e854f388bae056db380a0eb].[one prop] }}"
|
2021-01-22 18:57:38 +01:00
|
|
|
const output = await processString(hbs, context)
|
|
|
|
expect(output).toBe("nulltest")
|
|
|
|
})
|
2021-04-07 11:56:06 +02:00
|
|
|
})
|
2022-02-07 18:56:01 +01:00
|
|
|
|
|
|
|
describe("check behaviour with newlines", () => {
|
|
|
|
const context = {
|
|
|
|
binding: `Hello
|
2023-11-20 15:36:55 +01:00
|
|
|
there`,
|
2022-02-07 18:56:01 +01:00
|
|
|
}
|
|
|
|
it("should escape new line to \\n with double brace", async () => {
|
|
|
|
const hbs = JSON.stringify({
|
2023-11-20 15:36:55 +01:00
|
|
|
body: "{{ binding }}",
|
2022-02-07 18:56:01 +01:00
|
|
|
})
|
|
|
|
const output = await processString(hbs, context, { escapeNewlines: true })
|
|
|
|
expect(JSON.parse(output).body).toBe(context.binding)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should work the same with triple brace", async () => {
|
|
|
|
const hbs = JSON.stringify({
|
2023-11-20 15:36:55 +01:00
|
|
|
body: "{{{ binding }}}",
|
2022-02-07 18:56:01 +01:00
|
|
|
})
|
|
|
|
const output = await processString(hbs, context, { escapeNewlines: true })
|
|
|
|
expect(JSON.parse(output).body).toBe(context.binding)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should still work with helpers disabled", async () => {
|
|
|
|
const hbs = JSON.stringify({
|
2023-11-20 15:36:55 +01:00
|
|
|
body: "{{ binding }}",
|
|
|
|
})
|
|
|
|
const output = await processString(hbs, context, {
|
|
|
|
escapeNewlines: true,
|
|
|
|
noHelpers: true,
|
2022-02-07 18:56:01 +01:00
|
|
|
})
|
|
|
|
expect(JSON.parse(output).body).toBe(context.binding)
|
|
|
|
})
|
|
|
|
})
|