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/util/fill/fill.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
import { fill } from "./fill";
7
import { expectType } from "tsd";
8
9
test("Supplied default should be merged in to target even if marked undefined", () => {
10
const opts: { name: string; height?: number } = {
11
name: "jack",
12
height: undefined,
13
};
14
const actual = fill(opts, { height: 20 });
15
expect(actual).toStrictEqual({ name: "jack", height: 20 });
16
});
17
18
test("Defaults should not overwrite already defined optional params", () => {
19
const opts: { name: string; height?: number; weight?: number } = {
20
name: "jack",
21
height: 20,
22
};
23
const actual = fill(opts, { height: 30 });
24
expect(actual).toStrictEqual({ name: "jack", height: 20 });
25
});
26
27
test("Missing optional params should not appear if not given defaults", () => {
28
const opts: { name: string; height?: number; weight?: number } = {
29
name: "jack",
30
};
31
const actual = fill(opts, { height: 20 });
32
expect(actual).toStrictEqual({ name: "jack", height: 20 });
33
});
34
35
test("Supplied default should guarantee type existance", () => {
36
type Expected = {
37
name: string;
38
direction: "up" | "down" | "left" | "right";
39
highlight: boolean;
40
animate?: boolean;
41
};
42
43
const opts: {
44
name: string;
45
direction: "up" | "down" | "left" | "right";
46
highlight?: boolean;
47
animate?: boolean;
48
} = { name: "foo", direction: "up" };
49
50
const actual = fill(opts, { highlight: false });
51
52
expectType<Expected>(actual);
53
});
54
55
test("strings", () => {
56
function filled(props: {
57
name: string;
58
direction: "up" | "down" | "left" | "right";
59
highlight?: string;
60
animate?: boolean;
61
}) {
62
// This should not end up narrowing to the fixed value
63
return fill(props, { highlight: "fixed_string" });
64
}
65
const a = filled({ name: "foo", direction: "up" });
66
expectType<string>(a.name);
67
expectType<"up" | "down" | "left" | "right">(a.direction);
68
expectType<string>(a.highlight);
69
expectType<boolean | undefined>(a.animate);
70
});
71
72
// tsd expectError doesn't integrate into Jest
73
test("Errors", () => {
74
/*
75
function prop_typed_errors(props: {
76
name: string;
77
direction: "up" | "down" | "left" | "right";
78
highlight?: boolean;
79
animate?: boolean;
80
}) {
81
// Don't allow requireds to even be listed
82
return fill(props, { name: "undefined", highlight: false });
83
}
84
*/
85
});
86
87