Skip to content

Commit dd4e9f0

Browse files
Unit tests to secure changes of MetadataBuilder::createActions method, link https://github.com/typestack/routing-controllers/pull/301/files
1 parent d0793f2 commit dd4e9f0

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

sample/sample17-controllers-inheritance/controllers/ProductController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {MockedRepository} from "../repository/MockedRepository";
55
const domain = "product";
66

77
@Controller(`/${domain}`)
8-
export class CategoryController extends AbstractControllerTemplate {
8+
export class ProductController extends AbstractControllerTemplate {
99
protected constructor() {
1010
super();
1111

src/metadata-builder/MetadataBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class MetadataBuilder {
2424
/**
2525
* Builds controller metadata from a registered controller metadata args.
2626
*/
27-
buildControllerMetadata(classes?: Function[]) {
27+
buildControllerMetadata(classes?: Function[]): ControllerMetadata[] {
2828
return this.createControllers(classes);
2929
}
3030

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import "reflect-metadata";
2+
import {MetadataBuilder} from "../../src/metadata-builder/MetadataBuilder";
3+
4+
import {Post} from "../../src/decorator/Post";
5+
import {Controller} from "../../src";
6+
7+
const expect = require("chakram").expect;
8+
9+
describe("controller inheritance", () => {
10+
const metadataBuilder = new MetadataBuilder({});
11+
12+
abstract class AbstractControllerTemplate {
13+
@Post()
14+
public create() {
15+
}
16+
}
17+
18+
@Controller(`/derivative`)
19+
class DerivativeController extends AbstractControllerTemplate {
20+
}
21+
22+
@Controller(`/autonomous`)
23+
class AutonomousController {
24+
@Post()
25+
public create() {
26+
}
27+
}
28+
29+
30+
it("should build empty meta for empty set", () => {
31+
const meta = metadataBuilder.buildControllerMetadata([]);
32+
33+
expect(meta.length).to.be.eq(0);
34+
});
35+
36+
it("should build meta if only derivative controller given", () => {
37+
const meta = metadataBuilder.buildControllerMetadata([
38+
DerivativeController,
39+
]);
40+
41+
expect(meta.length).to.be.eq(1);
42+
expect(meta[0].route).to.be.eq("/derivative");
43+
expect(meta[0].actions.length).to.be.eq(1);
44+
45+
expect(meta[0].actions[0].method).to.be.eq("create");
46+
expect(meta[0].actions[0].type).to.be.eq("post");
47+
});
48+
49+
it("should build meta if only autonomous controller given", () => {
50+
const meta = metadataBuilder.buildControllerMetadata([
51+
AutonomousController,
52+
]);
53+
54+
expect(meta.length).to.be.eq(1);
55+
expect(meta[0].route).to.be.eq("/autonomous");
56+
expect(meta[0].actions.length).to.be.eq(1);
57+
58+
expect(meta[0].actions[0].method).to.be.eq("create");
59+
expect(meta[0].actions[0].type).to.be.eq("post");
60+
});
61+
62+
it("should build meta if autonomous and derivative controllers given", () => {
63+
const meta = metadataBuilder.buildControllerMetadata([
64+
DerivativeController,
65+
AutonomousController,
66+
]);
67+
68+
expect(meta.length).to.be.eq(2);
69+
expect(meta[0].actions.length).to.be.eq(1);
70+
expect(meta[1].actions.length).to.be.eq(1);
71+
72+
expect(meta[0].actions[0].method).to.be.eq("create");
73+
expect(meta[0].actions[0].type).to.be.eq("post");
74+
expect(meta[1].actions[0].method).to.be.eq("create");
75+
expect(meta[1].actions[0].type).to.be.eq("post");
76+
});
77+
78+
});

0 commit comments

Comments
 (0)