Expanding test cases with a few more examples from queries, automations and formulas.
This commit is contained in:
parent
1484170a22
commit
6543207974
|
@ -71,4 +71,29 @@ describe("Test isolated vm directly", () => {
|
||||||
expect(result.length).toBe(1)
|
expect(result.length).toBe(1)
|
||||||
expect(result[0].imageLinks.length).toBe(6)
|
expect(result[0].imageLinks.length).toBe(6)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should handle automation script example", () => {
|
||||||
|
const context = {
|
||||||
|
steps: [{}, { response: "hello" }, { items: [{ rows: [{ a: 1 }] }] }],
|
||||||
|
}
|
||||||
|
const result = runJSWithIsolatedVM(
|
||||||
|
`const queryResults = steps[2].items;
|
||||||
|
|
||||||
|
const intervals = steps[1].response;
|
||||||
|
const whereNoItemsReturned = [];
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
for (let queryResult of queryResults) {
|
||||||
|
if (queryResult.rows.length === 0) {
|
||||||
|
whereNoItemsReturned.push(intervals[index]);
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return whereNoItemsReturned;
|
||||||
|
`,
|
||||||
|
context
|
||||||
|
)
|
||||||
|
expect(result).toEqual([])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import { validate as isValidUUID } from "uuid"
|
import { validate as isValidUUID } from "uuid"
|
||||||
import { processStringSync, encodeJSBinding } from "@budibase/string-templates"
|
import { processStringSync, encodeJSBinding } from "@budibase/string-templates"
|
||||||
import fs from "fs"
|
|
||||||
import path from "path"
|
|
||||||
|
|
||||||
const { runJsHelpersTests } = require("@budibase/string-templates/test/utils")
|
const { runJsHelpersTests } = require("@budibase/string-templates/test/utils")
|
||||||
|
|
||||||
|
@ -9,7 +7,9 @@ import tk from "timekeeper"
|
||||||
import { init } from ".."
|
import { init } from ".."
|
||||||
import TestConfiguration from "../../tests/utilities/TestConfiguration"
|
import TestConfiguration from "../../tests/utilities/TestConfiguration"
|
||||||
|
|
||||||
tk.freeze("2021-01-21T12:00:00")
|
const DATE = "2021-01-21T12:00:00"
|
||||||
|
|
||||||
|
tk.freeze(DATE)
|
||||||
|
|
||||||
describe("jsRunner (using isolated-vm)", () => {
|
describe("jsRunner (using isolated-vm)", () => {
|
||||||
const config = new TestConfiguration()
|
const config = new TestConfiguration()
|
||||||
|
@ -92,7 +92,7 @@ describe("jsRunner (using isolated-vm)", () => {
|
||||||
|
|
||||||
it("handle test case 2", async () => {
|
it("handle test case 2", async () => {
|
||||||
const context = {
|
const context = {
|
||||||
"Purchase Date": "2021-01-21T12:00:00",
|
"Purchase Date": DATE,
|
||||||
}
|
}
|
||||||
const result = await processJS(
|
const result = await processJS(
|
||||||
`
|
`
|
||||||
|
@ -147,9 +147,10 @@ describe("jsRunner (using isolated-vm)", () => {
|
||||||
|
|
||||||
it("should handle test case 4", async () => {
|
it("should handle test case 4", async () => {
|
||||||
const context = {
|
const context = {
|
||||||
"Time Sheets": ["a", "b"]
|
"Time Sheets": ["a", "b"],
|
||||||
}
|
}
|
||||||
const result = await processJS(`
|
const result = await processJS(
|
||||||
|
`
|
||||||
let hours = 0
|
let hours = 0
|
||||||
if (($("[Time Sheets]") != null) == true){
|
if (($("[Time Sheets]") != null) == true){
|
||||||
for (i = 0; i < $("[Time Sheets]").length; i++){
|
for (i = 0; i < $("[Time Sheets]").length; i++){
|
||||||
|
@ -161,9 +162,188 @@ describe("jsRunner (using isolated-vm)", () => {
|
||||||
if (($("[Time Sheets]") != null) == false){
|
if (($("[Time Sheets]") != null) == false){
|
||||||
return hours
|
return hours
|
||||||
}
|
}
|
||||||
`, context)
|
`,
|
||||||
|
context
|
||||||
|
)
|
||||||
expect(result).toBeDefined()
|
expect(result).toBeDefined()
|
||||||
expect(result).toBe("0ab")
|
expect(result).toBe("0ab")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should handle test case 5", async () => {
|
||||||
|
const context = {
|
||||||
|
change: JSON.stringify({ a: 1, primaryDisplay: "a" }),
|
||||||
|
previous: JSON.stringify({ a: 2, primaryDisplay: "b" }),
|
||||||
|
}
|
||||||
|
const result = await processJS(
|
||||||
|
`
|
||||||
|
let change = $("[change]") ? JSON.parse($("[change]")) : {}
|
||||||
|
let previous = $("[previous]") ? JSON.parse($("[previous]")) : {}
|
||||||
|
|
||||||
|
function simplifyLink(originalKey, value, parent) {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
if (value.filter(item => Object.keys(item || {}).includes("primaryDisplay")).length > 0) {
|
||||||
|
parent[originalKey] = value.map(link => link.primaryDisplay)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let entry of Object.entries(change)) {
|
||||||
|
simplifyLink(entry[0], entry[1], change)
|
||||||
|
}
|
||||||
|
for (let entry of Object.entries(previous)) {
|
||||||
|
simplifyLink(entry[0], entry[1], previous)
|
||||||
|
}
|
||||||
|
|
||||||
|
let diff = Object.fromEntries(Object.entries(change).filter(([k, v]) => previous[k]?.toString() !== v?.toString()))
|
||||||
|
|
||||||
|
delete diff.audit_change
|
||||||
|
delete diff.audit_previous
|
||||||
|
delete diff._id
|
||||||
|
delete diff._rev
|
||||||
|
delete diff.tableId
|
||||||
|
delete diff.audit
|
||||||
|
|
||||||
|
for (let entry of Object.entries(diff)) {
|
||||||
|
simplifyLink(entry[0], entry[1], diff)
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSON.stringify(change)?.replaceAll(",\\"", ",\\n\\t\\"").replaceAll("{\\"", "{\\n\\t\\"").replaceAll("}", "\\n}")
|
||||||
|
`,
|
||||||
|
context
|
||||||
|
)
|
||||||
|
expect(result).toBe(`{\n\t"a":1,\n\t"primaryDisplay":"a"\n}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle test case 6", async () => {
|
||||||
|
const context = {
|
||||||
|
"Join Date": DATE,
|
||||||
|
}
|
||||||
|
const result = await processJS(
|
||||||
|
`
|
||||||
|
var rate = 5;
|
||||||
|
var today = new Date();
|
||||||
|
|
||||||
|
// comment
|
||||||
|
function monthDiff(dateFrom, dateTo) {
|
||||||
|
return dateTo.getMonth() - dateFrom.getMonth() +
|
||||||
|
(12 * (dateTo.getFullYear() - dateFrom.getFullYear()))
|
||||||
|
}
|
||||||
|
var serviceMonths = monthDiff( new Date($("[Join Date]")), today);
|
||||||
|
var serviceYears = serviceMonths / 12;
|
||||||
|
|
||||||
|
if (serviceYears >= 1 && serviceYears < 5){
|
||||||
|
rate = 10;
|
||||||
|
}
|
||||||
|
if (serviceYears >= 5 && serviceYears < 10){
|
||||||
|
rate = 15;
|
||||||
|
}
|
||||||
|
if (serviceYears >= 10){
|
||||||
|
rate = 15;
|
||||||
|
rate += 0.5 * (Number(serviceYears.toFixed(0)) - 10);
|
||||||
|
}
|
||||||
|
return rate;
|
||||||
|
`,
|
||||||
|
context
|
||||||
|
)
|
||||||
|
expect(result).toBe(10)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle test case 7", async () => {
|
||||||
|
const context = {
|
||||||
|
"P I": "Pass",
|
||||||
|
"PA I": "Pass",
|
||||||
|
"F I": "Fail",
|
||||||
|
"V I": "Pass",
|
||||||
|
}
|
||||||
|
const result = await processJS(
|
||||||
|
`if (($("[P I]") == "Pass") == true)
|
||||||
|
if (($("[ P I]") == "Pass") == true)
|
||||||
|
if (($("[F I]") == "Pass") == true)
|
||||||
|
if (($("[V I]") == "Pass") == true)
|
||||||
|
{return "Pass"}
|
||||||
|
|
||||||
|
if (($("[PA I]") == "Fail") == true)
|
||||||
|
{return "Fail"}
|
||||||
|
if (($("[ P I]") == "Fail") == true)
|
||||||
|
{return "Fail"}
|
||||||
|
if (($("[F I]") == "Fail") == true)
|
||||||
|
{return "Fail"}
|
||||||
|
if (($("[V I]") == "Fail") == true)
|
||||||
|
{return "Fail"}
|
||||||
|
|
||||||
|
else
|
||||||
|
{return ""}`,
|
||||||
|
context
|
||||||
|
)
|
||||||
|
expect(result).toBe("Fail")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle test case 8", async () => {
|
||||||
|
const context = {
|
||||||
|
"T L": [{ Hours: 10 }],
|
||||||
|
"B H": 50,
|
||||||
|
}
|
||||||
|
const result = await processJS(
|
||||||
|
`var totalHours = 0;
|
||||||
|
if (($("[T L]") != null) == true){
|
||||||
|
for (let i = 0; i < ($("[T L]").length); i++){
|
||||||
|
var individualHours = "T L." + i + ".Hours";
|
||||||
|
var hoursNum = Number($(individualHours));
|
||||||
|
totalHours += hoursNum;
|
||||||
|
}
|
||||||
|
return totalHours.toFixed(2);
|
||||||
|
}
|
||||||
|
if (($("[T L]") != null) == false) {
|
||||||
|
return totalHours.toFixed(2);
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
context
|
||||||
|
)
|
||||||
|
expect(result).toBe("10.00")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle test case 9", async () => {
|
||||||
|
const context = {
|
||||||
|
"T L": [{ Hours: 10 }],
|
||||||
|
"B H": 50,
|
||||||
|
}
|
||||||
|
const result = await processJS(
|
||||||
|
`var totalHours = 0;
|
||||||
|
if (($("[T L]") != null) == true){
|
||||||
|
for (let i = 0; i < ($("[T L]").length); i++){
|
||||||
|
var individualHours = "T L." + i + ".Hours";
|
||||||
|
var hoursNum = Number($(individualHours));
|
||||||
|
totalHours += hoursNum;
|
||||||
|
}
|
||||||
|
return ($("[B H]") - totalHours).toFixed(2);
|
||||||
|
}
|
||||||
|
if (($("[T L]") != null) == false) {
|
||||||
|
return ($("[B H]") - totalHours).toFixed(2);
|
||||||
|
}`,
|
||||||
|
context
|
||||||
|
)
|
||||||
|
expect(result).toBe("40.00")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle test case 10", async () => {
|
||||||
|
const context = {
|
||||||
|
"F F": [{ "F S": 10 }],
|
||||||
|
}
|
||||||
|
const result = await processJS(
|
||||||
|
`var rating = 0;
|
||||||
|
|
||||||
|
if ($("[F F]") != null){
|
||||||
|
for (i = 0; i < $("[F F]").length; i++){
|
||||||
|
var individualRating = $("F F." + i + ".F S");
|
||||||
|
rating += individualRating;
|
||||||
|
}
|
||||||
|
rating = (rating / $("[F F]").length);
|
||||||
|
}
|
||||||
|
return rating;
|
||||||
|
`,
|
||||||
|
context
|
||||||
|
)
|
||||||
|
expect(result).toBe(10)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue