Convert TableAPI.
This commit is contained in:
parent
039df5d476
commit
3203cc3d72
|
@ -831,7 +831,7 @@ describe("/tables", () => {
|
|||
subtype: FieldSubtype.USERS,
|
||||
},
|
||||
},
|
||||
{ expectStatus: 400 }
|
||||
{ status: 400 }
|
||||
)
|
||||
})
|
||||
|
||||
|
@ -846,7 +846,7 @@ describe("/tables", () => {
|
|||
subtype: FieldSubtype.USERS,
|
||||
},
|
||||
},
|
||||
{ expectStatus: 400 }
|
||||
{ status: 400 }
|
||||
)
|
||||
})
|
||||
|
||||
|
@ -861,7 +861,7 @@ describe("/tables", () => {
|
|||
subtype: FieldSubtype.USERS,
|
||||
},
|
||||
},
|
||||
{ expectStatus: 400 }
|
||||
{ status: 400 }
|
||||
)
|
||||
})
|
||||
|
||||
|
@ -880,7 +880,7 @@ describe("/tables", () => {
|
|||
subtype: FieldSubtype.USERS,
|
||||
},
|
||||
},
|
||||
{ expectStatus: 400 }
|
||||
{ status: 400 }
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -2,7 +2,7 @@ import TestConfiguration from "../TestConfiguration"
|
|||
import { SuperTest, Test } from "supertest"
|
||||
|
||||
export interface TestAPIOpts {
|
||||
headers?: any
|
||||
headers?: Record<string, string | string[] | undefined>
|
||||
status?: number
|
||||
}
|
||||
|
||||
|
@ -14,4 +14,67 @@ export abstract class TestAPI {
|
|||
this.config = config
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,74 +5,33 @@ import {
|
|||
SaveTableResponse,
|
||||
Table,
|
||||
} from "@budibase/types"
|
||||
import TestConfiguration from "../TestConfiguration"
|
||||
import { TestAPI } from "./base"
|
||||
import { TestAPI, TestAPIOpts } from "./base"
|
||||
|
||||
export class TableAPI extends TestAPI {
|
||||
constructor(config: TestConfiguration) {
|
||||
super(config)
|
||||
}
|
||||
|
||||
save = async (
|
||||
data: SaveTableRequest,
|
||||
{ expectStatus } = { expectStatus: 200 }
|
||||
opts?: TestAPIOpts
|
||||
): Promise<SaveTableResponse> => {
|
||||
const res = await this.request
|
||||
.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 await this._post<SaveTableResponse>("/api/tables", data, opts)
|
||||
}
|
||||
|
||||
return res.body
|
||||
fetch = async (opts?: TestAPIOpts): Promise<Table[]> => {
|
||||
return await this._get<Table[]>("/api/tables", opts)
|
||||
}
|
||||
|
||||
fetch = async (
|
||||
{ expectStatus } = { expectStatus: 200 }
|
||||
): 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 (
|
||||
tableId: string,
|
||||
{ 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
|
||||
get = async (tableId: string, opts?: TestAPIOpts): Promise<Table> => {
|
||||
return await this._get<Table>(`/api/tables/${tableId}`, opts)
|
||||
}
|
||||
|
||||
migrate = async (
|
||||
tableId: string,
|
||||
data: MigrateRequest,
|
||||
{ expectStatus } = { expectStatus: 200 }
|
||||
opts?: TestAPIOpts
|
||||
): Promise<MigrateResponse> => {
|
||||
const res = await this.request
|
||||
.post(`/api/tables/${tableId}/migrate`)
|
||||
.send(data)
|
||||
.set(this.config.defaultHeaders())
|
||||
if (res.status !== expectStatus) {
|
||||
throw new Error(
|
||||
`Expected status ${expectStatus} but got ${
|
||||
res.status
|
||||
} with body ${JSON.stringify(res.body)}`
|
||||
return await this._post<MigrateResponse>(
|
||||
`/api/tables/${tableId}/migrate`,
|
||||
data,
|
||||
opts
|
||||
)
|
||||
}
|
||||
return res.body
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue