@@ -2,20 +2,18 @@ import { Injectable } from '@angular/core';
22import { TestBed } from '@angular/core/testing' ;
33import {
44 ARRAY_PROPERTY ,
5- BOOLEAN_PROPERTY ,
5+ Deserializer ,
6+ JsonPrimitive ,
67 LogMessage ,
7- ModelPropertyTypeRegistrationInformation ,
8- NUMBER_PROPERTY
8+ ModelPropertyTypeRegistrationInformation
99} from '@hypertrace/hyperdash' ;
1010import { DeserializationManagerService } from '../injectable-wrappers/deserialization/deserialization-manager.service' ;
1111import { LoggerService } from '../injectable-wrappers/logger.service' ;
1212import { ModelLibraryService } from '../injectable-wrappers/model-library.service' ;
1313import { ModelManagerService } from '../injectable-wrappers/model-manager.service' ;
1414import { ModelPropertyTypeLibraryService } from '../injectable-wrappers/model-property-type-library.service' ;
15- import { ModelPropertyValidatorService } from '../injectable-wrappers/model-property-validator.service' ;
1615import { 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' ;
1917import { DefaultConfigurationService } from './default-configuration.service' ;
2018
2119describe ( 'Default configuration service' , ( ) => {
@@ -29,6 +27,11 @@ describe('Default configuration service', () => {
2927 provide : MODEL_PROPERTY_TYPES ,
3028 useValue : [ { type : 'test-property' } , TestPropertyTypeProvider ] ,
3129 multi : true
30+ } ,
31+ {
32+ provide : DASHBOARD_DESERIALIZERS ,
33+ useValue : [ TestDeserializer ] ,
34+ multi : true
3235 }
3336 ]
3437 } ) ;
@@ -48,92 +51,6 @@ describe('Default configuration service', () => {
4851 logger . warn = jest . fn ( ) ;
4952 } ) ;
5053
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-
13754 test ( 'correctly configures serialization' , ( ) => {
13855 const serializationManager = TestBed . inject ( SerializationManagerService ) ;
13956 const modelLibrary = TestBed . inject ( ModelLibraryService ) ;
@@ -173,9 +90,26 @@ describe('Default configuration service', () => {
17390 const propertyTypeLibrary = TestBed . inject ( ModelPropertyTypeLibraryService ) ;
17491 propertyTypeLibrary . registerPropertyType = jest . fn ( ) ;
17592 defaultConfigurationService . configure ( ) ;
176- expect ( propertyTypeLibrary . registerPropertyType ) . toHaveBeenCalledWith ( { type : 'test-property' } ) ;
17793
94+ expect ( propertyTypeLibrary . registerPropertyType ) . toHaveBeenCalledTimes ( 2 ) ;
95+ expect ( propertyTypeLibrary . registerPropertyType ) . toHaveBeenCalledWith ( { type : 'test-property' } ) ;
17896 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 ) ;
179113 } ) ;
180114} ) ;
181115
@@ -185,3 +119,15 @@ describe('Default configuration service', () => {
185119class TestPropertyTypeProvider implements ModelPropertyTypeRegistrationInformation {
186120 public readonly type : string = 'test-prop-provider' ;
187121}
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