-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
utils.test.ts
158 lines (142 loc) · 3.73 KB
/
utils.test.ts
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
import { assertEquals as equals } from "../deps/assert.ts";
import { getImportMap, isPlainObject, merge, sha1 } from "../core/utils.ts";
Deno.test("merge options", () => {
interface Options {
foo: string;
foo2?: string;
}
const defaults: Options = {
foo: "bar",
};
const user: Partial<Options> = {
foo2: "bar2",
};
const expected: Options = {
foo: "bar",
foo2: "bar2",
};
equals(expected, merge(defaults, user));
});
Deno.test("merge inner options", () => {
interface Options {
foo: string;
foo2: SubOptions;
}
interface SubOptions {
bar1: string;
bar2?: string;
bar3?: string;
}
const defaults: Options = {
foo: "bar",
foo2: {
bar1: "bar1",
bar2: "bar2",
},
};
const user: Partial<Options> = {
foo: "new bar",
foo2: {
bar1: "new bar1",
bar3: "bar3",
},
};
const expected = {
foo: "new bar",
foo2: {
bar1: "new bar1",
bar2: "bar2",
bar3: "bar3",
},
};
equals(expected, merge(defaults, user));
});
Deno.test("isPlainObject", () => {
equals(isPlainObject({}), true);
equals(isPlainObject(new URL("http://localhost")), false);
equals(isPlainObject([]), false);
equals(isPlainObject(new Map()), false);
equals(isPlainObject(new Set()), false);
equals(isPlainObject(Symbol.for("foo")), false);
});
Deno.test("sha1 function", async () => {
const data = "Hello World";
const dataUint8 = new TextEncoder().encode(data);
const expected = "\nMU��x�\x02/�p\x19w��@��";
equals(await sha1(data), expected);
equals(await sha1(dataUint8), expected);
});
Deno.test("import map", () => {
const map = getImportMap();
equals(map.imports["lume"], new URL("../mod.ts", import.meta.url).href);
equals(map.imports["lume/"], new URL("../", import.meta.url).href);
equals(map.imports["lume/"], new URL("../", import.meta.url).href);
equals(map, {
imports: {
"lume": new URL("../mod.ts", import.meta.url).href,
"lume/": new URL("../", import.meta.url).href,
"https://deno.land/x/lume/": new URL("../", import.meta.url).href,
},
});
});
Deno.test("merge import map", async () => {
const map = await getImportMap({
imports: {
"lume": "https://lume.land/lume.ts",
"std/": "https://deno.land/std@0.121.0/",
"/": "./",
},
scopes: {
"foo/": {
"std/": "https://deno.land/std@0.121.0/foo/",
"/": "./foo/",
},
},
});
equals(map, {
imports: {
"lume": "https://lume.land/lume.ts",
"lume/": new URL("../", import.meta.url).href,
"https://deno.land/x/lume/": new URL("../", import.meta.url).href,
"std/": "https://deno.land/std@0.121.0/",
"/": "./",
},
scopes: {
"foo/": {
"std/": "https://deno.land/std@0.121.0/foo/",
"/": "./foo/",
},
},
});
});
Deno.test("merge and resolve import map", async () => {
const path = new URL("./assets/import_map.json", import.meta.url);
const map = await getImportMap({
imports: {
"lume": "https://lume.land/lume.ts",
"std/": "https://deno.land/std@0.121.0/",
"/": "./",
},
scopes: {
"foo/": {
"std/": "https://deno.land/std@0.121.0/foo/",
"/": "./foo/",
},
},
}, path);
equals(map, {
imports: {
"lume": "https://lume.land/lume.ts",
"lume/": new URL("../", import.meta.url).href,
"https://deno.land/x/lume/": new URL("../", import.meta.url).href,
"std/": "https://deno.land/std@0.121.0/",
"/": new URL("./assets/", import.meta.url).href,
},
scopes: {
"foo/": {
"std/": "https://deno.land/std@0.121.0/foo/",
"/": new URL("./assets/foo/", import.meta.url).href,
},
},
});
});