3
3
// This file is licensed under the MIT License.
4
4
// License text available at https://opensource.org/licenses/MIT
5
5
6
- import { Application , Component , Provider } from '@loopback/core' ;
6
+ import { Application , Component , Provider , BindingScope } from '@loopback/core' ;
7
7
import { expect } from '@loopback/testlab' ;
8
8
import { Class , ServiceMixin } from '../../../' ;
9
9
@@ -15,7 +15,7 @@ describe('ServiceMixin', () => {
15
15
expect ( typeof myApp . serviceProvider ) . to . be . eql ( 'function' ) ;
16
16
} ) ;
17
17
18
- it ( 'binds repository from app.serviceProvider()' , async ( ) => {
18
+ it ( 'binds service from app.serviceProvider()' , async ( ) => {
19
19
const myApp = new AppWithServiceMixin ( ) ;
20
20
21
21
expectGeocoderToNotBeBound ( myApp ) ;
@@ -56,16 +56,17 @@ describe('ServiceMixin', () => {
56
56
geocode ( address : string ) : Promise < GeoPoint > ;
57
57
}
58
58
59
- // A dummy service instance to make unit testing easier
60
- const GeocoderSingleton : GeocoderService = {
59
+ class DummyGeocoder implements GeocoderService {
61
60
geocode ( address : string ) {
62
61
return Promise . resolve ( { lat : 0 , lng : 0 } ) ;
63
- } ,
64
- } ;
62
+ }
63
+ }
65
64
66
65
class GeocoderServiceProvider implements Provider < GeocoderService > {
67
66
value ( ) : Promise < GeocoderService > {
68
- return Promise . resolve ( GeocoderSingleton ) ;
67
+ // Returns different instances so that we can verify the TRANSIENT
68
+ // binding scope, which is now the default for service proxies
69
+ return Promise . resolve ( new DummyGeocoder ( ) ) ;
69
70
}
70
71
}
71
72
@@ -74,15 +75,19 @@ describe('ServiceMixin', () => {
74
75
}
75
76
76
77
async function expectGeocoderToBeBound ( myApp : Application ) {
77
- const boundRepositories = myApp . find ( 'services.*' ) . map ( b => b . key ) ;
78
- expect ( boundRepositories ) . to . containEql ( 'services.GeocoderService' ) ;
79
- const repoInstance = await myApp . get ( 'services.GeocoderService' ) ;
80
- expect ( repoInstance ) . to . equal ( GeocoderSingleton ) ;
78
+ const boundServices = myApp . find ( 'services.*' ) . map ( b => b . key ) ;
79
+ expect ( boundServices ) . to . containEql ( 'services.GeocoderService' ) ;
80
+ const binding = myApp . getBinding ( 'services.GeocoderService' ) ;
81
+ expect ( binding . scope ) . to . equal ( BindingScope . TRANSIENT ) ;
82
+ const serviceInstance1 = await myApp . get ( 'services.GeocoderService' ) ;
83
+ expect ( serviceInstance1 ) . to . be . instanceOf ( DummyGeocoder ) ;
84
+ const serviceInstance2 = await myApp . get ( 'services.GeocoderService' ) ;
85
+ expect ( serviceInstance2 ) . to . not . be . equal ( serviceInstance1 ) ;
81
86
}
82
87
83
88
function expectGeocoderToNotBeBound ( myApp : Application ) {
84
- const boundRepos = myApp . find ( 'services.*' ) . map ( b => b . key ) ;
85
- expect ( boundRepos ) . to . be . empty ( ) ;
89
+ const boundServices = myApp . find ( 'services.*' ) . map ( b => b . key ) ;
90
+ expect ( boundServices ) . to . be . empty ( ) ;
86
91
}
87
92
88
93
function expectComponentToBeBound (
0 commit comments