budibase/packages/string-templates/test/javascript.spec.js

157 lines
3.9 KiB
JavaScript
Raw Normal View History

2024-02-09 10:33:09 +01:00
const vm = require("vm")
const {
processStringSync,
encodeJSBinding,
setJSRunner,
} = require("../src/index.js")
const { UUID_REGEX } = require("./constants")
2021-10-13 14:45:14 +02:00
const processJS = (js, context) => {
return processStringSync(encodeJSBinding(js), context)
}
2024-02-09 10:33:09 +01:00
describe("Javascript", () => {
beforeAll(() => {
setJSRunner((js, context) => {
return vm.runInNewContext(js, context, { timeout: 1000 })
})
2021-10-13 14:45:14 +02:00
})
2024-02-09 10:33:09 +01:00
describe("Test the JavaScript helper", () => {
it("should execute a simple expression", () => {
const output = processJS(`return 1 + 2`)
expect(output).toBe(3)
2021-10-13 14:45:14 +02:00
})
2024-02-09 10:33:09 +01:00
it("should be able to use primitive bindings", () => {
const output = processJS(`return $("foo")`, {
foo: "bar",
})
expect(output).toBe("bar")
2021-10-13 14:45:14 +02:00
})
2024-02-09 10:33:09 +01:00
it("should be able to use an object binding", () => {
const output = processJS(`return $("foo").bar`, {
foo: {
bar: "baz",
},
})
expect(output).toBe("baz")
2021-10-13 14:45:14 +02:00
})
2024-02-09 10:33:09 +01:00
it("should be able to use a complex object binding", () => {
const output = processJS(`return $("foo").bar[0].baz`, {
foo: {
bar: [
{
baz: "shazbat",
},
],
2021-10-13 14:45:14 +02:00
},
2024-02-09 10:33:09 +01:00
})
expect(output).toBe("shazbat")
2021-10-13 14:45:14 +02:00
})
2024-02-09 10:33:09 +01:00
it("should be able to use a deep binding", () => {
const output = processJS(`return $("foo.bar.baz")`, {
foo: {
bar: {
baz: "shazbat",
},
},
2024-02-09 10:33:09 +01:00
})
expect(output).toBe("shazbat")
})
2024-02-09 10:33:09 +01:00
it("should be able to return an object", () => {
const output = processJS(`return $("foo")`, {
foo: {
bar: {
baz: "shazbat",
},
},
})
expect(output.bar.baz).toBe("shazbat")
})
2024-02-09 10:33:09 +01:00
it("should be able to return an array", () => {
const output = processJS(`return $("foo")`, {
foo: ["a", "b", "c"],
})
expect(output[2]).toBe("c")
})
2024-02-09 10:33:09 +01:00
it("should be able to return null", () => {
const output = processJS(`return $("foo")`, {
foo: null,
})
expect(output).toBe(null)
})
2024-02-09 10:33:09 +01:00
it("should be able to return undefined", () => {
const output = processJS(`return $("foo")`, {
foo: undefined,
})
expect(output).toBe(undefined)
})
2024-02-09 10:33:09 +01:00
it("should be able to return 0", () => {
const output = processJS(`return $("foo")`, {
foo: 0,
})
expect(output).toBe(0)
})
2024-02-09 10:33:09 +01:00
it("should be able to return an empty string", () => {
const output = processJS(`return $("foo")`, {
foo: "",
})
expect(output).toBe("")
2021-10-13 14:45:14 +02:00
})
2024-02-09 10:33:09 +01:00
it("should be able to use a deep array binding", () => {
const output = processJS(`return $("foo.0.bar")`, {
foo: [
{
bar: "baz",
},
],
})
expect(output).toBe("baz")
})
2021-10-13 14:45:14 +02:00
2024-02-09 10:33:09 +01:00
it("should handle errors", () => {
const output = processJS(`throw "Error"`)
expect(output).toBe("Error while executing JS")
})
2024-02-09 10:33:09 +01:00
it("should timeout after one second", () => {
const output = processJS(`while (true) {}`)
expect(output).toBe("Timed out while executing JS")
})
2024-02-09 10:33:09 +01:00
it("should prevent access to the process global", () => {
const output = processJS(`return process`)
expect(output).toBe("Error while executing JS")
})
})
2024-02-09 10:33:09 +01:00
describe("check JS helpers", () => {
it("should error if using the format helper. not helpers.", () => {
const output = processJS(`return helper.toInt(4.3)`)
expect(output).toBe("Error while executing JS")
})
2024-02-09 10:33:09 +01:00
it("should be able to use toInt", () => {
const output = processJS(`return helpers.toInt(4.3)`)
expect(output).toBe(4)
})
it("should be able to use uuid", () => {
const output = processJS(`return helpers.uuid()`)
expect(output).toMatch(UUID_REGEX)
})
})
2023-11-20 15:36:55 +01:00
})