1
+ import { InjectionToken } from '@angular/core' ;
2
+ import { TestBed } from '@angular/core/testing' ;
1
3
import { faker } from '@faker-js/faker/locale/en_US' ;
2
4
3
5
import { DaffConfigInjectionToken } from '@daffodil/core' ;
@@ -11,13 +13,16 @@ interface Config {
11
13
12
14
describe ( '@daffodil/core | createConfigInjectionToken' , ( ) => {
13
15
let name : string ;
14
- let value : number ;
16
+ let value : Partial < Config > ;
15
17
let defaultConfig : Config ;
16
18
17
19
let result : DaffConfigInjectionToken < Config > ;
18
20
19
21
beforeEach ( ( ) => {
20
22
name = faker . random . word ( ) ;
23
+ value = {
24
+ field : faker . random . word ( ) ,
25
+ } ;
21
26
defaultConfig = {
22
27
field : faker . random . word ( ) ,
23
28
other : faker . random . word ( ) ,
@@ -29,13 +34,39 @@ describe('@daffodil/core | createConfigInjectionToken', () => {
29
34
expect ( result . token . toString ( ) ) . toContain ( name ) ;
30
35
} ) ;
31
36
32
- it ( 'should return a provider that spreads in passed values with the default' , ( ) => {
33
- const val = faker . random . word ( ) ;
34
- const res = result . provider ( {
35
- field : val ,
37
+ describe ( 'when the provided value is an injection token' , ( ) => {
38
+ let token : InjectionToken < Partial < Config > > ;
39
+ let defaultToken : InjectionToken < Config > ;
40
+
41
+ beforeEach ( ( ) => {
42
+ token = new InjectionToken ( 'test' ) ;
43
+ defaultToken = new InjectionToken ( 'default' ) ;
44
+ TestBed . configureTestingModule ( {
45
+ providers : [
46
+ {
47
+ provide : token ,
48
+ useValue : value ,
49
+ } ,
50
+ {
51
+ provide : defaultToken ,
52
+ useValue : defaultConfig ,
53
+ } ,
54
+ ] ,
55
+ } ) ;
36
56
} ) ;
57
+
58
+ it ( 'should return a provider that spreads in passed values with the default' , ( ) => {
59
+ const res = result . provider ( token ) ;
60
+ expect ( res . provide ) . toEqual ( result . token ) ;
61
+ expect ( TestBed . runInInjectionContext < Config > ( < any > res . useFactory ) . field ) . toEqual ( value . field ) ;
62
+ expect ( TestBed . runInInjectionContext < Config > ( < any > res . useFactory ) . other ) . toEqual ( defaultConfig . other ) ;
63
+ } ) ;
64
+ } ) ;
65
+
66
+ it ( 'should return a provider that spreads in passed values with the default' , ( ) => {
67
+ const res = result . provider ( value ) ;
37
68
expect ( res . provide ) . toEqual ( result . token ) ;
38
- expect ( res . useValue . field ) . toEqual ( val ) ;
39
- expect ( res . useValue . other ) . toEqual ( defaultConfig . other ) ;
69
+ expect ( TestBed . runInInjectionContext < Config > ( < any > res . useFactory ) . field ) . toEqual ( value . field ) ;
70
+ expect ( TestBed . runInInjectionContext < Config > ( < any > res . useFactory ) . other ) . toEqual ( defaultConfig . other ) ;
40
71
} ) ;
41
72
} ) ;
0 commit comments