-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfallback-calculator.spec.ts
More file actions
134 lines (120 loc) · 4.79 KB
/
fallback-calculator.spec.ts
File metadata and controls
134 lines (120 loc) · 4.79 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
import { FallbackTreatmentsCalculator } from '../';
import type { FallbackTreatmentConfiguration } from '../../../../types/splitio';
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';
import { CONTROL } from '../../../utils/constants';
describe('FallbackTreatmentsCalculator' , () => {
const longName = 'a'.repeat(101);
test('logs an error if flag name is invalid - by Flag', () => {
let config: FallbackTreatmentConfiguration = {
byFlag: {
'feature A': { treatment: 'TREATMENT_A', config: '{ value: 1 }' },
},
};
new FallbackTreatmentsCalculator(loggerMock, config);
expect(loggerMock.error.mock.calls[0][0]).toBe(
'Fallback treatments - Discarded flag \'feature A\': Invalid flag name (max 100 chars, no spaces)'
);
config = {
byFlag: {
[longName]: { treatment: 'TREATMENT_A', config: '{ value: 1 }' },
},
};
new FallbackTreatmentsCalculator(loggerMock, config);
expect(loggerMock.error.mock.calls[1][0]).toBe(
`Fallback treatments - Discarded flag '${longName}': Invalid flag name (max 100 chars, no spaces)`
);
config = {
byFlag: {
'featureB': { treatment: longName, config: '{ value: 1 }' },
},
};
new FallbackTreatmentsCalculator(loggerMock, config);
expect(loggerMock.error.mock.calls[2][0]).toBe(
'Fallback treatments - Discarded treatment for flag \'featureB\': Invalid treatment (max 100 chars and must match pattern)'
);
config = {
byFlag: {
// @ts-ignore
'featureC': { config: '{ global: true }' },
},
};
new FallbackTreatmentsCalculator(loggerMock, config);
expect(loggerMock.error.mock.calls[3][0]).toBe(
'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
);
config = {
byFlag: {
// @ts-ignore
'featureC': { treatment: 'invalid treatment!', config: '{ global: true }' },
},
};
new FallbackTreatmentsCalculator(loggerMock, config);
expect(loggerMock.error.mock.calls[4][0]).toBe(
'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
);
});
test('logs an error if flag name is invalid - global', () => {
let config: FallbackTreatmentConfiguration = {
global: { treatment: longName, config: '{ value: 1 }' },
};
new FallbackTreatmentsCalculator(loggerMock, config);
expect(loggerMock.error.mock.calls[2][0]).toBe(
'Fallback treatments - Discarded treatment for flag \'featureB\': Invalid treatment (max 100 chars and must match pattern)'
);
config = {
// @ts-ignore
global: { config: '{ global: true }' },
};
new FallbackTreatmentsCalculator(loggerMock, config);
expect(loggerMock.error.mock.calls[3][0]).toBe(
'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
);
config = {
// @ts-ignore
global: { treatment: 'invalid treatment!', config: '{ global: true }' },
};
new FallbackTreatmentsCalculator(loggerMock, config);
expect(loggerMock.error.mock.calls[4][0]).toBe(
'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
);
});
test('returns specific fallback if flag exists', () => {
const config: FallbackTreatmentConfiguration = {
byFlag: {
'featureA': { treatment: 'TREATMENT_A', config: '{ value: 1 }' },
},
};
const calculator = new FallbackTreatmentsCalculator(loggerMock, config);
const result = calculator.resolve('featureA', 'label by flag');
expect(result).toEqual({
treatment: 'TREATMENT_A',
config: '{ value: 1 }',
label: 'fallback - label by flag',
});
});
test('returns global fallback if flag is missing and global exists', () => {
const config: FallbackTreatmentConfiguration = {
byFlag: {},
global: { treatment: 'GLOBAL_TREATMENT', config: '{ global: true }' },
};
const calculator = new FallbackTreatmentsCalculator(loggerMock, config);
const result = calculator.resolve('missingFlag', 'label by global');
expect(result).toEqual({
treatment: 'GLOBAL_TREATMENT',
config: '{ global: true }',
label: 'fallback - label by global',
});
});
test('returns control fallback if flag and global are missing', () => {
const config: FallbackTreatmentConfiguration = {
byFlag: {},
};
const calculator = new FallbackTreatmentsCalculator(loggerMock, config);
const result = calculator.resolve('missingFlag', 'label by noFallback');
expect(result).toEqual({
treatment: CONTROL,
config: null,
label: 'label by noFallback',
});
});
});