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

View file

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