1
1
import { TestBed } from '@angular/core/testing'
2
2
import { MetadataJsonResolver } from './metadata-json-resolver'
3
3
import { MetadataValues } from './metadata-values'
4
- import { MetadataDefinition } from './metadata-definition '
5
- import { makeScopedMetadataDefinition } from './__tests__/ make-scoped- metadata-definition '
6
- import { makeGlobalMetadataDefinition } from './__tests__/ make-global-metadata-definition '
4
+ import { Metadata } from './metadata'
5
+ import { makeMetadata } from './make-metadata'
6
+ import { makeGlobalMetadata } from './make-global-metadata'
7
7
8
8
describe ( 'MetadataJsonResolver' , ( ) => {
9
9
let sut : MetadataJsonResolver
@@ -13,41 +13,34 @@ describe('MetadataJsonResolver', () => {
13
13
} )
14
14
15
15
describe ( 'get' , ( ) => {
16
- const scope = 'scope'
16
+ const key = 'key'
17
+ const subKey = 'subKey'
17
18
const global = 'global'
18
- const name = 'name'
19
19
const value = 'value'
20
20
21
21
function testGlobalMayBeRetrieved (
22
- metadataDefinition : MetadataDefinition ,
22
+ metadata : Metadata ,
23
23
values : MetadataValues ,
24
24
) {
25
25
describe ( 'when global is not defined' , ( ) => {
26
26
it ( 'should return undefined' , ( ) => {
27
- expect ( sut . get ( metadataDefinition , values ) ) . toBeUndefined ( )
27
+ expect ( sut . get ( metadata , values ) ) . toBeUndefined ( )
28
28
} )
29
29
} )
30
30
31
31
describe ( 'when global is defined' , ( ) => {
32
- const metadataDefinitionWithGlobal = makeScopedMetadataDefinition ( {
33
- ...metadataDefinition ,
34
- global,
35
- } )
32
+ const metadataWithGlobal = makeMetadata ( metadata . jsonPath , global )
36
33
37
34
describe ( 'but global value does not exist' , ( ) => {
38
35
it ( 'should return undefined' , ( ) => {
39
- expect (
40
- sut . get ( metadataDefinitionWithGlobal , values ) ,
41
- ) . toBeUndefined ( )
36
+ expect ( sut . get ( metadataWithGlobal , values ) ) . toBeUndefined ( )
42
37
} )
43
38
} )
44
39
describe ( 'and global value exists' , ( ) => {
45
40
const valuesWithGlobal = { [ global ] : value , ...values }
46
41
47
42
it ( 'should return global value' , ( ) => {
48
- expect (
49
- sut . get ( metadataDefinitionWithGlobal , valuesWithGlobal ) ,
50
- ) . toEqual ( value )
43
+ expect ( sut . get ( metadataWithGlobal , valuesWithGlobal ) ) . toEqual ( value )
51
44
} )
52
45
} )
53
46
} )
@@ -58,123 +51,82 @@ describe('MetadataJsonResolver', () => {
58
51
const values = undefined
59
52
60
53
it ( 'should return undefined' , ( ) => {
61
- expect (
62
- sut . get ( makeGlobalMetadataDefinition ( ) , values ) ,
63
- ) . toBeUndefined ( )
54
+ expect ( sut . get ( makeGlobalMetadata ( 'dummy' ) , values ) ) . toBeUndefined ( )
64
55
} )
65
56
} )
66
- describe ( 'like when scope does not exist' , ( ) => {
67
- const metadataDefinition = makeScopedMetadataDefinition ( {
68
- scope,
69
- } )
57
+ describe ( 'like when key does not exist' , ( ) => {
58
+ const metadata = makeMetadata ( [ key ] )
70
59
const values = { }
71
60
72
- testGlobalMayBeRetrieved ( metadataDefinition , values )
61
+ testGlobalMayBeRetrieved ( metadata , values )
73
62
} )
74
63
75
- describe ( 'like when scope is defined but property name does not exist' , ( ) => {
76
- const metadataDefinition = makeScopedMetadataDefinition ( {
77
- scope,
78
- name,
79
- } )
64
+ describe ( 'like when key is defined but sub key does not exist' , ( ) => {
65
+ const metadata = makeMetadata ( [ key , subKey ] )
80
66
const values = {
81
- [ scope ] : { } ,
67
+ [ key ] : { } ,
82
68
}
83
- testGlobalMayBeRetrieved ( metadataDefinition , values )
69
+ testGlobalMayBeRetrieved ( metadata , values )
84
70
} )
85
71
86
- describe ( 'like when scope is null' , ( ) => {
87
- const metadataDefinition = makeScopedMetadataDefinition ( {
88
- scope,
89
- } )
90
-
91
- const values = { [ scope ] : null }
72
+ describe ( 'like when key is null' , ( ) => {
73
+ const metadata = makeMetadata ( [ key ] )
74
+ const values = { [ key ] : null }
92
75
93
76
it ( 'should return null' , ( ) => {
94
- expect ( sut . get ( metadataDefinition , values ) ) . toBeNull ( )
77
+ expect ( sut . get ( metadata , values ) ) . toBeNull ( )
95
78
} )
96
79
} )
97
80
98
- describe ( 'like when scope value is null and there is sub scope' , ( ) => {
99
- const metadataDefinition = makeScopedMetadataDefinition ( {
100
- scope : `${ scope } .subScope` ,
101
- name,
102
- } )
103
-
104
- const values = { [ scope ] : null }
81
+ describe ( 'like when key is null and there is sub key' , ( ) => {
82
+ const metadata = makeMetadata ( [ key , subKey , 'dummy' ] )
83
+ const values = { [ key ] : null }
105
84
106
85
it ( 'should return null' , ( ) => {
107
- expect ( sut . get ( metadataDefinition , values ) ) . toBeNull ( )
86
+ expect ( sut . get ( metadata , values ) ) . toBeNull ( )
108
87
} )
109
88
} )
110
89
111
- describe ( 'like when scope is not an object' , ( ) => {
112
- const metadataDefinition = makeScopedMetadataDefinition ( { scope } )
90
+ describe ( 'like when value in key is not an object' , ( ) => {
91
+ const metadata = makeMetadata ( [ key , subKey ] )
113
92
const values = {
114
- [ scope ] : 42 ,
93
+ [ key ] : 42 ,
115
94
}
116
- testGlobalMayBeRetrieved ( metadataDefinition , values )
95
+ testGlobalMayBeRetrieved ( metadata , values )
117
96
} )
118
97
} )
119
98
120
99
describe ( 'when specific value is defined' , ( ) => {
121
- describe ( 'like when scope does not contain sub scopes' , ( ) => {
122
- const metadataDefinition = makeScopedMetadataDefinition ( {
123
- scope,
124
- name,
125
- } )
100
+ describe ( 'like when there is a key and sub key' , ( ) => {
101
+ const metadata = makeMetadata ( [ key , subKey ] )
126
102
127
103
const values = {
128
- [ scope ] : {
129
- [ name ] : value ,
104
+ [ key ] : {
105
+ [ subKey ] : value ,
130
106
} ,
131
107
}
132
108
133
- it ( 'should return value using scope and name as keys ' , ( ) => {
134
- expect ( sut . get ( metadataDefinition , values ) ) . toEqual ( value )
109
+ it ( 'should return value using key and sub key as path ' , ( ) => {
110
+ expect ( sut . get ( metadata , values ) ) . toEqual ( value )
135
111
} )
136
112
} )
137
113
138
- describe ( 'like when scope contains sub scopes' , ( ) => {
139
- const subScope = 'subScope'
140
- const metadataDefinition = makeScopedMetadataDefinition ( {
141
- scope : `${ scope } .${ subScope } ` ,
142
- name,
143
- } )
144
-
145
- const values = {
146
- [ scope ] : {
147
- [ subScope ] : {
148
- [ name ] : value ,
149
- } ,
150
- } ,
151
- }
152
-
153
- it ( 'should return value using sub scope, scope and name as keys' , ( ) => {
154
- expect ( sut . get ( metadataDefinition , values ) ) . toEqual ( value )
155
- } )
156
- } )
157
114
describe ( 'and it is and object, and a global object exists too' , ( ) => {
158
115
const valueObject = { value : 'value' , prop : 'value' }
159
116
const globalValueObject = {
160
117
globalValue : 'globalValue' ,
161
118
prop : 'globalValue' ,
162
119
}
163
- const metadataDefinition = makeScopedMetadataDefinition ( {
164
- scope,
165
- name,
166
- global,
167
- } )
168
-
120
+ const metadata = makeMetadata ( [ key , subKey ] , global )
169
121
const values = {
170
122
[ global ] : globalValueObject ,
171
- [ scope ] : {
172
- [ name ] : valueObject ,
123
+ [ key ] : {
124
+ [ subKey ] : valueObject ,
173
125
} ,
174
126
}
175
127
176
128
it ( 'should merge both objects, with specific value taking priority' , ( ) => {
177
- expect ( sut . get ( metadataDefinition , values ) ) . toEqual ( {
129
+ expect ( sut . get ( metadata , values ) ) . toEqual ( {
178
130
...globalValueObject ,
179
131
...valueObject ,
180
132
} )
0 commit comments