diff --git a/packages/server/src/api/routes/tests/screen.spec.js b/packages/server/src/api/routes/tests/screen.spec.js deleted file mode 100644 index 481baac703..0000000000 --- a/packages/server/src/api/routes/tests/screen.spec.js +++ /dev/null @@ -1,103 +0,0 @@ -const { checkBuilderEndpoint } = require("./utilities/TestFunctions") -const setup = require("./utilities") -const { basicScreen } = setup.structures -const { events } = require("@budibase/backend-core") - -describe("/screens", () => { - let request = setup.getRequest() - let config = setup.getConfig() - let screen - - afterAll(setup.afterAll) - - beforeAll(async () => { - await config.init() - screen = await config.createScreen() - }) - - describe("fetch", () => { - it("should be able to create a layout", async () => { - const res = await request - .get(`/api/screens`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.length).toEqual(1) - expect(res.body.some(s => s._id === screen._id)).toEqual(true) - }) - - it("should apply authorization to endpoint", async () => { - await checkBuilderEndpoint({ - config, - method: "GET", - url: `/api/screens`, - }) - }) - }) - - describe("save", () => { - const saveScreen = async screen => { - const res = await request - .post(`/api/screens`) - .send(screen) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - return res - } - - it("should be able to create a screen", async () => { - jest.clearAllMocks() - - const screen = basicScreen() - const res = await saveScreen(screen) - - expect(res.body._rev).toBeDefined() - expect(res.body.name).toEqual(screen.name) - expect(events.screen.created).toBeCalledTimes(1) - }) - - it("should be able to update a screen", async () => { - const screen = basicScreen() - let res = await saveScreen(screen) - screen._id = res.body._id - screen._rev = res.body._rev - screen.name = "edit" - jest.clearAllMocks() - - res = await saveScreen(screen) - - expect(res.body._rev).toBeDefined() - expect(res.body.name).toEqual(screen.name) - expect(events.screen.created).not.toBeCalled() - }) - - it("should apply authorization to endpoint", async () => { - await checkBuilderEndpoint({ - config, - method: "POST", - url: `/api/screens`, - }) - }) - }) - - describe("destroy", () => { - it("should be able to delete the screen", async () => { - const res = await request - .delete(`/api/screens/${screen._id}/${screen._rev}`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.message).toBeDefined() - expect(events.screen.deleted).toBeCalledTimes(1) - }) - - it("should apply authorization to endpoint", async () => { - await checkBuilderEndpoint({ - config, - method: "DELETE", - url: `/api/screens/${screen._id}/${screen._rev}`, - }) - }) - }) -}) diff --git a/packages/server/src/api/routes/tests/screen.spec.ts b/packages/server/src/api/routes/tests/screen.spec.ts new file mode 100644 index 0000000000..73c3062058 --- /dev/null +++ b/packages/server/src/api/routes/tests/screen.spec.ts @@ -0,0 +1,94 @@ +import { checkBuilderEndpoint } from "./utilities/TestFunctions" +import * as setup from "./utilities" +import { events } from "@budibase/backend-core" +import { Screen } from "@budibase/types" + +const { basicScreen } = setup.structures + +describe("/screens", () => { + let config = setup.getConfig() + let screen: Screen + + afterAll(setup.afterAll) + + beforeAll(async () => { + await config.init() + screen = await config.createScreen() + }) + + describe("fetch", () => { + it("should be able to create a layout", async () => { + const screens = await config.api.screen.list({ status: 200 }) + expect(screens.length).toEqual(1) + expect(screens.some(s => s._id === screen._id)).toEqual(true) + }) + + it("should apply authorization to endpoint", async () => { + await checkBuilderEndpoint({ + config, + method: "GET", + url: `/api/screens`, + }) + }) + }) + + describe("save", () => { + const saveScreen = async (screen: Screen) => { + return await config.api.screen.save(screen, { status: 200 }) + } + + it("should be able to create a screen", async () => { + jest.clearAllMocks() + + const screen = basicScreen() + const responseScreen = await saveScreen(screen) + + expect(responseScreen._rev).toBeDefined() + expect(responseScreen.name).toEqual(screen.name) + expect(events.screen.created).toHaveBeenCalledTimes(1) + }) + + it("should be able to update a screen", async () => { + const screen = basicScreen() + let responseScreen = await saveScreen(screen) + screen._id = responseScreen._id + screen._rev = responseScreen._rev + screen.name = "edit" + jest.clearAllMocks() + + responseScreen = await saveScreen(screen) + + expect(responseScreen._rev).toBeDefined() + expect(responseScreen.name).toEqual(screen.name) + expect(events.screen.created).not.toHaveBeenCalled() + }) + + it("should apply authorization to endpoint", async () => { + await checkBuilderEndpoint({ + config, + method: "POST", + url: `/api/screens`, + }) + }) + }) + + describe("destroy", () => { + it("should be able to delete the screen", async () => { + const response = await config.api.screen.destroy( + screen._id!, + screen._rev!, + { status: 200 } + ) + expect(response.message).toBeDefined() + expect(events.screen.deleted).toHaveBeenCalledTimes(1) + }) + + it("should apply authorization to endpoint", async () => { + await checkBuilderEndpoint({ + config, + method: "DELETE", + url: `/api/screens/${screen._id}/${screen._rev}`, + }) + }) + }) +}) diff --git a/packages/server/src/tests/utilities/api/screen.ts b/packages/server/src/tests/utilities/api/screen.ts index c8d3e647d8..8b7d7f92c1 100644 --- a/packages/server/src/tests/utilities/api/screen.ts +++ b/packages/server/src/tests/utilities/api/screen.ts @@ -5,4 +5,27 @@ export class ScreenAPI extends TestAPI { list = async (expectations?: Expectations): Promise => { return await this._get(`/api/screens`, { expectations }) } + + save = async ( + screen: Screen, + expectations?: Expectations + ): Promise => { + return await this._post(`/api/screens`, { + expectations, + body: screen, + }) + } + + destroy = async ( + screenId: string, + screenRev: string, + expectations?: Expectations + ): Promise<{ message: string }> => { + return this._delete<{ message: string }>( + `/api/screens/${screenId}/${screenRev}`, + { + expectations, + } + ) + } }