-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathpreloadedData.spec.js
More file actions
174 lines (163 loc) · 6.64 KB
/
preloadedData.spec.js
File metadata and controls
174 lines (163 loc) · 6.64 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import tape from 'tape';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
const proxyquireStrict = proxyquire.noCallThru();
const loggerMock = {
warn: sinon.stub(),
error: sinon.stub(),
};
function LogFactoryMock() {
return loggerMock;
}
// Import the module mocking the logger.
const validatePreloadedData = proxyquireStrict('../../inputValidation/preloadedData', {
'../logger': LogFactoryMock
}).validatePreloadedData;
const method = 'some_method';
const testCases = [
// valid inputs
{
input: { lastUpdated: 10, since: 10, splitsData: {} },
output: true,
warn: `${method}: preloadedData.splitsData doesn't contain split definitions.`
},
{
input: { lastUpdated: 10, since: 10, splitsData: { 'some_split': 'SPLIT DEFINITION' } },
output: true
},
{
input: { lastUpdated: 10, since: 10, splitsData: { 'some_split': 'SPLIT DEFINITION' }, mySegmentsData: { 'some_key': [] } },
output: true
},
{
input: { lastUpdated: 10, since: 10, splitsData: { 'some_split': 'SPLIT DEFINITION' }, mySegmentsData: { 'some_key': [] } },
output: true
},
{
input: { lastUpdated: 10, since: 10, splitsData: { 'some_split': 'SPLIT DEFINITION' }, mySegmentsData: {} },
output: true
},
{
input: { lastUpdated: 10, since: 10, splitsData: { 'some_split': 'SPLIT DEFINITION' }, mySegmentsData: { some_key: [] } },
output: true
},
{
input: { lastUpdated: 10, since: 10, splitsData: { 'some_split': 'SPLIT DEFINITION' }, mySegmentsData: { some_key: ['some_segment'] } },
output: true
},
{
input: { lastUpdated: 10, since: 10, splitsData: { 'some_split': 'SPLIT DEFINITION' }, segmentsData: {} },
output: true
},
{
input: { lastUpdated: 10, since: 10, splitsData: { 'some_split': 'SPLIT DEFINITION' }, segmentsData: { some_segment: 'SEGMENT DEFINITION' } },
output: true
},
{
input: { lastUpdated: 10, since: 10, splitsData: { 'some_split': 'SPLIT DEFINITION' }, mySegmentsData: { some_key: ['some_segment'], some_other_key: ['some_segment'] }, segmentsData: { some_segment: 'SEGMENT DEFINITION', some_other_segment: 'SEGMENT DEFINITION' } },
output: true
},
{
msg: 'should be true, even using objects for strings and numbers or having extra properties',
input: { ignoredProperty: 'IGNORED', lastUpdated: new Number(10), since: new Number(10), splitsData: { 'some_split': new String('SPLIT DEFINITION') }, mySegmentsData: { some_key: [new String('some_segment')] }, segmentsData: { some_segment: new String('SEGMENT DEFINITION') } },
output: true
},
// invalid inputs
{
msg: 'should be false if preloadedData is not an object',
input: undefined,
output: false,
error: `${method}: preloadedData must be an object.`
},
{
msg: 'should be false if preloadedData is not an object',
input: [],
output: false,
error: `${method}: preloadedData must be an object.`
},
{
msg: 'should be false if lastUpdated property is invalid',
input: { lastUpdated: undefined, since: 10, splitsData: {} },
output: false,
error: `${method}: preloadedData.lastUpdated must be a positive number.`
},
{
msg: 'should be false if lastUpdated property is invalid',
input: { lastUpdated: -1, since: 10, splitsData: {} },
output: false,
error: `${method}: preloadedData.lastUpdated must be a positive number.`
},
{
msg: 'should be false if since property is invalid',
input: { lastUpdated: 10, since: undefined, splitsData: {} },
output: false,
error: `${method}: preloadedData.since must be a positive number.`
},
{
msg: 'should be false if since property is invalid',
input: { lastUpdated: 10, since: -1, splitsData: {} },
output: false,
error: `${method}: preloadedData.since must be a positive number.`
},
{
msg: 'should be false if splitsData property is invalid',
input: { lastUpdated: 10, since: 10, splitsData: undefined },
output: false,
error: `${method}: preloadedData.splitsData must be a map of split names to their serialized definitions.`
},
{
msg: 'should be false if splitsData property is invalid',
input: { lastUpdated: 10, since: 10, splitsData: ['DEFINITION'] },
output: false,
error: `${method}: preloadedData.splitsData must be a map of split names to their serialized definitions.`
},
{
msg: 'should be false if splitsData property is invalid',
input: { lastUpdated: 10, since: 10, splitsData: { some_split: undefined } },
output: false,
error: `${method}: preloadedData.splitsData must be a map of split names to their serialized definitions.`
},
{
msg: 'should be false if mySegmentsData property is invalid',
input: { lastUpdated: 10, since: 10, splitsData: { some_split: 'DEFINITION' }, mySegmentsData: ['DEFINITION'] },
output: false,
error: `${method}: preloadedData.mySegmentsData must be a map of user keys to their list of segment names.`
},
{
msg: 'should be false if mySegmentsData property is invalid',
input: { lastUpdated: 10, since: 10, splitsData: { some_split: 'DEFINITION' }, mySegmentsData: { some_key: undefined } },
output: false,
error: `${method}: preloadedData.mySegmentsData must be a map of user keys to their list of segment names.`
},
{
msg: 'should be false if segmentsData property is invalid',
input: { lastUpdated: 10, since: 10, splitsData: { some_split: 'DEFINITION' }, segmentsData: ['DEFINITION'] },
output: false,
error: `${method}: preloadedData.segmentsData must be a map of segment names to their serialized definitions.`
},
{
msg: 'should be false if segmentsData property is invalid',
input: { lastUpdated: 10, since: 10, splitsData: { some_split: 'DEFINITION' }, segmentsData: { some_segment: undefined } },
output: false,
error: `${method}: preloadedData.segmentsData must be a map of segment names to their serialized definitions.`
}
];
tape('INPUT VALIDATION for preloadedData', assert => {
for (let i = 0; i < testCases.length; i++) {
const testCase = testCases[i];
assert.equal(validatePreloadedData(testCase.input, method), testCase.output, testCase.msg);
if (testCase.error) {
assert.ok(loggerMock.error.calledWithExactly(testCase.error), 'Should log the error for the invalid preloadedData.');
loggerMock.warn.resetHistory();
} else {
assert.true(loggerMock.error.notCalled, 'Should not log any error.');
}
if (testCase.warn) {
assert.ok(loggerMock.warn.calledWithExactly(testCase.warn), 'Should log the warning for the given preloadedData.');
loggerMock.warn.resetHistory();
} else {
assert.true(loggerMock.warn.notCalled, 'Should not log any warning.');
}
}
assert.end();
});