1
0
Fork 0
mirror of synced 2024-09-27 06:42:03 +12:00
budibase/packages/server/src/utilities/rowProcessor/tests/inputProcessing.spec.ts

187 lines
4.4 KiB
TypeScript
Raw Normal View History

2023-09-15 20:33:36 +12:00
import { inputProcessing } from ".."
import { generator, structures } from "@budibase/backend-core/tests"
import { FieldType, FieldTypeSubtypes, Table } from "@budibase/types"
import * as bbReferenceProcessor from "../bbReferenceProcessor"
jest.mock("../bbReferenceProcessor", (): typeof bbReferenceProcessor => ({
2023-09-15 20:54:43 +12:00
processInputBBReferences: jest.fn(),
2023-09-15 23:31:22 +12:00
processOutputBBReferences: jest.fn(),
2023-09-15 20:33:36 +12:00
}))
describe("rowProcessor - inputProcessing", () => {
2023-09-15 20:41:39 +12:00
beforeEach(() => {
jest.resetAllMocks()
})
it("processes BB references if on the schema and it's populated", async () => {
2023-09-15 20:33:36 +12:00
const userId = generator.guid()
const table: Table = {
_id: generator.guid(),
name: "TestTable",
type: "table",
schema: {
name: {
type: FieldType.STRING,
name: "name",
constraints: {
presence: true,
type: "string",
},
},
user: {
type: FieldType.BB_REFERENCE,
subtype: FieldTypeSubtypes.BB_REFERENCE.USER,
name: "user",
constraints: {
presence: true,
type: "string",
},
},
},
}
const newRow = {
name: "Jack",
user: "123",
}
const user = structures.users.user()
;(
2023-09-15 20:54:43 +12:00
bbReferenceProcessor.processInputBBReferences as jest.Mock
2023-09-15 20:33:36 +12:00
).mockResolvedValue(user)
const { row } = await inputProcessing(userId, table, newRow)
2023-09-15 20:54:43 +12:00
expect(bbReferenceProcessor.processInputBBReferences).toBeCalledTimes(1)
expect(bbReferenceProcessor.processInputBBReferences).toBeCalledWith(
2023-09-15 20:33:36 +12:00
"123",
"user"
)
expect(row).toEqual({ ...newRow, user })
})
2023-09-15 20:41:39 +12:00
2023-09-15 23:04:45 +12:00
it("it does not process BB references if on the schema but it is not populated", async () => {
2023-09-15 20:41:39 +12:00
const userId = generator.guid()
const table: Table = {
_id: generator.guid(),
name: "TestTable",
type: "table",
schema: {
name: {
type: FieldType.STRING,
name: "name",
constraints: {
presence: true,
type: "string",
},
},
user: {
type: FieldType.BB_REFERENCE,
subtype: FieldTypeSubtypes.BB_REFERENCE.USER,
name: "user",
constraints: {
presence: false,
type: "string",
},
},
},
}
const newRow = {
name: "Jack",
}
const { row } = await inputProcessing(userId, table, newRow)
2023-09-15 20:54:43 +12:00
expect(bbReferenceProcessor.processInputBBReferences).not.toBeCalled()
2023-09-15 20:41:39 +12:00
expect(row).toEqual({ ...newRow, user: undefined })
})
2023-09-15 23:04:45 +12:00
it.each([undefined, null, ""])(
"it does not process BB references the field is $%",
async userValue => {
const userId = generator.guid()
const table: Table = {
_id: generator.guid(),
name: "TestTable",
type: "table",
schema: {
name: {
type: FieldType.STRING,
name: "name",
constraints: {
presence: true,
type: "string",
},
},
user: {
type: FieldType.BB_REFERENCE,
subtype: FieldTypeSubtypes.BB_REFERENCE.USER,
name: "user",
constraints: {
presence: false,
type: "string",
},
},
},
}
const newRow = {
name: "Jack",
user: userValue,
}
const { row } = await inputProcessing(userId, table, newRow)
expect(bbReferenceProcessor.processInputBBReferences).not.toBeCalled()
expect(row).toEqual(newRow)
}
)
it("it does not process BB references if not in the schema", async () => {
2023-09-15 20:41:39 +12:00
const userId = generator.guid()
const table: Table = {
_id: generator.guid(),
name: "TestTable",
type: "table",
schema: {
name: {
type: FieldType.STRING,
name: "name",
constraints: {
presence: true,
type: "string",
},
},
user: {
type: FieldType.NUMBER,
name: "user",
constraints: {
presence: true,
type: "string",
},
},
},
}
const newRow = {
name: "Jack",
user: "123",
}
const { row } = await inputProcessing(userId, table, newRow)
2023-09-15 20:54:43 +12:00
expect(bbReferenceProcessor.processInputBBReferences).not.toBeCalled()
2023-09-15 20:41:39 +12:00
expect(row).toEqual({
name: "Jack",
user: 123,
})
})
2023-09-15 20:33:36 +12:00
})