-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfallback-sanitizer.spec.ts
More file actions
135 lines (113 loc) · 4.84 KB
/
fallback-sanitizer.spec.ts
File metadata and controls
135 lines (113 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { isValidFlagName, isValidTreatment, sanitizeFallbacks } from '../fallbackSanitizer';
import { TreatmentWithConfig } from '../../../../types/splitio';
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';
describe('FallbacksSanitizer', () => {
const validTreatment: TreatmentWithConfig = { treatment: 'on', config: '{"color":"blue"}' };
const invalidTreatment: TreatmentWithConfig = { treatment: ' ', config: null };
const fallbackMock = {
global: undefined,
byFlag: {}
};
beforeEach(() => {
loggerMock.mockClear();
});
describe('isValidFlagName', () => {
test('returns true for a valid flag name', () => {
// @ts-expect-private-access
expect(isValidFlagName('my_flag')).toBe(true);
});
test('returns false for a name longer than 100 chars', () => {
const longName = 'a'.repeat(101);
expect(isValidFlagName(longName)).toBe(false);
});
test('returns false if the name contains spaces', () => {
expect(isValidFlagName('invalid flag')).toBe(false);
});
test('returns false if the name contains spaces', () => {
// @ts-ignore
expect(isValidFlagName(true)).toBe(false);
});
});
describe('isValidTreatment', () => {
test('returns true for a valid treatment string', () => {
expect(isValidTreatment(validTreatment)).toBe(true);
});
test('returns false for null or undefined', () => {
expect(isValidTreatment()).toBe(false);
expect(isValidTreatment(undefined)).toBe(false);
});
test('returns false for a treatment longer than 100 chars', () => {
const long = { treatment: 'a'.repeat(101), config: null };
expect(isValidTreatment(long)).toBe(false);
});
test('returns false if treatment does not match regex pattern', () => {
const invalid = { treatment: 'invalid treatment!', config: null };
expect(isValidTreatment(invalid)).toBe(false);
});
});
describe('sanitizeGlobal', () => {
test('returns the treatment if valid', () => {
expect(sanitizeFallbacks(loggerMock, { ...fallbackMock, global: validTreatment })).toEqual({ ...fallbackMock, global: validTreatment });
expect(loggerMock.error).not.toHaveBeenCalled();
});
test('returns undefined and logs error if invalid', () => {
const result = sanitizeFallbacks(loggerMock, { ...fallbackMock, global: invalidTreatment });
expect(result).toEqual(fallbackMock);
expect(loggerMock.error).toHaveBeenCalledWith(
expect.stringContaining('Fallback treatments - Discarded fallback')
);
});
});
describe('sanitizeByFlag', () => {
test('returns a sanitized map with valid entries only', () => {
const input = {
valid_flag: validTreatment,
'invalid flag': validTreatment,
bad_treatment: invalidTreatment,
};
const result = sanitizeFallbacks(loggerMock, {...fallbackMock, byFlag: input});
expect(result).toEqual({ ...fallbackMock, byFlag: { valid_flag: validTreatment } });
expect(loggerMock.error).toHaveBeenCalledTimes(2); // invalid flag + bad_treatment
});
test('returns empty object if all invalid', () => {
const input = {
'invalid flag': invalidTreatment,
};
const result = sanitizeFallbacks(loggerMock, {...fallbackMock, byFlag: input});
expect(result).toEqual(fallbackMock);
expect(loggerMock.error).toHaveBeenCalled();
});
test('returns same object if all valid', () => {
const input = {
...fallbackMock,
byFlag:{
flag_one: validTreatment,
flag_two: { treatment: 'valid_2', config: null },
}
};
const result = sanitizeFallbacks(loggerMock, input);
expect(result).toEqual(input);
expect(loggerMock.error).not.toHaveBeenCalled();
});
});
describe('sanitizeFallbacks', () => {
test('returns undefined and logs error if fallbacks is not an object', () => { // @ts-expect-error
const result = sanitizeFallbacks(loggerMock, 'invalid_fallbacks');
expect(result).toBeUndefined();
expect(loggerMock.error).toHaveBeenCalledWith(
'Fallback treatments - Discarded configuration: it must be an object with optional `global` and `byFlag` properties'
);
});
test('returns undefined and logs error if fallbacks is not an object', () => { // @ts-expect-error
const result = sanitizeFallbacks(loggerMock, true);
expect(result).toBeUndefined();
expect(loggerMock.error).toHaveBeenCalledWith(
'Fallback treatments - Discarded configuration: it must be an object with optional `global` and `byFlag` properties'
);
});
test('sanitizes both global and byFlag fallbacks for empty object', () => { // @ts-expect-error
const result = sanitizeFallbacks(loggerMock, { global: {} });
expect(result).toEqual({ global: undefined, byFlag: {} });
});
});
});