screen events

This commit is contained in:
Rory Powell 2022-04-08 10:55:39 +01:00
parent 75be1e031b
commit e725b1fb52
3 changed files with 51 additions and 5 deletions

View File

@ -69,6 +69,11 @@ jest.mock("../../../events", () => {
assigned: jest.fn(), assigned: jest.fn(),
unassigned: jest.fn(), unassigned: jest.fn(),
}, },
row: {},
screen: {
created: jest.fn(),
deleted: jest.fn(),
},
user: { user: {
created: jest.fn(), created: jest.fn(),
updated: jest.fn(), updated: jest.fn(),
@ -85,6 +90,9 @@ jest.mock("../../../events", () => {
passwordResetRequested: jest.fn(), passwordResetRequested: jest.fn(),
passwordReset: jest.fn(), passwordReset: jest.fn(),
}, },
serve: {},
table: {},
view: {},
} }
}) })

View File

@ -1,6 +1,7 @@
const { getScreenParams, generateScreenID } = require("../../db/utils") const { getScreenParams, generateScreenID } = require("../../db/utils")
const { AccessController } = require("@budibase/backend-core/roles") const { AccessController } = require("@budibase/backend-core/roles")
const { getAppDB } = require("@budibase/backend-core/context") const { getAppDB } = require("@budibase/backend-core/context")
const { events } = require("@budibase/backend-core")
exports.fetch = async ctx => { exports.fetch = async ctx => {
const db = getAppDB() const db = getAppDB()
@ -23,11 +24,17 @@ exports.save = async ctx => {
const db = getAppDB() const db = getAppDB()
let screen = ctx.request.body let screen = ctx.request.body
let eventFn
if (!screen._id) { if (!screen._id) {
screen._id = generateScreenID() screen._id = generateScreenID()
eventFn = events.screen.created
} }
const response = await db.put(screen) const response = await db.put(screen)
if (eventFn) {
eventFn(screen)
}
ctx.message = `Screen ${screen.name} saved.` ctx.message = `Screen ${screen.name} saved.`
ctx.body = { ctx.body = {
...screen, ...screen,
@ -38,7 +45,12 @@ exports.save = async ctx => {
exports.destroy = async ctx => { exports.destroy = async ctx => {
const db = getAppDB() const db = getAppDB()
await db.remove(ctx.params.screenId, ctx.params.screenRev) const id = ctx.params.screenId
const screen = await db.get(id)
await db.remove(id, ctx.params.screenRev)
events.screen.deleted(screen)
ctx.body = { ctx.body = {
message: "Screen deleted successfully", message: "Screen deleted successfully",
} }

View File

@ -1,6 +1,7 @@
const { checkBuilderEndpoint } = require("./utilities/TestFunctions") const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const setup = require("./utilities") const setup = require("./utilities")
const { basicScreen } = setup.structures const { basicScreen } = setup.structures
const { events } = require("@budibase/backend-core")
describe("/screens", () => { describe("/screens", () => {
let request = setup.getRequest() let request = setup.getRequest()
@ -35,16 +36,40 @@ describe("/screens", () => {
}) })
describe("save", () => { describe("save", () => {
it("should be able to save a screen", async () => { const saveScreen = async (screen) => {
const screenCfg = basicScreen()
const res = await request const res = await request
.post(`/api/screens`) .post(`/api/screens`)
.send(screenCfg) .send(screen)
.set(config.defaultHeaders()) .set(config.defaultHeaders())
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(200) .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._rev).toBeDefined()
expect(res.body.name).toEqual(screenCfg.name) 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 () => { it("should apply authorization to endpoint", async () => {
@ -64,6 +89,7 @@ describe("/screens", () => {
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
expect(res.body.message).toBeDefined() expect(res.body.message).toBeDefined()
expect(events.screen.deleted).toBeCalledTimes(1)
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {