1
1
import "reflect-metadata" ;
2
2
import { MetadataBuilder } from "../../src/metadata-builder/MetadataBuilder" ;
3
-
4
- import { Post } from "../../src/decorator/Post" ;
5
- import { Controller } from "../../src" ;
3
+ import { Controller , getMetadataArgsStorage , Post } from "../../src" ;
6
4
7
5
const expect = require ( "chakram" ) . expect ;
8
6
9
7
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
8
30
9
it ( "should build empty meta for empty set" , ( ) => {
10
+ // Reset storage
11
+ getMetadataArgsStorage ( ) . reset ( ) ;
12
+ const metadataBuilder = new MetadataBuilder ( { } ) ;
31
13
const meta = metadataBuilder . buildControllerMetadata ( [ ] ) ;
32
14
33
15
expect ( meta . length ) . to . be . eq ( 0 ) ;
34
16
} ) ;
35
17
36
- it ( "should build meta if only derivative controller given" , ( ) => {
18
+ it ( "should build meta if the only derivative controller is given" , ( ) => {
19
+ // Reset storage
20
+ getMetadataArgsStorage ( ) . reset ( ) ;
21
+
22
+ // Persist storage from decorators
23
+ abstract class AbstractControllerTemplate {
24
+ @Post ( )
25
+ public create ( ) { }
26
+ }
27
+
28
+ @Controller ( `/derivative` )
29
+ class DerivativeController extends AbstractControllerTemplate { }
30
+
31
+ @Controller ( `/autonomous` )
32
+ class AutonomousController {
33
+ @Post ( )
34
+ public create ( ) { }
35
+ }
36
+
37
+ // Build controllers
38
+ const metadataBuilder = new MetadataBuilder ( { } ) ;
37
39
const meta = metadataBuilder . buildControllerMetadata ( [
38
40
DerivativeController ,
39
41
] ) ;
@@ -46,7 +48,26 @@ describe("controller inheritance", () => {
46
48
expect ( meta [ 0 ] . actions [ 0 ] . type ) . to . be . eq ( "post" ) ;
47
49
} ) ;
48
50
49
- it ( "should build meta if only autonomous controller given" , ( ) => {
51
+ it ( "should build meta if the only autonomous controller is given" , ( ) => {
52
+ getMetadataArgsStorage ( ) . reset ( ) ;
53
+
54
+ // Persist storage from decorators
55
+ abstract class AbstractControllerTemplate {
56
+ @Post ( )
57
+ public create ( ) { }
58
+ }
59
+
60
+ @Controller ( `/derivative` )
61
+ class DerivativeController extends AbstractControllerTemplate { }
62
+
63
+ @Controller ( `/autonomous` )
64
+ class AutonomousController {
65
+ @Post ( )
66
+ public create ( ) { }
67
+ }
68
+
69
+ // Build controllers
70
+ const metadataBuilder = new MetadataBuilder ( { } ) ;
50
71
const meta = metadataBuilder . buildControllerMetadata ( [
51
72
AutonomousController ,
52
73
] ) ;
@@ -59,11 +80,27 @@ describe("controller inheritance", () => {
59
80
expect ( meta [ 0 ] . actions [ 0 ] . type ) . to . be . eq ( "post" ) ;
60
81
} ) ;
61
82
62
- it ( "should build meta if autonomous and derivative controllers given" , ( ) => {
63
- const meta = metadataBuilder . buildControllerMetadata ( [
64
- DerivativeController ,
65
- AutonomousController ,
66
- ] ) ;
83
+ it ( "should build meta both when autonomous and derivative controllers are given" , ( ) => {
84
+ getMetadataArgsStorage ( ) . reset ( ) ;
85
+
86
+ // Persist storage from decorators
87
+ abstract class AbstractControllerTemplate {
88
+ @Post ( )
89
+ public create ( ) { }
90
+ }
91
+
92
+ @Controller ( `/derivative` )
93
+ class DerivativeController extends AbstractControllerTemplate { }
94
+
95
+ @Controller ( `/autonomous` )
96
+ class AutonomousController {
97
+ @Post ( )
98
+ public create ( ) { }
99
+ }
100
+
101
+ // Build controllers
102
+ const metadataBuilder = new MetadataBuilder ( { } ) ;
103
+ const meta = metadataBuilder . buildControllerMetadata ( ) ;
67
104
68
105
expect ( meta . length ) . to . be . eq ( 2 ) ;
69
106
expect ( meta [ 0 ] . actions . length ) . to . be . eq ( 1 ) ;
@@ -74,5 +111,4 @@ describe("controller inheritance", () => {
74
111
expect ( meta [ 1 ] . actions [ 0 ] . method ) . to . be . eq ( "create" ) ;
75
112
expect ( meta [ 1 ] . actions [ 0 ] . type ) . to . be . eq ( "post" ) ;
76
113
} ) ;
77
-
78
114
} ) ;
0 commit comments