-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtoPaths.spec.js
262 lines (236 loc) · 7.18 KB
/
toPaths.spec.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
var expect = require('chai').expect;
var pathUtils = require('..');
var toPaths = pathUtils.toPaths;
var toTree = pathUtils.toTree;
var isIntegerKey = pathUtils.isIntegerKey;
describe('toPaths', function() {
it('toPaths a pathmap that has overlapping branch and leaf nodes', function() {
var pathMaps = [null, {
lolomo: 1
}, {
lolomo: {
summary: 1,
13: 1,
14: 1
}
}, {
lolomo: {
15: {
rating: 1,
summary: 1
},
13: {
summary: 1
},
16: {
rating: 1,
summary: 1
},
14: {
summary: 1
},
17: {
rating: 1,
summary: 1
}
}
}];
var paths = toPaths(pathMaps).sort(function(a, b) {
return a.length - b.length;
});
var first = paths[0];
var second = paths[1];
var third = paths[2];
var fourth = paths[3];
expect(first[0] === 'lolomo').to.equal(true);
expect((
second[0] === 'lolomo') && (
second[1][0] === 13) && (
second[1][1] === 14) && (
second[1][2] === 'summary')
).to.equal(true);
expect((third[0] === 'lolomo') && (
third[1].from === 13) && (
third[1].to === 14) && (
third[2] === 'summary')
).to.equal(true);
expect((fourth[0] === 'lolomo') && (
fourth[1].from === 15) && (
fourth[1].to === 17) && (
fourth[2][0] === 'rating') && (
fourth[2][1] === 'summary')
).to.equal(true);
});
it('toPaths should not coerce numbers to strings outside the safe range', function() {
/*
* For reference:
* https://github.com/Netflix/falcor-router/issues/176
* https://github.com/Netflix/falcor-router/issues/68
* https://github.com/Netflix/falcor-path-utils/pull/6
*/
var pathMaps = [null, {
lolomo: 1
}, {
lolomo: {
'0': 1,
'1': 1,
'234': 1,
'345678': 1,
'4253674286': 1,
'9007199254740991': 1,
'9007199254740992': 1,
'918572487653498743278645': 1,
}
}];
var paths = toPaths(pathMaps).sort(function(a, b) {
return a.length - b.length;
});
// NOTE:
// chai equal() is strict such that expect(4).to.equal('4')
// will fail and vice versa.
expect(paths[1][1][0]).to.equal(0);
expect(paths[1][1][1]).to.equal(1);
expect(paths[1][1][2]).to.equal(234);
expect(paths[1][1][3]).to.equal(345678);
expect(paths[1][1][4]).to.equal(4253674286);
expect(paths[1][1][5]).to.equal(9007199254740991); // max safe int
expect(paths[1][1][6]).to.equal('9007199254740992'); // max safe int + 1
expect(paths[1][1][7]).to.equal('918572487653498743278645'); // absurdly large
});
it('should explode a simplePath.', function() {
var out = ['one', 'two'];
var input = {2: {one: {two: null}}};
expect(toPaths(input)).to.deep.equals([out]);
});
it('should explode a complex.', function() {
var input = {2: {one: {two: null, three: null}}};
var out = ['one', ['three', 'two']];
var output = toPaths(input);
output[0][1].sort();
expect(output).to.deep.equals([out]);
});
it('should explode a set of complex and simple paths.', function() {
var out = [
['one', ['three', 'two']],
['one', {from: 0, to: 3}, 'summary']
];
var input = {
2: {
one: {
three: null,
two: null
}
},
3: {
one: {
0: { summary: null },
1: { summary: null },
2: { summary: null },
3: { summary: null }
}
}
};
var output = toPaths(input);
if (!Array.isArray(output[0][1])) {
var tmp = output[0];
output[0] = output[1];
output[1] = tmp;
}
output[0][1].sort();
expect(output).to.deep.equals(out);
});
it('should translate between toPaths and toTrees', function() {
var expectedTree = {
one: {
0: { summary: null },
1: { summary: null },
2: { summary: null },
3: { summary: null },
three: null,
two: null
}
};
var treeMap = {
2: {
one: {
three: null,
two: null
}
},
3: {
one: {
0: { summary: null },
1: { summary: null },
2: { summary: null },
3: { summary: null }
}
}
};
expect(toTree(toPaths(treeMap))).to.deep.equals(expectedTree);
});
describe('isIntegerKey', function() {
var thingsThatShouldReturnTrue = [
0,
1,
-0,
-1,
10,
-10,
9007199254740991, // max safe int
-9007199254740991, // min safe int
'0',
'1',
'-1',
'10',
'-10',
'9007199254740991', // max safe int
'-9007199254740991', // min safe int
];
thingsThatShouldReturnTrue.forEach(function(thing) {
var should = 'should return true on ' + JSON.stringify(thing);
it(should, function() {
expect(isIntegerKey(thing)).to.equal(true);
});
});
var thingsThatShouldReturnFalse = [
[],
{},
null,
true,
false,
undefined,
NaN,
Infinity,
-Infinity,
'9007199254740992', // max safe int + 1
'-9007199254740992', // min safe int - 1
9007199254740992, // max safe int + 1
-9007199254740992, // min safe int - 1
'648365838265483646384563538',
'-0',
'',
'01',
'0d',
'1d',
'_',
' 1',
'- 1',
' ',
'0x123',
'0b1101',
'deadbeef',
0.1,
-0.1,
'1.0',
'-1.0',
'0.1',
'-0.1',
];
thingsThatShouldReturnFalse.forEach(function(thing) {
var should = 'should return false on ' + JSON.stringify(thing);
it(should, function() {
expect(isIntegerKey(thing)).to.equal(false);
});
});
});
});