Skip to content

Commit b24882b

Browse files
Unit tests
1 parent a976968 commit b24882b

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/sdkClient/__tests__/clientInputValidation.spec.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { clientInputValidationDecorator } from '../clientInputValidation';
33

44
// Mocks
55
import { DebugLogger } from '../../logger/browser/DebugLogger';
6+
import { createClientMock } from './testUtils';
67

78
const 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

1416
const 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: /asd/, 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
});

src/sdkClient/__tests__/testUtils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,18 @@ export function assertClientApi(client: any, sdkStatus?: object) {
88
expect(typeof client[method]).toBe('function');
99
});
1010
}
11+
12+
export function createClientMock(returnValue: any) {
13+
14+
return {
15+
getTreatment: jest.fn(()=> returnValue),
16+
getTreatmentWithConfig: jest.fn(()=> returnValue),
17+
getTreatments: jest.fn(()=> returnValue),
18+
getTreatmentsWithConfig: jest.fn(()=> returnValue),
19+
getTreatmentsByFlagSets: jest.fn(()=> returnValue),
20+
getTreatmentsWithConfigByFlagSets: jest.fn(()=> returnValue),
21+
getTreatmentsByFlagSet: jest.fn(()=> returnValue),
22+
getTreatmentsWithConfigByFlagSet: jest.fn(()=> returnValue),
23+
track: jest.fn(()=> returnValue),
24+
};
25+
}

0 commit comments

Comments
 (0)