CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/app-framework/__tests__/getIn.test.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/**
7
* Need to test permutations of (at all depths):
8
* TypedMap -> immutable.List -> immutable.Map
9
*
10
*/
11
12
13
import * as Immutable from "immutable";
14
import { expectType } from "tsd";
15
16
import { TypedCollectionMethods } from "@cocalc/util/types/immutable-types";
17
import { TypedMap } from "@cocalc/util/types/typed-map";
18
19
// TODO: Iterate through appropriate base types
20
type BASE = string;
21
const assign: BASE = "0";
22
23
type GetIn<T> = TypedCollectionMethods<T>["getIn"];
24
const STUB = (() => null) as any; // No implementation
25
26
// Seems trivial an unecessary but imagine a variable array that may turn
27
// out to have only one arg. We still want to type that correctly.
28
describe("length 1", () => {
29
test("[obj]", () => {
30
type T = { str: BASE };
31
const getIn: GetIn<T> = STUB;
32
let test = getIn(["str"]);
33
test = assign; // Checks against never
34
expectType<BASE>(test); // Finally check against the BASE type again
35
});
36
test("[Array]", () => {
37
type T = BASE[];
38
const getIn: GetIn<T> = STUB;
39
let test = getIn([0]);
40
test = assign;
41
expectType<BASE>(test);
42
});
43
test("[TypedMap]", () => {
44
type T = TypedMap<{ str: BASE }>;
45
const getIn: GetIn<T> = STUB;
46
let test = getIn(["str"]);
47
test = assign; // Checks against never
48
expectType<BASE>(test); // Finally check against the BASE type again
49
});
50
test("[List]", () => {
51
type T = Immutable.List<BASE>;
52
const getIn: GetIn<T> = STUB;
53
let test = getIn([0]);
54
test = assign;
55
expectType<BASE>(test);
56
});
57
test("[Map]", () => {
58
type T = Immutable.Map<string, BASE>;
59
const getIn: GetIn<T> = STUB;
60
let test = getIn(["anystr"]);
61
test = assign; // Checks against never
62
expectType<BASE>(test); // Finally check against the BASE type again
63
});
64
});
65
66
describe("length 2", () => {
67
test("[obj, obj]", () => {
68
type T = { foo: { str: BASE } };
69
const getIn: GetIn<T> = STUB;
70
let test = getIn(["foo", "str"]);
71
test = assign; // Checks against never
72
expectType<BASE>(test); // Finally check against the BASE type again
73
});
74
test("[obj, Array]", () => {
75
type T = { foo: BASE[] };
76
const getIn: GetIn<T> = STUB;
77
let test = getIn(["foo", 0]);
78
test = assign;
79
expectType<BASE>(test);
80
});
81
test("[obj, TypedMap]", () => {
82
type T = { foo: TypedMap<{ str: BASE }> };
83
const getIn: GetIn<T> = STUB;
84
let test = getIn(["foo", "str"]);
85
test = assign; // Checks against never
86
expectType<BASE>(test); // Finally check against the BASE type again
87
});
88
test("[obj, List]", () => {
89
type T = { foo: Immutable.List<BASE> };
90
const getIn: GetIn<T> = STUB;
91
let test = getIn(["foo", 0]);
92
test = assign;
93
expectType<BASE>(test);
94
});
95
test("[obj, Map]", () => {
96
type T = { foo: Immutable.Map<string, BASE> };
97
const getIn: GetIn<T> = STUB;
98
let test = getIn(["foo", "anystr"]);
99
test = assign; // Checks against never
100
expectType<BASE>(test); // Finally check against the BASE type again
101
});
102
103
test("[Array, obj]", () => {
104
type T = { str: BASE }[];
105
const getIn: GetIn<T> = STUB;
106
let test = getIn([0, "str"]);
107
108
test = assign; // Checks against never
109
expectType<BASE>(test); // Finally check against the BASE type again
110
});
111
test("[Array, Array]", () => {
112
type T = BASE[][];
113
const getIn: GetIn<T> = STUB;
114
let test = getIn([0, 0]);
115
test = assign;
116
expectType<BASE>(test);
117
});
118
test("[Array, TypedMap]", () => {
119
type T = TypedMap<{ str: BASE }>[];
120
const getIn: GetIn<T> = STUB;
121
let test = getIn([0, "str"]);
122
test = assign; // Checks against never
123
expectType<BASE>(test); // Finally check against the BASE type again
124
});
125
test("[Array, List]", () => {
126
type T = Immutable.List<BASE>[];
127
const getIn: GetIn<T> = STUB;
128
let test = getIn([0, 0]);
129
test = assign;
130
expectType<BASE>(test);
131
});
132
test("[Array, Map]", () => {
133
type T = Immutable.Map<string, BASE>[];
134
const getIn: GetIn<T> = STUB;
135
let test = getIn([0, "anystr"]);
136
test = assign; // Checks against never
137
expectType<BASE>(test); // Finally check against the BASE type again
138
});
139
});
140
141
describe("length 3", () => {
142
describe("object -> L3", () => {
143
test("[obj, obj, obj]", () => {
144
type T = { foo: { bar: { str: BASE } } };
145
const getIn: GetIn<T> = STUB;
146
let test = getIn(["foo", "bar", "str"]);
147
test = assign; // Checks against never
148
expectType<BASE>(test); // Finally check against the BASE type again
149
});
150
test("[obj, obj, Array]", () => {
151
type T = { foo: { bar: BASE[] } };
152
const getIn: GetIn<T> = STUB;
153
let test = getIn(["foo", "bar", 0]);
154
test = assign;
155
expectType<BASE>(test);
156
});
157
test("[obj, obj, TypedMap]", () => {
158
type T = { foo: { bar: TypedMap<{ str: BASE }> } };
159
const getIn: GetIn<T> = STUB;
160
let test = getIn(["foo", "bar", "str"]);
161
test = assign; // Checks against never
162
expectType<BASE>(test); // Finally check against the BASE type again
163
});
164
test("[obj, obj, List]", () => {
165
type T = { foo: { bar: Immutable.List<BASE> } };
166
const getIn: GetIn<T> = STUB;
167
let test = getIn(["foo", "bar", 0]);
168
test = assign;
169
expectType<BASE>(test);
170
});
171
test("[obj, obj, Map]", () => {
172
type T = { foo: { bar: Immutable.Map<string, BASE> } };
173
const getIn: GetIn<T> = STUB;
174
let test = getIn(["foo", "bar", "anystr"]);
175
test = assign; // Checks against never
176
expectType<BASE>(test); // Finally check against the BASE type again
177
});
178
});
179
180
describe("Array -> L3", () => {
181
test("[obj, Array, obj]", () => {
182
type T = { foo: { str: BASE }[] };
183
const getIn: GetIn<T> = STUB;
184
let test = getIn(["foo", 0, "str"]);
185
test = assign; // Checks against never
186
expectType<BASE>(test); // Finally check against the BASE type again
187
});
188
test("[obj, Array, Array]", () => {
189
type T = { foo: BASE[][] };
190
const getIn: GetIn<T> = STUB;
191
let test = getIn(["foo", 2, 0]);
192
test = assign;
193
expectType<BASE>(test);
194
});
195
test("[obj, Array, TypedMap]", () => {
196
type T = { foo: TypedMap<{ str: BASE }>[] };
197
const getIn: GetIn<T> = STUB;
198
let test = getIn(["foo", 2, "str"]);
199
test = assign; // Checks against never
200
expectType<BASE>(test); // Finally check against the BASE type again
201
});
202
test("[obj, Array, List]", () => {
203
type T = { foo: Immutable.List<BASE>[] };
204
const getIn: GetIn<T> = STUB;
205
let test = getIn(["foo", 2, 0]);
206
test = assign;
207
expectType<BASE>(test);
208
});
209
test("[obj, Array, Map]", () => {
210
type T = { foo: Immutable.Map<string, BASE>[] };
211
const getIn: GetIn<T> = STUB;
212
let test = getIn(["foo", 2, "anystr"]);
213
test = assign; // Checks against never
214
expectType<BASE>(test); // Finally check against the BASE type again
215
});
216
});
217
218
describe("TypedMap -> L3", () => {
219
test("[obj, TypedMap, obj]", () => {
220
type T = { foo: TypedMap<{ bar: { str: BASE } }> };
221
const getIn: GetIn<T> = STUB;
222
let test = getIn(["foo", "bar", "str"]);
223
test = assign; // Checks against never
224
expectType<BASE>(test); // Finally check against the BASE type again
225
});
226
test("[obj, TypedMap, Array]", () => {
227
type T = { foo: TypedMap<{ bar: BASE[] }> };
228
const getIn: GetIn<T> = STUB;
229
let test = getIn(["foo", "bar", 0]);
230
test = assign;
231
expectType<BASE>(test);
232
});
233
test("[obj, TypedMap, TypedMap]", () => {
234
type T = { foo: TypedMap<{ bar: TypedMap<{ str: BASE }> }> };
235
const getIn: GetIn<T> = STUB;
236
let test = getIn(["foo", "bar", "str"]);
237
test = assign; // Checks against never
238
expectType<BASE>(test); // Finally check against the BASE type again
239
});
240
test("[obj, TypedMap, List]", () => {
241
type T = { foo: TypedMap<{ bar: Immutable.List<BASE> }> };
242
const getIn: GetIn<T> = STUB;
243
let test = getIn(["foo", "bar", 0]);
244
test = assign;
245
expectType<BASE>(test);
246
});
247
test("[obj, TypedMap, Map]", () => {
248
type T = { foo: TypedMap<{ bar: Immutable.Map<string, BASE> }> };
249
const getIn: GetIn<T> = STUB;
250
let test = getIn(["foo", "bar", "anystr"]);
251
test = assign; // Checks against never
252
expectType<BASE>(test); // Finally check against the BASE type again
253
});
254
});
255
256
describe("List -> L3", () => {
257
test("[obj, List, obj]", () => {
258
type T = { foo: Immutable.List<{ str: BASE }> };
259
const getIn: GetIn<T> = STUB;
260
let test = getIn(["foo", 0, "str"]);
261
test = assign; // Checks against never
262
expectType<BASE>(test); // Finally check against the BASE type again
263
});
264
test("[obj, List, TypedMap]", () => {
265
type T = { foo: Immutable.List<TypedMap<{ str: BASE }>> };
266
const getIn: GetIn<T> = STUB;
267
let test = getIn(["foo", 0, "str"]);
268
test = assign; // Checks against never
269
expectType<BASE>(test); // Finally check against the BASE type again
270
});
271
test("[obj, List, List]", () => {
272
type T = { foo: Immutable.List<Immutable.List<BASE>> };
273
const getIn: GetIn<T> = STUB;
274
let test = getIn(["foo", 0, 0]);
275
test = assign;
276
expectType<BASE>(test);
277
});
278
test("[obj, List, Array]", () => {
279
type T = { foo: Immutable.List<BASE[]> };
280
const getIn: GetIn<T> = STUB;
281
let test = getIn(["foo", 0, 0]);
282
test = assign;
283
expectType<BASE>(test);
284
});
285
test("[obj, List, Map]", () => {
286
type T = { foo: Immutable.List<Immutable.Map<string, BASE>> };
287
const getIn: GetIn<T> = STUB;
288
let test = getIn(["foo", 0, "anystr"]);
289
test = assign; // Checks against never
290
expectType<BASE>(test); // Finally check against the BASE type again
291
});
292
});
293
294
describe("Map -> L3", () => {
295
test("[obj, Map, obj]", () => {
296
type T = { foo: Immutable.Map<string, { str: BASE }> };
297
const getIn: GetIn<T> = STUB;
298
let test = getIn(["foo", "anystr", "str"]);
299
test = assign; // Checks against never
300
expectType<BASE>(test); // Finally check against the BASE type again
301
});
302
test("[obj, Map, TypedMap]", () => {
303
type T = { foo: Immutable.Map<string, TypedMap<{ str: BASE }>> };
304
const getIn: GetIn<T> = STUB;
305
let test = getIn(["foo", "anystr", "str"]);
306
test = assign; // Checks against never
307
expectType<BASE>(test); // Finally check against the BASE type again
308
});
309
test("[obj, Map, List]", () => {
310
type T = { foo: Immutable.Map<string, Immutable.List<BASE>> };
311
const getIn: GetIn<T> = STUB;
312
let test = getIn(["foo", "anystr", 0]);
313
test = assign;
314
expectType<BASE>(test);
315
});
316
test("[obj, Map, Array]", () => {
317
type T = { foo: Immutable.Map<string, BASE[]> };
318
const getIn: GetIn<T> = STUB;
319
let test = getIn(["foo", "anystr", 0]);
320
test = assign;
321
expectType<BASE>(test);
322
});
323
test("[obj, Map, Map]", () => {
324
type T = { foo: Immutable.Map<string, Immutable.Map<string, BASE>> };
325
const getIn: GetIn<T> = STUB;
326
let test = getIn(["foo", "anystr", "anystr"]);
327
test = assign; // Checks against never
328
expectType<BASE>(test); // Finally check against the BASE type again
329
});
330
});
331
});
332
333
describe("length 4", () => {
334
describe("object -> L4", () => {
335
test("[obj, obj, obj, obj]", () => {
336
type T = { foo: { foo: { bar: { str: BASE } } } };
337
const getIn: GetIn<T> = STUB;
338
let test = getIn(["foo", "foo", "bar", "str"]);
339
test = assign; // Checks against never
340
expectType<BASE>(test); // Finally check against the BASE type again
341
});
342
test("[obj, obj, obj, Array]", () => {
343
type T = { foo: { foo: { bar: BASE[] } } };
344
const getIn: GetIn<T> = STUB;
345
let test = getIn(["foo", "foo", "bar", 0]);
346
test = assign;
347
expectType<BASE>(test);
348
});
349
test("[obj, obj, obj, TypedMap]", () => {
350
type T = { foo: { foo: { bar: TypedMap<{ str: BASE }> } } };
351
const getIn: GetIn<T> = STUB;
352
let test = getIn(["foo", "foo", "bar", "str"]);
353
test = assign; // Checks against never
354
expectType<BASE>(test); // Finally check against the BASE type again
355
});
356
test("[obj, obj, obj, List]", () => {
357
type T = { foo: { foo: { bar: Immutable.List<BASE> } } };
358
const getIn: GetIn<T> = STUB;
359
let test = getIn(["foo", "foo", "bar", 0]);
360
test = assign;
361
expectType<BASE>(test);
362
});
363
test("[obj, obj, obj, Map]", () => {
364
type T = { foo: { foo: { bar: Immutable.Map<string, BASE> } } };
365
const getIn: GetIn<T> = STUB;
366
let test = getIn(["foo", "foo", "bar", "anystr"]);
367
test = assign; // Checks against never
368
expectType<BASE>(test); // Finally check against the BASE type again
369
});
370
});
371
372
describe("Array -> L4", () => {
373
test("[obj, obj, Array, obj]", () => {
374
type T = { foo: { foo: { str: BASE }[] } };
375
const getIn: GetIn<T> = STUB;
376
let test = getIn(["foo", "foo", 0, "str"]);
377
test = assign; // Checks against never
378
expectType<BASE>(test); // Finally check against the BASE type again
379
});
380
test("[obj, obj, Array, Array]", () => {
381
type T = { foo: { foo: BASE[][] } };
382
const getIn: GetIn<T> = STUB;
383
let test = getIn(["foo", "foo", 2, 0]);
384
test = assign;
385
expectType<BASE>(test);
386
});
387
test("[obj, obj, Array, TypedMap]", () => {
388
type T = { foo: { foo: TypedMap<{ str: BASE }>[] } };
389
const getIn: GetIn<T> = STUB;
390
let test = getIn(["foo", "foo", 2, "str"]);
391
test = assign; // Checks against never
392
expectType<BASE>(test); // Finally check against the BASE type again
393
});
394
test("[obj, obj, Array, List]", () => {
395
type T = { foo: { foo: Immutable.List<BASE>[] } };
396
const getIn: GetIn<T> = STUB;
397
let test = getIn(["foo", "foo", 2, 0]);
398
test = assign;
399
expectType<BASE>(test);
400
});
401
test("[obj, obj, Array, Map]", () => {
402
type T = { foo: { foo: Immutable.Map<string, BASE>[] } };
403
const getIn: GetIn<T> = STUB;
404
let test = getIn(["foo", "foo", 2, "anystr"]);
405
test = assign; // Checks against never
406
expectType<BASE>(test); // Finally check against the BASE type again
407
});
408
});
409
410
describe("TypedMap -> L4", () => {
411
test("[obj, obj, TypedMap, obj]", () => {
412
type T = { foo: { foo: TypedMap<{ bar: { str: BASE } }> } };
413
const getIn: GetIn<T> = STUB;
414
let test = getIn(["foo", "foo", "bar", "str"]);
415
416
test = assign; // Checks against never
417
expectType<BASE>(test); // Finally check against the BASE type again
418
});
419
test("[obj, obj, TypedMap, Array]", () => {
420
type T = { foo: { foo: TypedMap<{ bar: BASE[] }> } };
421
const getIn: GetIn<T> = STUB;
422
let test = getIn(["foo", "foo", "bar", 0]);
423
test = assign;
424
expectType<BASE>(test);
425
});
426
test("[obj, obj, TypedMap, TypedMap]", () => {
427
type T = { foo: { foo: TypedMap<{ bar: TypedMap<{ str: BASE }> }> } };
428
const getIn: GetIn<T> = STUB;
429
let test = getIn(["foo", "foo", "bar", "str"]);
430
test = assign; // Checks against never
431
expectType<BASE>(test); // Finally check against the BASE type again
432
});
433
test("[obj, obj, TypedMap, List]", () => {
434
type T = { foo: { foo: TypedMap<{ bar: Immutable.List<BASE> }> } };
435
const getIn: GetIn<T> = STUB;
436
let test = getIn(["foo", "foo", "bar", 0]);
437
test = assign;
438
expectType<BASE>(test);
439
});
440
test("[obj, obj, TypedMap, Map]", () => {
441
type T = { foo: { foo: TypedMap<{ bar: Immutable.Map<string, BASE> }> } };
442
const getIn: GetIn<T> = STUB;
443
let test = getIn(["foo", "foo", "bar", "anystr"]);
444
test = assign; // Checks against never
445
expectType<BASE>(test); // Finally check against the BASE type again
446
});
447
});
448
449
describe("List -> L4", () => {
450
test("[obj, obj, List, obj]", () => {
451
type T = { foo: { foo: Immutable.List<{ str: BASE }> } };
452
const getIn: GetIn<T> = STUB;
453
let test = getIn(["foo", "foo", 0, "str"]);
454
455
test = assign; // Checks against never
456
expectType<BASE>(test); // Finally check against the BASE type again
457
});
458
test("[obj, obj, List, TypedMap]", () => {
459
type T = { foo: { foo: Immutable.List<TypedMap<{ str: BASE }>> } };
460
const getIn: GetIn<T> = STUB;
461
let test = getIn(["foo", "foo", 0, "str"]);
462
test = assign; // Checks against never
463
expectType<BASE>(test); // Finally check against the BASE type again
464
});
465
test("[obj, obj, List, List]", () => {
466
type T = { foo: { foo: Immutable.List<Immutable.List<BASE>> } };
467
const getIn: GetIn<T> = STUB;
468
let test = getIn(["foo", "foo", 0, 0]);
469
test = assign;
470
expectType<BASE>(test);
471
});
472
test("[obj, obj, List, Array]", () => {
473
type T = { foo: { foo: Immutable.List<BASE[]> } };
474
const getIn: GetIn<T> = STUB;
475
let test = getIn(["foo", "foo", 0, 0]);
476
test = assign;
477
expectType<BASE>(test);
478
});
479
test("[obj, obj, List, Map]", () => {
480
type T = { foo: { foo: Immutable.List<Immutable.Map<string, BASE>> } };
481
const getIn: GetIn<T> = STUB;
482
let test = getIn(["foo", "foo", 0, "anystr"]);
483
test = assign; // Checks against never
484
expectType<BASE>(test); // Finally check against the BASE type again
485
});
486
});
487
488
describe("Map -> L4", () => {
489
test("[obj, obj, Map, obj]", () => {
490
type T = { foo: { foo: Immutable.Map<string, { str: BASE }> } };
491
const getIn: GetIn<T> = STUB;
492
let test = getIn(["foo", "foo", "anystr", "str"]);
493
test = assign; // Checks against never
494
expectType<BASE>(test); // Finally check against the BASE type again
495
});
496
test("[obj, obj, Map, TypedMap]", () => {
497
type T = { foo: { foo: Immutable.Map<string, TypedMap<{ str: BASE }>> } };
498
const getIn: GetIn<T> = STUB;
499
let test = getIn(["foo", "foo", "anystr", "str"]);
500
test = assign; // Checks against never
501
expectType<BASE>(test); // Finally check against the BASE type again
502
});
503
test("[obj, obj, Map, List]", () => {
504
type T = { foo: { foo: Immutable.Map<string, Immutable.List<BASE>> } };
505
const getIn: GetIn<T> = STUB;
506
let test = getIn(["foo", "foo", "anystr", 0]);
507
test = assign;
508
expectType<BASE>(test);
509
});
510
test("[obj, obj, Map, Array]", () => {
511
type T = { foo: { foo: Immutable.Map<string, BASE[]> } };
512
const getIn: GetIn<T> = STUB;
513
let test = getIn(["foo", "foo", "anystr", 0]);
514
test = assign;
515
expectType<BASE>(test);
516
});
517
test("[obj, obj, Map, Map]", () => {
518
type T = {
519
foo: { foo: Immutable.Map<string, Immutable.Map<string, BASE>> };
520
};
521
const getIn: GetIn<T> = STUB;
522
let test = getIn(["foo", "foo", "anystr", "anystr"]);
523
test = assign; // Checks against never
524
expectType<BASE>(test); // Finally check against the BASE type again
525
});
526
});
527
});
528
529