Skip to content

Commit

Permalink
Improve unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
evandroabukamel committed Oct 15, 2019
1 parent 5ce6dbd commit 40a5be1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
23 changes: 22 additions & 1 deletion test/fetures/controller_inheritance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as supertest from "supertest";
import {
controller, httpMethod, httpGet, request,
response, requestParam, queryParam, requestHeaders,
httpDelete, httpPost, httpPut, requestBody
httpDelete, httpPost, httpPut, httpOptions, requestBody
} from "../../src/decorators";
import { cleanUpMetadata } from "../../src/utils";

Expand Down Expand Up @@ -65,6 +65,13 @@ function getDemoServer() {
return { status: `BASE DELETE! ${id}` };
}

@httpOptions("/:id")
public options(
@requestParam("id") id: string
) {
return { status: `BASE OPTIONS! ${id}` };
}

}

@controller("/api/v1/movies")
Expand Down Expand Up @@ -179,6 +186,20 @@ describe("Derived controller", () => {

});

it("Can access methods decorated with @httpOptions from parent", (done) => {

const server = getDemoServer();
const id = 5;

supertest(server).options(`/api/v1/movies/${id}`)
.expect(200)
.then(res => {
expect(res.body.status).to.eql(`BASE OPTIONS! ${id}`);
done();
});

});

it("Derived controller can have its own methods", (done) => {

const server = getDemoServer();
Expand Down
22 changes: 21 additions & 1 deletion test/framework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { injectable, Container } from "inversify";
import { interfaces, InversifyExpressServer } from "../src";
import {
controller, httpMethod, all, httpGet, httpPost, httpPut, httpPatch,
httpHead, httpDelete, request, response, params, requestParam,
httpHead, httpDelete, httpOptions, request, response, params, requestParam,
requestBody, queryParam, requestHeaders, cookies,
next, principal
} from "../src/decorators";
Expand Down Expand Up @@ -435,6 +435,26 @@ describe("Integration Tests:", () => {
});
});

it("should call method-level middleware correctly (OPTIONS)", (done) => {

@controller("/")
class TestController {
@httpOptions("/", spyA, spyB, spyC) public postTest(req: express.Request, res: express.Response) { res.send("OPTIONS"); }
}

server = new InversifyExpressServer(container);
let agent = supertest(server.build());

agent.options("/")
.expect(200, "OPTIONS", function () {
expect(spyA.calledOnce).to.eqls(true);
expect(spyB.calledOnce).to.eqls(true);
expect(spyC.calledOnce).to.eqls(true);
expect(result).to.equal("abc");
done();
});
});

it("should call method-level middleware correctly (ALL)", (done) => {

@controller("/")
Expand Down

0 comments on commit 40a5be1

Please sign in to comment.