budibase/packages/server/tests/builder.spec.js

253 lines
7.7 KiB
JavaScript
Raw Normal View History

const testAppDef = require("../appPackages/testApp/appDefinition.json")
const testAccessLevels = require("../appPackages/testApp/access_levels.json")
const mainPage = require("../appPackages/testApp/pages/main/page.json")
const unauthenticatedPage = require("../appPackages/testApp/pages/unauthenticated/page.json")
const testComponents = require("../appPackages/testApp/customComponents/components.json")
const testMoreComponents = require("../appPackages/testApp/moreCustomComponents/components.json")
const statusCodes = require("../utilities/statusCodes")
const screen1 = require("../appPackages/testApp/pages/main/screens/screen1.json")
const screen2 = require("../appPackages/testApp/pages/main/screens/screen2.json")
const { readJSON, pathExists, unlink, readFile } = require("fs-extra")
const { getHashedCssPaths } = require("../utilities/builder/convertCssToFiles")
2020-02-11 18:23:25 +01:00
const listScreens = require("../utilities/builder/listScreens");
const app = require("./testApp")()
testComponents.textbox.name = `./customComponents/textbox`
testMoreComponents.textbox.name = `./moreCustomComponents/textbox`
2019-07-27 08:31:31 +02:00
beforeAll(async () => {
const testScreen = "./appPackages/testApp/pages/main/screens/newscreen.json"
const testScreenAfterMove =
"./appPackages/testApp/pages/main/screens/anotherscreen.json"
2019-07-27 08:31:31 +02:00
if (await pathExists(testScreen)) await unlink(testScreen)
if (await pathExists(testScreenAfterMove)) await unlink(testScreenAfterMove)
await app.start()
})
afterAll(async () => await app.destroy())
it("/apppackage should get appDefinition", async () => {
const { body } = await app
.get("/_builder/api/testApp/appPackage")
.expect(statusCodes.OK)
expect(body.appDefinition).toEqual(testAppDef)
})
it("/apppackage should get access levels", async () => {
const { body } = await app
.get("/_builder/api/testApp/appPackage")
.expect(statusCodes.OK)
expect(body.accessLevels).toEqual(testAccessLevels)
})
it("/apppackage should get pages", async () => {
const { body } = await app
.get("/_builder/api/testApp/appPackage")
.expect(statusCodes.OK)
expect(body.pages).toEqual({
main: mainPage,
unauthenticated: unauthenticatedPage,
})
})
it("/apppackage should get components", async () => {
const { body } = await app
.get("/_builder/api/testApp/appPackage")
.expect(statusCodes.OK)
expect(body.components.components["./customComponents/textbox"]).toBeDefined()
expect(
body.components.components["./moreCustomComponents/textbox"]
).toBeDefined()
expect(body.components.components["./customComponents/textbox"]).toEqual(
testComponents.textbox
)
expect(body.components.components["./moreCustomComponents/textbox"]).toEqual(
testMoreComponents.textbox
)
})
it("/pages/:pageName/screens should get screens", async () => {
const { body } = await app
.get("/_builder/api/testApp/pages/main/screens")
.expect(statusCodes.OK)
const expectedComponents = {
screen1: { ...screen1, name: "screen1" },
screen2: { ...screen2, name: "screen2" },
}
expect(body).toEqual(expectedComponents)
})
2019-07-26 18:08:59 +02:00
it("should be able to create new screen", async () => {
const newscreen = {
name: "newscreen",
props: {
_component: "@budibase/standard-component/div",
className: "something",
},
}
await app
.post("/_builder/api/testApp/pages/main/screen", newscreen)
.expect(statusCodes.OK)
const screenFile = "./appPackages/testApp/pages/main/screens/newscreen.json"
expect(await pathExists(screenFile)).toBe(true)
expect(await readJSON(screenFile)).toEqual(newscreen)
})
2019-07-26 18:08:59 +02:00
it("should be able to update screen", async () => {
const updatedscreen = {
name: "newscreen",
props: {
_component: "@budibase/standard-component/div",
className: "something else",
},
}
await app
.post("/_builder/api/testApp/pages/main/screen", updatedscreen)
.expect(statusCodes.OK)
const screenFile = "./appPackages/testApp/pages/main/screens/newscreen.json"
expect(await readJSON(screenFile)).toEqual(updatedscreen)
})
2019-07-26 18:08:59 +02:00
it("should be able to rename screen", async () => {
await app
.patch("/_builder/api/testApp/pages/main/screen", {
oldname: "newscreen",
newname: "anotherscreen",
})
.expect(statusCodes.OK)
2019-07-26 18:08:59 +02:00
const oldcomponentFile =
"./appPackages/testApp/pages/main/screens/newscreen.json"
const newcomponentFile =
"./appPackages/testApp/pages/main/screens/anotherscreen.json"
expect(await pathExists(oldcomponentFile)).toBe(false)
expect(await pathExists(newcomponentFile)).toBe(true)
})
2019-07-26 18:08:59 +02:00
it("should be able to delete screen", async () => {
await app
.delete("/_builder/api/testApp/pages/main/screen/anotherscreen")
.expect(statusCodes.OK)
const componentFile =
"./appPackages/testApp/pages/main/screens/anotherscreen.json"
expect(await pathExists(componentFile)).toBe(false)
})
2019-09-09 06:24:14 +02:00
it("/savePage should prepare all necessary client files", async () => {
const mainCss = "/*main page css*/"
mainPage._css = mainCss
const screen1Css = "/*screen1 css*/"
screen1._css = screen1Css
const screen2Css = "/*screen2 css*/"
screen2._css = screen2Css
await app
.post("/_builder/api/testApp/pages/main", {
appDefinition: testAppDef,
accessLevels: testAccessLevels,
page: mainPage,
uiFunctions: "{'1234':() => 'test return'}",
screens: [screen1, screen2],
2019-09-09 06:24:14 +02:00
})
.expect(statusCodes.OK)
const publicFolderMain = relative =>
"./appPackages/testApp/public/main" + relative
const cssDir = publicFolderMain("/css")
expect(await pathExists(publicFolderMain("/index.html"))).toBe(true)
expect(
await pathExists(publicFolderMain("/lib/customComponents/index.js"))
).toBe(true)
expect(
await pathExists(publicFolderMain("/lib/moreCustomComponents/index.js"))
).toBe(true)
expect(
await pathExists(
publicFolderMain(
"/lib/node_modules/@budibase/standard-components/dist/index.js"
)
)
).toBe(true)
const indexHtmlMain = await readFile(publicFolderMain("/index.html"), "utf8")
const pageCssPaths = getHashedCssPaths(cssDir, mainCss)
const screen1CssPaths = getHashedCssPaths(cssDir, screen1Css)
const screen2CssPaths = getHashedCssPaths(cssDir, screen2Css)
expect(await pathExists(publicFolderMain(pageCssPaths.url))).toBe(true)
const savedPageCss = await readFile(
publicFolderMain(pageCssPaths.url),
"utf8"
)
expect(savedPageCss).toEqual(mainCss)
expect(indexHtmlMain.includes(pageCssPaths.url)).toBe(true)
expect(await pathExists(publicFolderMain(screen1CssPaths.url))).toBe(true)
const savedScreen1Css = await readFile(
publicFolderMain(screen1CssPaths.url),
"utf8"
)
expect(savedScreen1Css).toEqual(screen1Css)
expect(indexHtmlMain.includes(screen1CssPaths.url)).toBe(true)
expect(await pathExists(publicFolderMain(screen2CssPaths.url))).toBe(true)
const savedScreen2Css = await readFile(
publicFolderMain(screen2CssPaths.url),
"utf8"
)
expect(savedScreen2Css).toEqual(screen2Css)
expect(indexHtmlMain.includes(screen2CssPaths.url)).toBe(true)
2019-09-09 06:24:14 +02:00
})
2020-02-11 18:23:25 +01:00
it("builds the correct stateOrigins object from a screen definition with no handlers", () => {
expect(listScreens.buildStateOrigins(screen1)).toEqual({});
});
it("builds the correct stateOrigins object from a screen definition with handlers", () => {
expect(listScreens.buildStateOrigins({
"name": "screen1",
"description": "",
"props": {
"_component": "@budibase/standard-components/div",
"className": "",
"onClick": [
{
"##eventHandlerType": "Set State",
"parameters": {
"path": "testKey",
"value": "value"
}
}
]
}
})).toEqual({
"testKey": {
"##eventHandlerType": "Set State",
"parameters": {
"path": "testKey",
"value": "value"
}
}
});
});