@@ -8,10 +8,64 @@ import {Application, Server, Component} from '../../index';
8
8
import { Context , Constructor } from '@loopback/context' ;
9
9
10
10
describe ( 'Application' , ( ) => {
11
+ describe ( 'controller binding' , ( ) => {
12
+ let app : Application ;
13
+ class MyController { }
14
+
15
+ beforeEach ( givenApp ) ;
16
+
17
+ it ( 'binds a controller' , ( ) => {
18
+ const binding = app . controller ( MyController ) ;
19
+ expect ( Array . from ( binding . tags ) ) . to . containEql ( 'controller' ) ;
20
+ expect ( binding . key ) . to . equal ( 'controllers.MyController' ) ;
21
+ expect ( findKeysByTag ( app , 'controller' ) ) . to . containEql ( binding . key ) ;
22
+ } ) ;
23
+
24
+ it ( 'binds a controller with custom name' , ( ) => {
25
+ const binding = app . controller ( MyController , 'my-controller' ) ;
26
+ expect ( Array . from ( binding . tags ) ) . to . containEql ( 'controller' ) ;
27
+ expect ( binding . key ) . to . equal ( 'controllers.my-controller' ) ;
28
+ expect ( findKeysByTag ( app , 'controller' ) ) . to . containEql ( binding . key ) ;
29
+ } ) ;
30
+
31
+ function givenApp ( ) {
32
+ app = new Application ( ) ;
33
+ }
34
+ } ) ;
35
+
36
+ describe ( 'component binding' , ( ) => {
37
+ let app : Application ;
38
+ class MyController { }
39
+ class MyComponent implements Component {
40
+ controllers = [ MyController ] ;
41
+ }
42
+
43
+ beforeEach ( givenApp ) ;
44
+
45
+ it ( 'binds a component' , ( ) => {
46
+ app . component ( MyComponent ) ;
47
+ expect ( findKeysByTag ( app , 'component' ) ) . to . containEql (
48
+ 'components.MyComponent' ,
49
+ ) ;
50
+ } ) ;
51
+
52
+ it ( 'binds a component' , ( ) => {
53
+ app . component ( MyComponent , 'my-component' ) ;
54
+ expect ( findKeysByTag ( app , 'component' ) ) . to . containEql (
55
+ 'components.my-component' ,
56
+ ) ;
57
+ } ) ;
58
+
59
+ function givenApp ( ) {
60
+ app = new Application ( ) ;
61
+ }
62
+ } ) ;
63
+
11
64
describe ( 'server binding' , ( ) => {
12
65
it ( 'defaults to constructor name' , async ( ) => {
13
66
const app = new Application ( ) ;
14
- app . server ( FakeServer ) ;
67
+ const binding = app . server ( FakeServer ) ;
68
+ expect ( Array . from ( binding . tags ) ) . to . containEql ( 'server' ) ;
15
69
const result = await app . getServer ( FakeServer . name ) ;
16
70
expect ( result . constructor . name ) . to . equal ( FakeServer . name ) ;
17
71
} ) ;
@@ -62,6 +116,10 @@ describe('Application', () => {
62
116
} ) ;
63
117
} ) ;
64
118
} ) ;
119
+
120
+ function findKeysByTag ( ctx : Context , tag : string | RegExp ) {
121
+ return ctx . findByTag ( tag ) . map ( binding => binding . key ) ;
122
+ }
65
123
} ) ;
66
124
67
125
class FakeComponent implements Component {
0 commit comments