@@ -2,20 +2,18 @@ import { Injectable } from '@angular/core';
2
2
import { TestBed } from '@angular/core/testing' ;
3
3
import {
4
4
ARRAY_PROPERTY ,
5
- BOOLEAN_PROPERTY ,
5
+ Deserializer ,
6
+ JsonPrimitive ,
6
7
LogMessage ,
7
- ModelPropertyTypeRegistrationInformation ,
8
- NUMBER_PROPERTY
8
+ ModelPropertyTypeRegistrationInformation
9
9
} from '@hypertrace/hyperdash' ;
10
10
import { DeserializationManagerService } from '../injectable-wrappers/deserialization/deserialization-manager.service' ;
11
11
import { LoggerService } from '../injectable-wrappers/logger.service' ;
12
12
import { ModelLibraryService } from '../injectable-wrappers/model-library.service' ;
13
13
import { ModelManagerService } from '../injectable-wrappers/model-manager.service' ;
14
14
import { ModelPropertyTypeLibraryService } from '../injectable-wrappers/model-property-type-library.service' ;
15
- import { ModelPropertyValidatorService } from '../injectable-wrappers/model-property-validator.service' ;
16
15
import { SerializationManagerService } from '../injectable-wrappers/serialization/serialization-manager.service' ;
17
- import { VariableManagerService } from '../injectable-wrappers/variable-manager.service' ;
18
- import { MODEL_PROPERTY_TYPES } from '../module/dashboard-core.module' ;
16
+ import { DASHBOARD_DESERIALIZERS , MODEL_PROPERTY_TYPES } from '../module/dashboard-core.module' ;
19
17
import { DefaultConfigurationService } from './default-configuration.service' ;
20
18
21
19
describe ( 'Default configuration service' , ( ) => {
@@ -29,6 +27,11 @@ describe('Default configuration service', () => {
29
27
provide : MODEL_PROPERTY_TYPES ,
30
28
useValue : [ { type : 'test-property' } , TestPropertyTypeProvider ] ,
31
29
multi : true
30
+ } ,
31
+ {
32
+ provide : DASHBOARD_DESERIALIZERS ,
33
+ useValue : [ TestDeserializer ] ,
34
+ multi : true
32
35
}
33
36
]
34
37
} ) ;
@@ -48,92 +51,6 @@ describe('Default configuration service', () => {
48
51
logger . warn = jest . fn ( ) ;
49
52
} ) ;
50
53
51
- test ( 'correctly configures deserialization' , ( ) => {
52
- const deserializationManager = TestBed . inject ( DeserializationManagerService ) ;
53
- const modelLibrary = TestBed . inject ( ModelLibraryService ) ;
54
-
55
- TestBed . inject ( ModelPropertyValidatorService ) . setStrictSchema ( false ) ;
56
- const testModel = class ModelClass {
57
- public constructor ( public prop : unknown ) { }
58
- } ;
59
-
60
- modelLibrary . registerModelClass ( testModel , { type : 'test-model' } ) ;
61
- modelLibrary . registerModelProperty ( testModel , 'prop' , {
62
- type : BOOLEAN_PROPERTY . type ,
63
- key : 'prop'
64
- } ) ;
65
-
66
- // Should throw until we configure the deserialization
67
- expect ( ( ) => deserializationManager . deserialize ( { type : 'test-model' , prop : false } ) ) . toThrow ( ) ;
68
-
69
- defaultConfigurationService . configure ( ) ;
70
- expect ( deserializationManager . deserialize ( { type : 'test-model' , prop : false } ) ) . toEqual ( new testModel ( false ) ) ;
71
- expect ( deserializationManager . deserialize ( { type : 'test-model' , prop : [ false ] } ) ) . toEqual ( new testModel ( [ false ] ) ) ;
72
- expect ( deserializationManager . deserialize ( { type : 'test-model' , prop : { nested : false } } ) ) . toEqual (
73
- new testModel ( { nested : false } )
74
- ) ;
75
-
76
- expect (
77
- deserializationManager . deserialize ( {
78
- type : 'test-model' ,
79
- prop : {
80
- type : 'test-model' ,
81
- prop : 'two models'
82
- }
83
- } )
84
- ) . toEqual ( new testModel ( new testModel ( 'two models' ) ) ) ;
85
-
86
- expect (
87
- deserializationManager . deserialize ( {
88
- type : 'test-model' ,
89
- prop : {
90
- nested : {
91
- type : 'test-model' ,
92
- prop : 'object sandwich'
93
- }
94
- }
95
- } )
96
- ) . toEqual ( new testModel ( { nested : new testModel ( 'object sandwich' ) } ) ) ;
97
- } ) ;
98
-
99
- test ( 'correctly configures deserialization and setting of variables' , ( ) => {
100
- const deserializationManager = TestBed . inject ( DeserializationManagerService ) ;
101
- const modelLibrary = TestBed . inject ( ModelLibraryService ) ;
102
-
103
- const testModel = class ModelClass {
104
- public constructor ( public prop ?: number ) { }
105
- } ;
106
-
107
- modelLibrary . registerModelClass ( testModel , { type : 'test-model' } ) ;
108
- modelLibrary . registerModelProperty ( testModel , 'prop' , {
109
- type : NUMBER_PROPERTY . type ,
110
- key : 'prop' ,
111
- required : false
112
- } ) ;
113
-
114
- defaultConfigurationService . configure ( ) ;
115
-
116
- const deserializedModel = deserializationManager . deserialize < object > ( {
117
- type : 'test-model' ,
118
- // tslint:disable-next-line:no-invalid-template-strings
119
- prop : '${test}'
120
- } ) ;
121
-
122
- expect ( deserializedModel ) . toEqual ( new testModel ( ) ) ;
123
-
124
- TestBed . inject ( VariableManagerService ) . set ( 'test' , 42 , deserializedModel ) ;
125
-
126
- expect ( deserializedModel ) . toEqual ( new testModel ( 42 ) ) ;
127
- } ) ;
128
-
129
- test ( 'should throw if attempting to configure twice' , ( ) => {
130
- defaultConfigurationService . configure ( ) ;
131
-
132
- expect ( ( ) => defaultConfigurationService . configure ( ) ) . toThrow (
133
- 'Default Configuration Service cannot be configured twice'
134
- ) ;
135
- } ) ;
136
-
137
54
test ( 'correctly configures serialization' , ( ) => {
138
55
const serializationManager = TestBed . inject ( SerializationManagerService ) ;
139
56
const modelLibrary = TestBed . inject ( ModelLibraryService ) ;
@@ -173,9 +90,26 @@ describe('Default configuration service', () => {
173
90
const propertyTypeLibrary = TestBed . inject ( ModelPropertyTypeLibraryService ) ;
174
91
propertyTypeLibrary . registerPropertyType = jest . fn ( ) ;
175
92
defaultConfigurationService . configure ( ) ;
176
- expect ( propertyTypeLibrary . registerPropertyType ) . toHaveBeenCalledWith ( { type : 'test-property' } ) ;
177
93
94
+ expect ( propertyTypeLibrary . registerPropertyType ) . toHaveBeenCalledTimes ( 2 ) ;
95
+ expect ( propertyTypeLibrary . registerPropertyType ) . toHaveBeenCalledWith ( { type : 'test-property' } ) ;
178
96
expect ( propertyTypeLibrary . registerPropertyType ) . toHaveBeenCalledWith ( expect . any ( TestPropertyTypeProvider ) ) ;
97
+
98
+ defaultConfigurationService . configure ( ) ;
99
+ // Should not be called a third time
100
+ expect ( propertyTypeLibrary . registerPropertyType ) . toHaveBeenCalledTimes ( 2 ) ;
101
+ } ) ;
102
+
103
+ test ( 'registers provided deserializers' , ( ) => {
104
+ const deserializationManager = TestBed . inject ( DeserializationManagerService ) ;
105
+ deserializationManager . registerDeserializer = jest . fn ( ) ;
106
+ defaultConfigurationService . configure ( ) ;
107
+ expect ( deserializationManager . registerDeserializer ) . toHaveBeenCalledTimes ( 1 ) ;
108
+ expect ( deserializationManager . registerDeserializer ) . toHaveBeenCalledWith ( expect . any ( TestDeserializer ) ) ;
109
+
110
+ defaultConfigurationService . configure ( ) ;
111
+ // Should not be called a second time
112
+ expect ( deserializationManager . registerDeserializer ) . toHaveBeenCalledTimes ( 1 ) ;
179
113
} ) ;
180
114
} ) ;
181
115
@@ -185,3 +119,15 @@ describe('Default configuration service', () => {
185
119
class TestPropertyTypeProvider implements ModelPropertyTypeRegistrationInformation {
186
120
public readonly type : string = 'test-prop-provider' ;
187
121
}
122
+
123
+ @Injectable ( {
124
+ providedIn : 'root'
125
+ } )
126
+ class TestDeserializer implements Deserializer < string , string > {
127
+ public canDeserialize ( json : JsonPrimitive ) : json is string {
128
+ return typeof json === 'string' ;
129
+ }
130
+ public deserialize ( json : string ) : string {
131
+ return json . toUpperCase ( ) ;
132
+ }
133
+ }
0 commit comments