@tsed/platform-test-sdk
Version:
Package to test platform adapter integration with Ts.ED
138 lines (137 loc) • 4.72 kB
JavaScript
import { __decorate, __metadata, __param } from "tslib";
import "@tsed/ajv";
import { Controller } from "@tsed/di";
import { PlatformTest } from "@tsed/platform-http/testing";
import { QueryParams } from "@tsed/platform-params";
import { Default, GenericOf, Generics, Get, Maximum, Minimum, Property } from "@tsed/schema";
import SuperTest from "supertest";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
class FindQuery {
}
__decorate([
Property(),
__metadata("design:type", Number)
], FindQuery.prototype, "a", void 0);
__decorate([
Property(),
__metadata("design:type", Number)
], FindQuery.prototype, "b", void 0);
let PaginationQuery = class PaginationQuery {
};
__decorate([
Minimum(0),
Default(0),
__metadata("design:type", Number)
], PaginationQuery.prototype, "offset", void 0);
__decorate([
Minimum(1),
Maximum(1000),
Default(50),
__metadata("design:type", Number)
], PaginationQuery.prototype, "limit", void 0);
__decorate([
Property("T"),
__metadata("design:type", Object)
], PaginationQuery.prototype, "where", void 0);
PaginationQuery = __decorate([
Generics("T")
], PaginationQuery);
let TestDeepQueryParamsCtrl = class TestDeepQueryParamsCtrl {
testScenario6(q, qs) {
return { q };
}
testScenario7(q) {
return { q };
}
};
__decorate([
Get("/scenario-1"),
__param(0, QueryParams()),
__param(0, GenericOf(FindQuery)),
__param(1, QueryParams()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PaginationQuery, Object]),
__metadata("design:returntype", void 0)
], TestDeepQueryParamsCtrl.prototype, "testScenario6", null);
__decorate([
Get("/scenario-2"),
__param(0, QueryParams("q")),
__param(0, GenericOf(FindQuery)),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PaginationQuery]),
__metadata("design:returntype", void 0)
], TestDeepQueryParamsCtrl.prototype, "testScenario7", null);
TestDeepQueryParamsCtrl = __decorate([
Controller("/deep-query-params")
], TestDeepQueryParamsCtrl);
export function testDeepQueryParams(options) {
let request;
beforeAll(PlatformTest.bootstrap(options.server, {
...options,
ajv: {
verbose: false
},
mount: {
"/rest": [TestDeepQueryParamsCtrl]
}
}));
beforeAll(() => {
request = SuperTest(PlatformTest.callback());
});
afterAll(PlatformTest.reset);
describe("Scenario1: DeepObject", () => {
const endpoint = "/rest/deep-query-params/scenario-1";
it("should return 0 when query is 0", async () => {
const response = await request.get(`${endpoint}?offset=0&limit=10&where[a]=0&where[b]=1`).expect(200);
expect(response.body).toEqual({
q: {
limit: 10,
offset: 0,
where: {
a: 0,
b: 1
}
}
});
});
});
describe("Scenario2: DeepObject", () => {
const endpoint = "/rest/deep-query-params/scenario-2";
it("should return the query value", async () => {
const response = await request.get(`${endpoint}?q[offset]=0&q[limit]=10&q[where][a]=0&q[where][b]=1`).expect(200);
expect(response.body).toEqual({
q: {
limit: 10,
offset: 0,
where: {
a: 0,
b: 1
}
}
});
});
it("should throw a bad request", async () => {
const response = await request.get(`${endpoint}?q[offset]=0&q[limit]=10&q[where][a]=ca&q[where][b]=1`).expect(400);
expect(response.body).toEqual({
errors: [
{
requestPath: "query",
data: "ca",
dataPath: ".where.a",
instancePath: "/where/a",
keyword: "type",
message: "must be number",
modelName: "PaginationQuery",
params: {
type: "number"
},
schemaPath: "#/definitions/FindQuery/properties/a/type"
}
],
message: 'Bad request on parameter "request.query.q".\nPaginationQuery.where.a must be number. Given value: "ca"',
name: "AJV_VALIDATION_ERROR",
status: 400
});
});
});
}