1
0
Fork 0
mirror of synced 2024-07-08 15:56:23 +12:00

Typescript conversion for some old tests - were failing after refactor.

This commit is contained in:
mike12345567 2024-01-26 13:33:50 +00:00
parent c1bc2f7010
commit cc249e6696
2 changed files with 104 additions and 52 deletions

View file

@ -1,17 +1,57 @@
const TestConfig = require("../../tests/utilities/TestConfiguration")
const {
basicRow,
import TestConfig from "../../tests/utilities/TestConfiguration"
import {
basicLinkedRow,
basicRow,
basicTable,
} = require("../../tests/utilities/structures")
const LinkController = require("../linkedRows/LinkController").default
const { context } = require("@budibase/backend-core")
const { RelationshipType } = require("../../constants")
const { cloneDeep } = require("lodash/fp")
} from "../../tests/utilities/structures"
import LinkController from "../linkedRows/LinkController"
import { context } from "@budibase/backend-core"
import {
FieldType,
ManyToManyRelationshipFieldMetadata,
ManyToOneRelationshipFieldMetadata,
OneToManyRelationshipFieldMetadata,
RelationshipFieldMetadata,
RelationshipType,
Row,
Table,
} from "@budibase/types"
import { cloneDeep } from "lodash"
const baseColumn = {
type: FieldType.LINK,
fieldName: "",
tableId: "",
name: "",
}
function mockManyToManyColumn(): ManyToManyRelationshipFieldMetadata {
return <ManyToManyRelationshipFieldMetadata>{
...baseColumn,
through: "",
throughFrom: "",
throughTo: "",
relationshipType: RelationshipType.MANY_TO_MANY,
}
}
function mockManyToOneColumn(): ManyToOneRelationshipFieldMetadata {
return <ManyToOneRelationshipFieldMetadata>{
...baseColumn,
relationshipType: RelationshipType.MANY_TO_ONE,
}
}
function mockOneToManyColumn(): OneToManyRelationshipFieldMetadata {
return <OneToManyRelationshipFieldMetadata>{
...baseColumn,
relationshipType: RelationshipType.ONE_TO_MANY,
}
}
describe("test the link controller", () => {
let config = new TestConfig()
let table1, table2, appId
let table1: Table, table2: Table, appId: string
beforeAll(async () => {
const app = await config.init()
@ -30,9 +70,18 @@ describe("test the link controller", () => {
afterAll(config.end)
async function createLinkController(table, row = null, oldTable = null) {
async function createLinkController(
table: Table,
row?: Row,
oldTable?: Table
) {
return context.doInAppContext(appId, () => {
const linkConfig = {
const linkConfig: {
tableId?: string
table: Table
row?: Row
oldTable?: Table
} = {
tableId: table._id,
table,
}
@ -47,11 +96,11 @@ describe("test the link controller", () => {
}
async function createLinkedRow(linkField = "link", t1 = table1, t2 = table2) {
const row = await config.createRow(basicRow(t2._id))
const row = await config.createRow(basicRow(t2._id!))
const { _id } = await config.createRow(
basicLinkedRow(t1._id, row._id, linkField)
basicLinkedRow(t1._id!, row._id!, linkField)
)
return config.getRow(t1._id, _id)
return config.getRow(t1._id!, _id!)
}
it("should be able to confirm if two table schemas are equal", async () => {
@ -71,6 +120,7 @@ describe("test the link controller", () => {
it("should be able to check the relationship types across two fields", async () => {
const controller = await createLinkController(table1)
// empty case
//@ts-ignore
let output = controller.handleRelationshipType({}, {})
expect(output.linkedField.relationshipType).toEqual(
RelationshipType.MANY_TO_MANY
@ -79,8 +129,8 @@ describe("test the link controller", () => {
RelationshipType.MANY_TO_MANY
)
output = controller.handleRelationshipType(
{ relationshipType: RelationshipType.MANY_TO_MANY },
{}
mockManyToManyColumn(),
{} as any
)
expect(output.linkedField.relationshipType).toEqual(
RelationshipType.MANY_TO_MANY
@ -88,20 +138,14 @@ describe("test the link controller", () => {
expect(output.linkerField.relationshipType).toEqual(
RelationshipType.MANY_TO_MANY
)
output = controller.handleRelationshipType(
{ relationshipType: RelationshipType.MANY_TO_ONE },
{}
)
output = controller.handleRelationshipType(mockManyToOneColumn(), {} as any)
expect(output.linkedField.relationshipType).toEqual(
RelationshipType.ONE_TO_MANY
)
expect(output.linkerField.relationshipType).toEqual(
RelationshipType.MANY_TO_ONE
)
output = controller.handleRelationshipType(
{ relationshipType: RelationshipType.ONE_TO_MANY },
{}
)
output = controller.handleRelationshipType(mockOneToManyColumn(), {} as any)
expect(output.linkedField.relationshipType).toEqual(
RelationshipType.MANY_TO_ONE
)
@ -115,16 +159,16 @@ describe("test the link controller", () => {
const controller = await createLinkController(table1, row)
await context.doInAppContext(appId, async () => {
// get initial count
const beforeLinks = await controller.getRowLinkDocs(row._id)
const beforeLinks = await controller.getRowLinkDocs(row._id!)
await controller.rowDeleted()
let afterLinks = await controller.getRowLinkDocs(row._id)
let afterLinks = await controller.getRowLinkDocs(row._id!)
expect(beforeLinks.length).toEqual(1)
expect(afterLinks.length).toEqual(0)
})
})
it("shouldn't throw an error when deleting a row with no links", async () => {
const row = await config.createRow(basicRow(table1._id))
const row = await config.createRow(basicRow(table1._id!))
const controller = await createLinkController(table1, row)
await context.doInAppContext(appId, async () => {
let error
@ -142,12 +186,13 @@ describe("test the link controller", () => {
const copyTable = {
...table1,
}
//@ts-ignore
copyTable.schema.otherTableLink = {
type: "link",
type: FieldType.LINK,
fieldName: "link",
tableId: table2._id,
tableId: table2._id!,
}
let error
let error: any
try {
controller.validateTable(copyTable)
} catch (err) {
@ -166,7 +211,7 @@ describe("test the link controller", () => {
const controller = await createLinkController(table1, row)
await context.doInAppContext(appId, async () => {
await controller.rowSaved()
let links = await controller.getRowLinkDocs(row._id)
let links = await controller.getRowLinkDocs(row._id!)
expect(links.length).toEqual(0)
})
})
@ -186,7 +231,7 @@ describe("test the link controller", () => {
it("should be able to remove a linked field from a table", async () => {
await createLinkedRow()
await createLinkedRow("link2")
const controller = await createLinkController(table1, null, table1)
const controller = await createLinkController(table1, undefined, table1)
await context.doInAppContext(appId, async () => {
let before = await controller.getTableLinkDocs()
await controller.removeFieldFromTable("link")
@ -199,7 +244,8 @@ describe("test the link controller", () => {
it("should throw an error when overwriting a link column", async () => {
const update = cloneDeep(table1)
update.schema.link.relationshipType = RelationshipType.MANY_TO_ONE
const linkSchema = update.schema.link as ManyToOneRelationshipFieldMetadata
linkSchema.relationshipType = RelationshipType.MANY_TO_ONE
let error
try {
const controller = await createLinkController(update)
@ -215,7 +261,7 @@ describe("test the link controller", () => {
await createLinkedRow()
const newTable = cloneDeep(table1)
delete newTable.schema.link
const controller = await createLinkController(newTable, null, table1)
const controller = await createLinkController(newTable, undefined, table1)
await context.doInAppContext(appId, async () => {
await controller.tableUpdated()
const links = await controller.getTableLinkDocs()
@ -235,7 +281,7 @@ describe("test the link controller", () => {
let error
try {
// create another row to initiate the error
await config.createRow(basicLinkedRow(row.tableId, row.link[0]))
await config.createRow(basicLinkedRow(row.tableId!, row.link[0]))
} catch (err) {
error = err
}
@ -245,7 +291,7 @@ describe("test the link controller", () => {
it("should not error if a link being created doesn't exist", async () => {
let error
try {
await config.createRow(basicLinkedRow(table1._id, "invalid"))
await config.createRow(basicLinkedRow(table1._id!, "invalid"))
} catch (err) {
error = err
}
@ -255,10 +301,11 @@ describe("test the link controller", () => {
it("make sure auto column goes onto other row too", async () => {
const table = await config.createTable()
const tableCfg = basicTable()
//@ts-ignore
tableCfg.schema.link = {
type: "link",
type: FieldType.LINK,
fieldName: "link",
tableId: table._id,
tableId: table._id!,
name: "link",
autocolumn: true,
}
@ -269,10 +316,11 @@ describe("test the link controller", () => {
it("should be able to link to self", async () => {
const table = await config.createTable()
//@ts-ignore
table.schema.link = {
type: "link",
type: FieldType.LINK,
fieldName: "link",
tableId: table._id,
tableId: table._id!,
name: "link",
autocolumn: true,
}
@ -282,8 +330,9 @@ describe("test the link controller", () => {
it("should be able to remove a linked field from a table, even if the linked table does not exist", async () => {
await createLinkedRow()
await createLinkedRow("link2")
table1.schema["link"].tableId = "not_found"
const controller = await createLinkController(table1, null, table1)
const linkSchema = table1.schema["link"] as RelationshipFieldMetadata
linkSchema.tableId = "not_found"
const controller = await createLinkController(table1, undefined, table1)
await context.doInAppContext(appId, async () => {
let before = await controller.getTableLinkDocs()
await controller.removeFieldFromTable("link")

View file

@ -1,14 +1,15 @@
const TestConfig = require("../../tests/utilities/TestConfiguration")
const { basicTable } = require("../../tests/utilities/structures")
const linkUtils = require("../linkedRows/linkUtils")
const { context } = require("@budibase/backend-core")
import TestConfig from "../../tests/utilities/TestConfiguration"
import { basicTable } from "../../tests/utilities/structures"
import * as linkUtils from "../linkedRows/linkUtils"
import { context } from "@budibase/backend-core"
import { FieldType, RelationshipType, Table } from "@budibase/types"
describe("test link functionality", () => {
const config = new TestConfig()
let appId
let appId: string
describe("getLinkedTable", () => {
let table
let table: Table
beforeAll(async () => {
const app = await config.init()
appId = app.appId
@ -17,15 +18,15 @@ describe("test link functionality", () => {
it("should be able to retrieve a linked table from a list", async () => {
await context.doInAppContext(appId, async () => {
const retrieved = await linkUtils.getLinkedTable(table._id, [table])
const retrieved = await linkUtils.getLinkedTable(table._id!, [table])
expect(retrieved._id).toBe(table._id)
})
})
it("should be able to retrieve a table from DB and update list", async () => {
const tables = []
const tables: Table[] = []
await context.doInAppContext(appId, async () => {
const retrieved = await linkUtils.getLinkedTable(table._id, tables)
const retrieved = await linkUtils.getLinkedTable(table._id!, tables)
expect(retrieved._id).toBe(table._id)
expect(tables[0]).toBeDefined()
})
@ -35,9 +36,11 @@ describe("test link functionality", () => {
describe("getRelatedTableForField", () => {
let link = basicTable()
link.schema.link = {
name: "link",
relationshipType: RelationshipType.ONE_TO_MANY,
fieldName: "otherLink",
tableId: "tableID",
type: "link",
type: FieldType.LINK,
}
it("should get the field from the table directly", () => {