Extract viewapi to its own file

This commit is contained in:
Adria Navarro 2023-07-18 12:56:24 +02:00
parent 7140df6ed3
commit 1e6a65d4e9
5 changed files with 85 additions and 24 deletions

View File

@ -26,13 +26,12 @@ function priceTable(): Table {
describe("/v2/views", () => {
const request = setup.getRequest()
const config = setup.getConfig()
let table: Table
afterAll(setup.afterAll)
beforeAll(async () => {
await config.init()
table = await config.createTable(priceTable())
await config.createTable(priceTable())
})
describe("fetch", () => {

View File

@ -54,6 +54,8 @@ import {
} from "@budibase/types"
import { BUILTIN_ROLE_IDS } from "@budibase/backend-core/src/security/roles"
import API from "./api"
type DefaultUserValues = {
globalUserId: string
email: string
@ -80,6 +82,7 @@ class TestConfiguration {
datasource: any
tenantId?: string
defaultUserValues: DefaultUserValues
api: API
constructor(openServer = true) {
if (openServer) {
@ -95,6 +98,8 @@ class TestConfiguration {
this.appId = null
this.allApps = []
this.defaultUserValues = this.populateDefaultUserValues()
this.api = new API(this)
}
populateDefaultUserValues(): DefaultUserValues {
@ -635,28 +640,6 @@ class TestConfiguration {
return this._req(view, null, controllers.view.v1.save)
}
api = {
viewV2: {
create: async (config?: Partial<ViewV2>) => {
if (!this.table) {
throw "Test requires table to be configured."
}
const view = {
tableId: this.table._id,
name: generator.guid(),
...config,
}
const result = await this._req(view, null, controllers.view.v2.save)
return result.data
},
get: (viewId: string): supertest.Test => {
return this.request!.get(`/api/v2/views/${viewId}`)
.set(this.defaultHeaders())
.expect("Content-Type", /json/)
},
},
}
// AUTOMATION
async createAutomation(config?: any) {

View File

@ -0,0 +1,17 @@
import TestConfiguration from "../TestConfiguration"
import { SuperTest, Test } from "supertest"
export interface TestAPIOpts {
headers?: any
status?: number
}
export abstract class TestAPI {
config: TestConfiguration
request: SuperTest<Test>
protected constructor(config: TestConfiguration) {
this.config = config
this.request = config.request!
}
}

View File

@ -0,0 +1,10 @@
import TestConfiguration from "../TestConfiguration"
import { ViewV2API } from "./viewV2"
export default class API {
viewV2: ViewV2API
constructor(config: TestConfiguration) {
this.viewV2 = new ViewV2API(config)
}
}

View File

@ -0,0 +1,52 @@
import { Account, AccountMetadata, ViewV2 } from "@budibase/types"
import TestConfiguration from "../TestConfiguration"
import { TestAPI } from "./base"
import { generator } from "@budibase/backend-core/tests"
import supertest from "supertest"
export class ViewV2API extends TestAPI {
constructor(config: TestConfiguration) {
super(config)
}
create = async (viewData?: Partial<ViewV2>) => {
if (!this.config.table) {
throw "Test requires table to be configured."
}
const view = {
tableId: this.config.table._id,
name: generator.guid(),
...viewData,
}
const result = await this.request
.post(`/api/v2/views`)
.send(view)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
return result.body.data as ViewV2
}
get = (viewId: string): supertest.Test => {
return this.request
.get(`/api/v2/views/${viewId}`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
}
// },
// }
// saveMetadata = async (account: Account) => {
// const res = await this.request
// .put(`/api/system/accounts/${account.accountId}/metadata`)
// .send(account)
// .set(this.config.internalAPIHeaders())
// .expect("Content-Type", /json/)
// .expect(200)
// return res.body as AccountMetadata
// }
// destroyMetadata = (accountId: string) => {
// return this.request
// .del(`/api/system/accounts/${accountId}/metadata`)
// .set(this.config.internalAPIHeaders())
// }
}