@@ -3,13 +3,15 @@ import { clientInputValidationDecorator } from '../clientInputValidation';
33
44// Mocks
55import { DebugLogger } from '../../logger/browser/DebugLogger' ;
6+ import { createClientMock } from './testUtils' ;
67
78const settings : any = {
89 log : DebugLogger ( ) ,
910 sync : { __splitFiltersValidation : { groupedFilters : { bySet : [ ] } } }
1011} ;
1112
12- const client : any = { } ;
13+ const EVALUATION_RESULT = 'on' ;
14+ const client : any = createClientMock ( EVALUATION_RESULT ) ;
1315
1416const readinessManager : any = {
1517 isReady : ( ) => true ,
@@ -52,4 +54,44 @@ describe('clientInputValidationDecorator', () => {
5254 // @TODO should be 8, but there is an additional log from `getTreatmentsByFlagSet` and `getTreatmentsWithConfigByFlagSet` that should be removed
5355 expect ( logSpy ) . toBeCalledTimes ( 10 ) ;
5456 } ) ;
57+
58+ test ( 'should evaluate but log an error if the passed 4th argument (evaluation options) is invalid' , ( ) => {
59+ expect ( clientWithValidation . getTreatment ( 'key' , 'ff' , undefined , 'invalid' ) ) . toBe ( EVALUATION_RESULT ) ;
60+ expect ( logSpy ) . toHaveBeenLastCalledWith ( '[ERROR] splitio => getTreatment: evaluation options must be a plain object.' ) ;
61+ expect ( client . getTreatment ) . toBeCalledWith ( 'key' , 'ff' , undefined , undefined ) ;
62+
63+ expect ( clientWithValidation . getTreatmentWithConfig ( 'key' , 'ff' , undefined , { properties : 'invalid' } ) ) . toBe ( EVALUATION_RESULT ) ;
64+ expect ( logSpy ) . toHaveBeenLastCalledWith ( '[ERROR] splitio => getTreatmentWithConfig: properties must be a plain object.' ) ;
65+ expect ( client . getTreatmentWithConfig ) . toBeCalledWith ( 'key' , 'ff' , undefined , undefined ) ;
66+
67+ expect ( clientWithValidation . getTreatments ( 'key' , [ 'ff' ] , undefined , { properties : 'invalid' } ) ) . toBe ( EVALUATION_RESULT ) ;
68+ expect ( logSpy ) . toHaveBeenLastCalledWith ( '[ERROR] splitio => getTreatments: properties must be a plain object.' ) ;
69+ expect ( client . getTreatments ) . toBeCalledWith ( 'key' , [ 'ff' ] , undefined , undefined ) ;
70+
71+ expect ( clientWithValidation . getTreatmentsWithConfig ( 'key' , [ 'ff' ] , { } , { properties : true } ) ) . toBe ( EVALUATION_RESULT ) ;
72+ expect ( logSpy ) . toHaveBeenLastCalledWith ( '[ERROR] splitio => getTreatmentsWithConfig: properties must be a plain object.' ) ;
73+ expect ( client . getTreatmentsWithConfig ) . toBeCalledWith ( 'key' , [ 'ff' ] , { } , undefined ) ;
74+
75+ expect ( clientWithValidation . getTreatmentsByFlagSet ( 'key' , 'flagSet' , undefined , { properties : 'invalid' } ) ) . toBe ( EVALUATION_RESULT ) ;
76+ expect ( logSpy ) . toHaveBeenLastCalledWith ( '[ERROR] splitio => getTreatmentsByFlagSet: properties must be a plain object.' ) ;
77+ expect ( client . getTreatmentsByFlagSet ) . toBeCalledWith ( 'key' , 'flagset' , undefined , undefined ) ;
78+
79+ expect ( clientWithValidation . getTreatmentsWithConfigByFlagSet ( 'key' , 'flagSet' , { } , { properties : 'invalid' } ) ) . toBe ( EVALUATION_RESULT ) ;
80+ expect ( logSpy ) . toBeCalledWith ( '[ERROR] splitio => getTreatmentsWithConfigByFlagSet: properties must be a plain object.' ) ;
81+ expect ( client . getTreatmentsWithConfigByFlagSet ) . toBeCalledWith ( 'key' , 'flagset' , { } , undefined ) ;
82+
83+ expect ( clientWithValidation . getTreatmentsByFlagSets ( 'key' , [ 'flagSet' ] , undefined , { properties : 'invalid' } ) ) . toBe ( EVALUATION_RESULT ) ;
84+ expect ( logSpy ) . toHaveBeenLastCalledWith ( '[ERROR] splitio => getTreatmentsByFlagSets: properties must be a plain object.' ) ;
85+ expect ( client . getTreatmentsByFlagSets ) . toBeCalledWith ( 'key' , [ 'flagset' ] , undefined , undefined ) ;
86+
87+ expect ( clientWithValidation . getTreatmentsWithConfigByFlagSets ( 'key' , [ 'flagSet' ] , { } , { properties : 'invalid' } ) ) . toBe ( EVALUATION_RESULT ) ;
88+ expect ( logSpy ) . toHaveBeenLastCalledWith ( '[ERROR] splitio => getTreatmentsWithConfigByFlagSets: properties must be a plain object.' ) ;
89+ expect ( client . getTreatmentsWithConfigByFlagSets ) . toBeCalledWith ( 'key' , [ 'flagset' ] , { } , undefined ) ;
90+ } ) ;
91+
92+ test ( 'should sanitize the properties in the 4th argument' , ( ) => {
93+ expect ( clientWithValidation . getTreatment ( 'key' , 'ff' , undefined , { properties : { toSanitize : / a s d / , correct : 100 } } ) ) . toBe ( EVALUATION_RESULT ) ;
94+ expect ( logSpy ) . toHaveBeenLastCalledWith ( '[WARN] splitio => getTreatment: Property "toSanitize" is of invalid type. Setting value to null.' ) ;
95+ expect ( client . getTreatment ) . toBeCalledWith ( 'key' , 'ff' , undefined , { properties : { toSanitize : null , correct : 100 } } ) ;
96+ } ) ;
5597} ) ;
0 commit comments