Convert TableAPI.

This commit is contained in:
Sam Rose 2024-02-28 16:27:14 +00:00
parent 039df5d476
commit 3203cc3d72
No known key found for this signature in database
3 changed files with 81 additions and 59 deletions

View File

@ -831,7 +831,7 @@ describe("/tables", () => {
subtype: FieldSubtype.USERS, subtype: FieldSubtype.USERS,
}, },
}, },
{ expectStatus: 400 } { status: 400 }
) )
}) })
@ -846,7 +846,7 @@ describe("/tables", () => {
subtype: FieldSubtype.USERS, subtype: FieldSubtype.USERS,
}, },
}, },
{ expectStatus: 400 } { status: 400 }
) )
}) })
@ -861,7 +861,7 @@ describe("/tables", () => {
subtype: FieldSubtype.USERS, subtype: FieldSubtype.USERS,
}, },
}, },
{ expectStatus: 400 } { status: 400 }
) )
}) })
@ -880,7 +880,7 @@ describe("/tables", () => {
subtype: FieldSubtype.USERS, subtype: FieldSubtype.USERS,
}, },
}, },
{ expectStatus: 400 } { status: 400 }
) )
}) })
}) })

View File

@ -2,7 +2,7 @@ import TestConfiguration from "../TestConfiguration"
import { SuperTest, Test } from "supertest" import { SuperTest, Test } from "supertest"
export interface TestAPIOpts { export interface TestAPIOpts {
headers?: any headers?: Record<string, string | string[] | undefined>
status?: number status?: number
} }
@ -14,4 +14,67 @@ export abstract class TestAPI {
this.config = config this.config = config
this.request = config.request! this.request = config.request!
} }
protected _get = async <T>(
url: string,
opts: TestAPIOpts = {}
): Promise<T> => {
return await this._request<T>("get", url, undefined, opts)
}
protected _post = async <T>(
url: string,
body: Record<string, any>,
opts: TestAPIOpts = {}
): Promise<T> => {
return await this._request<T>("post", url, body, opts)
}
protected _put = async <T>(
url: string,
body: Record<string, any>,
opts: TestAPIOpts = {}
): Promise<T> => {
return await this._request<T>("put", url, body, opts)
}
protected _patch = async <T>(
url: string,
body: Record<string, any>,
opts: TestAPIOpts = {}
): Promise<T> => {
return await this._request<T>("patch", url, body, opts)
}
protected _delete = async <T>(
url: string,
body: Record<string, any>,
opts: TestAPIOpts = {}
): Promise<T> => {
return await this._request<T>("delete", url, body, opts)
}
protected _request = async <T>(
method: "get" | "post" | "put" | "patch" | "delete",
url: string,
body?: Record<string, any>,
opts: TestAPIOpts = {}
): Promise<T> => {
const { headers = {}, status = 200 } = opts
const response = await this.request[method](url)
.send(body)
.set(this.config.defaultHeaders())
.set(headers)
.expect("Content-Type", /json/)
if (response.status !== status) {
throw new Error(
`Expected status ${status} but got ${
response.status
} with body ${JSON.stringify(response.body)}`
)
}
return response.body as T
}
} }

View File

@ -5,74 +5,33 @@ import {
SaveTableResponse, SaveTableResponse,
Table, Table,
} from "@budibase/types" } from "@budibase/types"
import TestConfiguration from "../TestConfiguration" import { TestAPI, TestAPIOpts } from "./base"
import { TestAPI } from "./base"
export class TableAPI extends TestAPI { export class TableAPI extends TestAPI {
constructor(config: TestConfiguration) {
super(config)
}
save = async ( save = async (
data: SaveTableRequest, data: SaveTableRequest,
{ expectStatus } = { expectStatus: 200 } opts?: TestAPIOpts
): Promise<SaveTableResponse> => { ): Promise<SaveTableResponse> => {
const res = await this.request return await this._post<SaveTableResponse>("/api/tables", data, opts)
.post(`/api/tables`)
.send(data)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
if (res.status !== expectStatus) {
throw new Error(
`Expected status ${expectStatus} but got ${
res.status
} with body ${JSON.stringify(res.body)}`
)
}
return res.body
} }
fetch = async ( fetch = async (opts?: TestAPIOpts): Promise<Table[]> => {
{ expectStatus } = { expectStatus: 200 } return await this._get<Table[]>("/api/tables", opts)
): Promise<Table[]> => {
const res = await this.request
.get(`/api/tables`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(expectStatus)
return res.body
} }
get = async ( get = async (tableId: string, opts?: TestAPIOpts): Promise<Table> => {
tableId: string, return await this._get<Table>(`/api/tables/${tableId}`, opts)
{ expectStatus } = { expectStatus: 200 }
): Promise<Table> => {
const res = await this.request
.get(`/api/tables/${tableId}`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(expectStatus)
return res.body
} }
migrate = async ( migrate = async (
tableId: string, tableId: string,
data: MigrateRequest, data: MigrateRequest,
{ expectStatus } = { expectStatus: 200 } opts?: TestAPIOpts
): Promise<MigrateResponse> => { ): Promise<MigrateResponse> => {
const res = await this.request return await this._post<MigrateResponse>(
.post(`/api/tables/${tableId}/migrate`) `/api/tables/${tableId}/migrate`,
.send(data) data,
.set(this.config.defaultHeaders()) opts
if (res.status !== expectStatus) { )
throw new Error(
`Expected status ${expectStatus} but got ${
res.status
} with body ${JSON.stringify(res.body)}`
)
}
return res.body
} }
} }