Test middlewate
This commit is contained in:
parent
eaa7d9bf81
commit
30c3c6e5ad
|
@ -0,0 +1,122 @@
|
||||||
|
import { generator } from "@budibase/backend-core/tests"
|
||||||
|
import { BBRequest, FieldType, Row, Table } from "@budibase/types"
|
||||||
|
import * as utils from "../../db/utils"
|
||||||
|
import trimViewRowInfoMiddleware from "../trimViewRowInfo"
|
||||||
|
|
||||||
|
jest.mock("../../sdk", () => ({
|
||||||
|
views: {
|
||||||
|
...jest.requireActual("../../sdk/app/views"),
|
||||||
|
get: jest.fn(),
|
||||||
|
},
|
||||||
|
tables: {
|
||||||
|
getTable: jest.fn(),
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
import sdk from "../../sdk"
|
||||||
|
import { BaseContext, Next } from "koa"
|
||||||
|
|
||||||
|
const mockGetView = sdk.views.get as jest.MockedFunction<typeof sdk.views.get>
|
||||||
|
const mockGetTable = sdk.tables.getTable as jest.MockedFunction<
|
||||||
|
typeof sdk.tables.getTable
|
||||||
|
>
|
||||||
|
|
||||||
|
class TestConfiguration {
|
||||||
|
next: Next
|
||||||
|
throw: (status: number, message: string) => never
|
||||||
|
middleware: typeof trimViewRowInfoMiddleware
|
||||||
|
params: Record<string, any>
|
||||||
|
request?: Pick<BBRequest<Row>, "body">
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.next = jest.fn()
|
||||||
|
this.throw = (_status: any, message: any) => {
|
||||||
|
throw new Error(message)
|
||||||
|
}
|
||||||
|
this.params = {}
|
||||||
|
|
||||||
|
this.middleware = trimViewRowInfoMiddleware
|
||||||
|
}
|
||||||
|
|
||||||
|
executeMiddleware(viewId: string, ctxRequestBody: Row) {
|
||||||
|
this.request = {
|
||||||
|
body: ctxRequestBody,
|
||||||
|
}
|
||||||
|
this.params.viewId = viewId
|
||||||
|
return this.middleware(
|
||||||
|
{
|
||||||
|
request: this.request as any,
|
||||||
|
next: this.next,
|
||||||
|
throw: this.throw as any,
|
||||||
|
params: this.params,
|
||||||
|
} as any,
|
||||||
|
this.next
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach() {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("trimViewRowInfo middleware", () => {
|
||||||
|
let config: TestConfiguration
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
config = new TestConfiguration()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
config.afterEach()
|
||||||
|
})
|
||||||
|
|
||||||
|
const table: Table = {
|
||||||
|
_id: utils.generateTableID(),
|
||||||
|
name: generator.word(),
|
||||||
|
type: "table",
|
||||||
|
schema: {
|
||||||
|
name: {
|
||||||
|
name: "name",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
},
|
||||||
|
age: {
|
||||||
|
name: "age",
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
},
|
||||||
|
address: {
|
||||||
|
name: "address",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("when no columns are defined, same data is returned", async () => {
|
||||||
|
mockGetView.mockResolvedValue({
|
||||||
|
version: 2,
|
||||||
|
id: generator.guid(),
|
||||||
|
name: generator.guid(),
|
||||||
|
tableId: generator.guid(),
|
||||||
|
})
|
||||||
|
mockGetTable.mockResolvedValue(table)
|
||||||
|
|
||||||
|
const viewId = utils.generateViewID(table._id!)
|
||||||
|
const data = {
|
||||||
|
_id: generator.guid(),
|
||||||
|
name: generator.name(),
|
||||||
|
age: generator.age(),
|
||||||
|
address: generator.address(),
|
||||||
|
}
|
||||||
|
|
||||||
|
await config.executeMiddleware(viewId, {
|
||||||
|
_viewId: viewId,
|
||||||
|
...data,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(config.request?.body).toEqual(data)
|
||||||
|
expect(config.params.tableId).toEqual(table._id)
|
||||||
|
})
|
||||||
|
})
|
|
@ -2,22 +2,23 @@ import { Ctx, Row } from "@budibase/types"
|
||||||
import * as utils from "../db/utils"
|
import * as utils from "../db/utils"
|
||||||
import sdk from "../sdk"
|
import sdk from "../sdk"
|
||||||
import { db } from "@budibase/backend-core"
|
import { db } from "@budibase/backend-core"
|
||||||
|
import { Next } from "koa"
|
||||||
|
|
||||||
export default () => async (ctx: Ctx<Row>, next: any) => {
|
export default async (ctx: Ctx<Row>, next: Next) => {
|
||||||
const { body } = ctx.request
|
const { body } = ctx.request
|
||||||
const { _viewId: viewId } = body
|
const { _viewId: viewId } = body
|
||||||
if (!viewId) {
|
if (!viewId) {
|
||||||
ctx.throw(400, "_viewId is required")
|
ctx.throw(400, "_viewId is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
const { tableId } = utils.extractViewInfoFromID(viewId)
|
const { tableId } = utils.extractViewInfoFromID(ctx.params.viewId)
|
||||||
const { _viewId, ...trimmedView } = await trimViewFields(
|
const { _viewId, ...trimmedView } = await trimViewFields(
|
||||||
viewId,
|
viewId,
|
||||||
tableId,
|
tableId,
|
||||||
body
|
body
|
||||||
)
|
)
|
||||||
ctx.request.body = trimmedView
|
ctx.request.body = trimmedView
|
||||||
ctx.params.tableId = body.tableId
|
ctx.params.tableId = tableId
|
||||||
|
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue