-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule-map.test.ts
More file actions
249 lines (201 loc) · 6.54 KB
/
module-map.test.ts
File metadata and controls
249 lines (201 loc) · 6.54 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
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
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
import * as assert from "@std/assert";
import {
addModule,
createModuleMap,
getAllChunks,
getModule,
hasModule,
type ModuleEntry,
} from "./module-map.ts";
// ============================================================================
// createModuleMap tests
// ============================================================================
Deno.test("createModuleMap returns empty object", () => {
const map = createModuleMap();
assert.assertEquals(map, {});
assert.assertEquals(Object.keys(map).length, 0);
});
// ============================================================================
// addModule tests
// ============================================================================
Deno.test("addModule adds module by ID", () => {
const map = createModuleMap();
const entry: ModuleEntry = {
id: "./src/App.tsx",
name: "App",
chunks: ["main.js"],
};
const updated = addModule(map, entry);
assert.assertExists(updated["./src/App.tsx"]);
assert.assertEquals(updated["./src/App.tsx"], entry);
});
Deno.test("addModule preserves existing modules", () => {
let map = createModuleMap();
const entry1: ModuleEntry = {
id: "./src/A.tsx",
name: "A",
chunks: ["a.js"],
};
const entry2: ModuleEntry = {
id: "./src/B.tsx",
name: "B",
chunks: ["b.js"],
};
map = addModule(map, entry1);
map = addModule(map, entry2);
assert.assertExists(map["./src/A.tsx"]);
assert.assertExists(map["./src/B.tsx"]);
assert.assertEquals(Object.keys(map).length, 2);
});
Deno.test("addModule is immutable - does not modify original", () => {
const original = createModuleMap();
const entry: ModuleEntry = {
id: "./src/New.tsx",
name: "New",
chunks: ["new.js"],
};
const updated = addModule(original, entry);
assert.assertEquals(Object.keys(original).length, 0);
assert.assertEquals(Object.keys(updated).length, 1);
});
Deno.test("addModule allows updating existing module", () => {
let map = createModuleMap();
const entry1: ModuleEntry = {
id: "./src/App.tsx",
name: "App",
chunks: ["old.js"],
};
const entry2: ModuleEntry = {
id: "./src/App.tsx",
name: "UpdatedApp",
chunks: ["new.js"],
};
map = addModule(map, entry1);
map = addModule(map, entry2);
assert.assertEquals(Object.keys(map).length, 1);
assert.assertEquals(map["./src/App.tsx"]?.name, "UpdatedApp");
assert.assertEquals(map["./src/App.tsx"]?.chunks[0], "new.js");
});
// ============================================================================
// getModule tests
// ============================================================================
Deno.test("getModule returns module by ID", () => {
let map = createModuleMap();
const entry: ModuleEntry = {
id: "./src/Counter.tsx",
name: "Counter",
chunks: ["counter-chunk.js"],
};
map = addModule(map, entry);
const result = getModule(map, "./src/Counter.tsx");
assert.assertEquals(result, entry);
});
Deno.test("getModule returns undefined for non-existent ID", () => {
const map = createModuleMap();
const result = getModule(map, "./src/NotFound.tsx");
assert.assertEquals(result, undefined);
});
// ============================================================================
// hasModule tests
// ============================================================================
Deno.test("hasModule returns true for existing module", () => {
let map = createModuleMap();
const entry: ModuleEntry = {
id: "./src/Button.tsx",
name: "Button",
chunks: ["button.js"],
};
map = addModule(map, entry);
assert.assertEquals(hasModule(map, "./src/Button.tsx"), true);
});
Deno.test("hasModule returns false for non-existent module", () => {
const map = createModuleMap();
assert.assertEquals(hasModule(map, "./src/Missing.tsx"), false);
});
// ============================================================================
// getAllChunks tests
// ============================================================================
Deno.test("getAllChunks returns empty array for empty map", () => {
const map = createModuleMap();
const chunks = getAllChunks(map);
assert.assertEquals(chunks, []);
});
Deno.test("getAllChunks returns all unique chunks", () => {
let map = createModuleMap();
map = addModule(map, {
id: "./A.tsx",
name: "A",
chunks: ["chunk-a.js", "shared.js"],
});
map = addModule(map, {
id: "./B.tsx",
name: "B",
chunks: ["chunk-b.js", "vendor.js"],
});
const chunks = getAllChunks(map);
assert.assertEquals(chunks.length, 4);
assert.assert(chunks.includes("chunk-a.js"));
assert.assert(chunks.includes("chunk-b.js"));
assert.assert(chunks.includes("shared.js"));
assert.assert(chunks.includes("vendor.js"));
});
Deno.test("getAllChunks deduplicates chunks across modules", () => {
let map = createModuleMap();
map = addModule(map, {
id: "./A.tsx",
name: "A",
chunks: ["common.js", "a.js"],
});
map = addModule(map, {
id: "./B.tsx",
name: "B",
chunks: ["common.js", "b.js"],
});
map = addModule(map, {
id: "./C.tsx",
name: "C",
chunks: ["common.js", "c.js"],
});
const chunks = getAllChunks(map);
// common.js should only appear once
assert.assertEquals(chunks.filter((c) => c === "common.js").length, 1);
assert.assertEquals(chunks.length, 4); // common, a, b, c
});
Deno.test("getAllChunks handles modules with overlapping chunks", () => {
let map = createModuleMap();
map = addModule(map, {
id: "./A.tsx",
name: "A",
chunks: ["vendor.js", "react.js"],
});
map = addModule(map, {
id: "./B.tsx",
name: "B",
chunks: ["vendor.js", "react.js", "extra.js"],
});
const chunks = getAllChunks(map);
assert.assertEquals(chunks.length, 3); // vendor, react, extra
assert.assertEquals(chunks.filter((c) => c === "vendor.js").length, 1);
assert.assertEquals(chunks.filter((c) => c === "react.js").length, 1);
});
Deno.test("getAllChunks handles single module", () => {
let map = createModuleMap();
map = addModule(map, {
id: "./Single.tsx",
name: "Single",
chunks: ["single.js"],
});
const chunks = getAllChunks(map);
assert.assertEquals(chunks, ["single.js"]);
});
Deno.test("getAllChunks handles module with multiple chunks", () => {
let map = createModuleMap();
map = addModule(map, {
id: "./Large.tsx",
name: "Large",
chunks: ["chunk1.js", "chunk2.js", "chunk3.js", "chunk4.js"],
});
const chunks = getAllChunks(map);
assert.assertEquals(chunks.length, 4);
});