1
0
Fork 0
mirror of synced 2024-09-12 23:43:09 +12:00

Fix tests

This commit is contained in:
Adria Navarro 2024-05-03 16:35:20 +02:00
parent d91292f532
commit 23d6c0dc3a
3 changed files with 119 additions and 10 deletions

View file

@ -14,6 +14,7 @@ import { cloneDeep } from "lodash/fp"
import {
processInputBBReference,
processInputBBReferences,
processOutputBBReference,
processOutputBBReferences,
} from "./bbReferenceProcessor"
import { isExternalTableID } from "../../integrations/utils"
@ -258,7 +259,7 @@ export async function outputProcessing<T extends Row[] | Row>(
column.type == FieldType.BB_REFERENCE_SINGLE
) {
for (let row of enriched) {
row[property] = await processOutputBBReferences(
row[property] = await processOutputBBReference(
row[property],
column.subtype
)

View file

@ -10,7 +10,9 @@ import {
import * as bbReferenceProcessor from "../bbReferenceProcessor"
jest.mock("../bbReferenceProcessor", (): typeof bbReferenceProcessor => ({
processInputBBReference: jest.fn(),
processInputBBReferences: jest.fn(),
processOutputBBReference: jest.fn(),
processOutputBBReferences: jest.fn(),
}))
@ -19,7 +21,64 @@ describe("rowProcessor - inputProcessing", () => {
jest.resetAllMocks()
})
it("processes BB references if on the schema and it's populated", async () => {
const processInputBBReferenceMock =
bbReferenceProcessor.processInputBBReference as jest.Mock
const processInputBBReferencesMock =
bbReferenceProcessor.processInputBBReferences as jest.Mock
it("processes single BB references if on the schema and it's populated", async () => {
const userId = generator.guid()
const table: Table = {
_id: generator.guid(),
name: "TestTable",
type: "table",
sourceId: INTERNAL_TABLE_SOURCE_ID,
sourceType: TableSourceType.INTERNAL,
schema: {
name: {
type: FieldType.STRING,
name: "name",
constraints: {
presence: true,
type: "string",
},
},
user: {
type: FieldType.BB_REFERENCE_SINGLE,
subtype: BBReferenceFieldSubType.USER,
name: "user",
constraints: {
presence: true,
type: "string",
},
},
},
}
const newRow = {
name: "Jack",
user: "123",
}
const user = structures.users.user()
processInputBBReferenceMock.mockResolvedValue(user)
const { row } = await inputProcessing(userId, table, newRow)
expect(bbReferenceProcessor.processInputBBReference).toHaveBeenCalledTimes(
1
)
expect(bbReferenceProcessor.processInputBBReference).toHaveBeenCalledWith(
"123",
"user"
)
expect(row).toEqual({ ...newRow, user })
})
it("processes multiple BB references if on the schema and it's populated", async () => {
const userId = generator.guid()
const table: Table = {
@ -56,9 +115,7 @@ describe("rowProcessor - inputProcessing", () => {
const user = structures.users.user()
;(
bbReferenceProcessor.processInputBBReferences as jest.Mock
).mockResolvedValue(user)
processInputBBReferencesMock.mockResolvedValue(user)
const { row } = await inputProcessing(userId, table, newRow)
@ -67,7 +124,6 @@ describe("rowProcessor - inputProcessing", () => {
)
expect(bbReferenceProcessor.processInputBBReferences).toHaveBeenCalledWith(
"123",
"bb_reference",
"user"
)

View file

@ -11,7 +11,9 @@ import { generator, structures } from "@budibase/backend-core/tests"
import * as bbReferenceProcessor from "../bbReferenceProcessor"
jest.mock("../bbReferenceProcessor", (): typeof bbReferenceProcessor => ({
processInputBBReference: jest.fn(),
processInputBBReferences: jest.fn(),
processOutputBBReference: jest.fn(),
processOutputBBReferences: jest.fn(),
}))
@ -20,10 +22,12 @@ describe("rowProcessor - outputProcessing", () => {
jest.resetAllMocks()
})
const processOutputBBReferenceMock =
bbReferenceProcessor.processOutputBBReference as jest.Mock
const processOutputBBReferencesMock =
bbReferenceProcessor.processOutputBBReferences as jest.Mock
it("fetches bb user references given a populated field", async () => {
it("fetches single user references given a populated field", async () => {
const table: Table = {
_id: generator.guid(),
name: "TestTable",
@ -40,7 +44,7 @@ describe("rowProcessor - outputProcessing", () => {
},
},
user: {
type: FieldType.BB_REFERENCE,
type: FieldType.BB_REFERENCE_SINGLE,
subtype: BBReferenceFieldSubType.USER,
name: "user",
constraints: {
@ -57,18 +61,66 @@ describe("rowProcessor - outputProcessing", () => {
}
const user = structures.users.user()
processOutputBBReferencesMock.mockResolvedValue(user)
processOutputBBReferenceMock.mockResolvedValue(user)
const result = await outputProcessing(table, row, { squash: false })
expect(result).toEqual({ name: "Jack", user })
expect(bbReferenceProcessor.processOutputBBReference).toHaveBeenCalledTimes(
1
)
expect(bbReferenceProcessor.processOutputBBReference).toHaveBeenCalledWith(
"123",
BBReferenceFieldSubType.USER
)
})
it("fetches users references given a populated field", async () => {
const table: Table = {
_id: generator.guid(),
name: "TestTable",
type: "table",
sourceId: INTERNAL_TABLE_SOURCE_ID,
sourceType: TableSourceType.INTERNAL,
schema: {
name: {
type: FieldType.STRING,
name: "name",
constraints: {
presence: true,
type: "string",
},
},
users: {
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER,
name: "users",
constraints: {
presence: false,
type: "string",
},
},
},
}
const row = {
name: "Jack",
users: "123",
}
const users = [structures.users.user()]
processOutputBBReferencesMock.mockResolvedValue(users)
const result = await outputProcessing(table, row, { squash: false })
expect(result).toEqual({ name: "Jack", users })
expect(
bbReferenceProcessor.processOutputBBReferences
).toHaveBeenCalledTimes(1)
expect(bbReferenceProcessor.processOutputBBReferences).toHaveBeenCalledWith(
"123",
FieldType.BB_REFERENCE,
BBReferenceFieldSubType.USER
)
})