@esmx/router 3.0.0-rc.29 → 3.0.0-rc.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.zh-CN.md +82 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.mjs +0 -1
- package/package.json +3 -3
- package/src/index.ts +0 -3
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.mjs +0 -8
- package/dist/location.test.d.ts +0 -8
- package/dist/location.test.mjs +0 -370
- package/dist/matcher.test.d.ts +0 -1
- package/dist/matcher.test.mjs +0 -1492
- package/dist/micro-app.dom.test.d.ts +0 -1
- package/dist/micro-app.dom.test.mjs +0 -532
- package/dist/navigation.test.d.ts +0 -1
- package/dist/navigation.test.mjs +0 -681
- package/dist/route-task.test.d.ts +0 -1
- package/dist/route-task.test.mjs +0 -673
- package/dist/route-transition.test.d.ts +0 -1
- package/dist/route-transition.test.mjs +0 -146
- package/dist/route.test.d.ts +0 -1
- package/dist/route.test.mjs +0 -1664
- package/dist/router-back.test.d.ts +0 -1
- package/dist/router-back.test.mjs +0 -361
- package/dist/router-forward.test.d.ts +0 -1
- package/dist/router-forward.test.mjs +0 -376
- package/dist/router-go.test.d.ts +0 -1
- package/dist/router-go.test.mjs +0 -73
- package/dist/router-guards-cleanup.test.d.ts +0 -1
- package/dist/router-guards-cleanup.test.mjs +0 -437
- package/dist/router-push.test.d.ts +0 -1
- package/dist/router-push.test.mjs +0 -115
- package/dist/router-replace.test.d.ts +0 -1
- package/dist/router-replace.test.mjs +0 -114
- package/dist/router-resolve.test.d.ts +0 -1
- package/dist/router-resolve.test.mjs +0 -393
- package/dist/router-restart-app.dom.test.d.ts +0 -1
- package/dist/router-restart-app.dom.test.mjs +0 -616
- package/dist/router-window-navigation.test.d.ts +0 -1
- package/dist/router-window-navigation.test.mjs +0 -359
- package/dist/util.test.d.ts +0 -1
- package/dist/util.test.mjs +0 -1020
- package/src/index.test.ts +0 -9
- package/src/location.test.ts +0 -406
- package/src/matcher.test.ts +0 -1685
- package/src/micro-app.dom.test.ts +0 -708
- package/src/navigation.test.ts +0 -858
- package/src/route-task.test.ts +0 -901
- package/src/route-transition.test.ts +0 -178
- package/src/route.test.ts +0 -2014
- package/src/router-back.test.ts +0 -487
- package/src/router-forward.test.ts +0 -506
- package/src/router-go.test.ts +0 -91
- package/src/router-guards-cleanup.test.ts +0 -595
- package/src/router-push.test.ts +0 -140
- package/src/router-replace.test.ts +0 -139
- package/src/router-resolve.test.ts +0 -475
- package/src/router-restart-app.dom.test.ts +0 -783
- package/src/router-window-navigation.test.ts +0 -457
- package/src/util.test.ts +0 -1262
|
@@ -1,708 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vitest-environment happy-dom
|
|
3
|
-
*/
|
|
4
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
-
import { MicroApp, resolveRootElement } from './micro-app';
|
|
6
|
-
import { parsedOptions } from './options';
|
|
7
|
-
import { Route } from './route';
|
|
8
|
-
import type { Router } from './router';
|
|
9
|
-
import type {
|
|
10
|
-
RouteParsedConfig,
|
|
11
|
-
RouterMicroAppCallback,
|
|
12
|
-
RouterMicroAppOptions,
|
|
13
|
-
RouterOptions,
|
|
14
|
-
RouterParsedOptions
|
|
15
|
-
} from './types';
|
|
16
|
-
import { RouteType, RouterMode } from './types';
|
|
17
|
-
|
|
18
|
-
const createMockParsedConfig = (
|
|
19
|
-
app?: string | RouterMicroAppCallback
|
|
20
|
-
): RouteParsedConfig => ({
|
|
21
|
-
path: '/test',
|
|
22
|
-
compilePath: '/test',
|
|
23
|
-
children: [],
|
|
24
|
-
match: vi.fn(),
|
|
25
|
-
compile: vi.fn(),
|
|
26
|
-
meta: {},
|
|
27
|
-
app
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
const createMockRouter = (
|
|
31
|
-
overrides: {
|
|
32
|
-
root?: string | HTMLElement;
|
|
33
|
-
matched?: Array<{ app?: string | RouterMicroAppCallback }>;
|
|
34
|
-
options?: any;
|
|
35
|
-
parsedOptions?: Partial<RouterParsedOptions>;
|
|
36
|
-
} = {}
|
|
37
|
-
): Router => {
|
|
38
|
-
const baseOptions: RouterOptions = {
|
|
39
|
-
root: overrides.root || '#test-router',
|
|
40
|
-
context: {},
|
|
41
|
-
routes: [],
|
|
42
|
-
mode: RouterMode.memory,
|
|
43
|
-
base: new URL('https://example.com/'),
|
|
44
|
-
req: null,
|
|
45
|
-
res: null,
|
|
46
|
-
apps: overrides.options?.apps || {},
|
|
47
|
-
normalizeURL: (url: URL) => url,
|
|
48
|
-
fallback: () => {},
|
|
49
|
-
rootStyle: false,
|
|
50
|
-
handleBackBoundary: () => {},
|
|
51
|
-
handleLayerClose: () => {}
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const mockParsedOptions = {
|
|
55
|
-
...parsedOptions(baseOptions),
|
|
56
|
-
...overrides.parsedOptions
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
if (overrides.matched) {
|
|
60
|
-
const customMatched = overrides.matched.map((item) =>
|
|
61
|
-
createMockParsedConfig(item.app || 'test-app')
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
mockParsedOptions.matcher = () => ({
|
|
65
|
-
matches: customMatched,
|
|
66
|
-
params: {}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const mockRoute = new Route({
|
|
71
|
-
options: mockParsedOptions,
|
|
72
|
-
toType: RouteType.push,
|
|
73
|
-
toInput: '/test'
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
root: overrides.root || '#test-router',
|
|
78
|
-
route: mockRoute,
|
|
79
|
-
options: overrides.options || {},
|
|
80
|
-
parsedOptions: mockParsedOptions
|
|
81
|
-
} as Router;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const createMockApp = (): RouterMicroAppOptions => ({
|
|
85
|
-
mount: vi.fn(),
|
|
86
|
-
unmount: vi.fn(),
|
|
87
|
-
renderToString: vi.fn().mockResolvedValue('<div>rendered</div>')
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
describe('resolveRootElement', () => {
|
|
91
|
-
afterEach(() => {
|
|
92
|
-
vi.clearAllMocks();
|
|
93
|
-
// Clean up the DOM
|
|
94
|
-
document.body.innerHTML = '';
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
describe('Basic functionality tests', () => {
|
|
98
|
-
it('should return a div element when the parameter is empty', () => {
|
|
99
|
-
const result1 = resolveRootElement();
|
|
100
|
-
expect(result1).toBeInstanceOf(HTMLElement);
|
|
101
|
-
expect(result1.tagName).toBe('DIV');
|
|
102
|
-
|
|
103
|
-
const result2 = resolveRootElement(undefined);
|
|
104
|
-
expect(result2).toBeInstanceOf(HTMLElement);
|
|
105
|
-
expect(result2.tagName).toBe('DIV');
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('should correctly handle a directly passed HTMLElement', () => {
|
|
109
|
-
const element = document.createElement('div');
|
|
110
|
-
element.id = 'test-element';
|
|
111
|
-
|
|
112
|
-
const result = resolveRootElement(element);
|
|
113
|
-
|
|
114
|
-
expect(result).toBe(element);
|
|
115
|
-
expect(result!.id).toBe('test-element');
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('should correctly handle a string selector', () => {
|
|
119
|
-
const existingElement = document.createElement('div');
|
|
120
|
-
existingElement.id = 'existing-element';
|
|
121
|
-
document.body.appendChild(existingElement);
|
|
122
|
-
|
|
123
|
-
const result = resolveRootElement('#existing-element');
|
|
124
|
-
|
|
125
|
-
expect(result).toBe(existingElement);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should create a new element when not found', () => {
|
|
129
|
-
const result = resolveRootElement('#non-existent');
|
|
130
|
-
|
|
131
|
-
expect(result).toBeInstanceOf(HTMLElement);
|
|
132
|
-
expect(result!.tagName).toBe('DIV');
|
|
133
|
-
expect(result!.id).toBe('');
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
describe('Selector type tests', () => {
|
|
138
|
-
it('should handle ID selectors', () => {
|
|
139
|
-
// Test finding an existing element
|
|
140
|
-
const existingElement = document.createElement('div');
|
|
141
|
-
existingElement.id = 'app';
|
|
142
|
-
document.body.appendChild(existingElement);
|
|
143
|
-
const result = resolveRootElement('#app');
|
|
144
|
-
expect(result).toBeInstanceOf(HTMLElement);
|
|
145
|
-
expect(result!.id).toBe('app');
|
|
146
|
-
|
|
147
|
-
// Test creating a new element when not found
|
|
148
|
-
const newResult = resolveRootElement('#new-app');
|
|
149
|
-
expect(newResult).toBeInstanceOf(HTMLElement);
|
|
150
|
-
expect(newResult.tagName).toBe('DIV');
|
|
151
|
-
expect(newResult.id).toBe('');
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('should handle class selectors', () => {
|
|
155
|
-
const element = document.createElement('div');
|
|
156
|
-
element.className = 'app-container';
|
|
157
|
-
document.body.appendChild(element);
|
|
158
|
-
|
|
159
|
-
const result = resolveRootElement('.app-container');
|
|
160
|
-
|
|
161
|
-
expect(result).toBe(element);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it('should handle attribute selectors', () => {
|
|
165
|
-
const element = document.createElement('div');
|
|
166
|
-
element.setAttribute('data-app', 'main');
|
|
167
|
-
document.body.appendChild(element);
|
|
168
|
-
|
|
169
|
-
const result = resolveRootElement('[data-app="main"]');
|
|
170
|
-
|
|
171
|
-
expect(result).toBe(element);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it('should handle tag selectors', () => {
|
|
175
|
-
const element = document.createElement('main');
|
|
176
|
-
document.body.appendChild(element);
|
|
177
|
-
|
|
178
|
-
const result = resolveRootElement('main');
|
|
179
|
-
|
|
180
|
-
expect(result).toBe(element);
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
describe('Edge case tests', () => {
|
|
185
|
-
it('should handle complex selectors', () => {
|
|
186
|
-
const container = document.createElement('div');
|
|
187
|
-
container.className = 'container';
|
|
188
|
-
const app = document.createElement('div');
|
|
189
|
-
app.id = 'app';
|
|
190
|
-
container.appendChild(app);
|
|
191
|
-
document.body.appendChild(container);
|
|
192
|
-
|
|
193
|
-
const result = resolveRootElement('.container #app');
|
|
194
|
-
|
|
195
|
-
expect(result).toBe(app);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it('should return the first matching element', () => {
|
|
199
|
-
const element1 = document.createElement('div');
|
|
200
|
-
element1.className = 'multiple';
|
|
201
|
-
element1.textContent = 'first';
|
|
202
|
-
const element2 = document.createElement('div');
|
|
203
|
-
element2.className = 'multiple';
|
|
204
|
-
element2.textContent = 'second';
|
|
205
|
-
|
|
206
|
-
document.body.appendChild(element1);
|
|
207
|
-
document.body.appendChild(element2);
|
|
208
|
-
|
|
209
|
-
const result = resolveRootElement('.multiple');
|
|
210
|
-
|
|
211
|
-
expect(result).toBe(element1);
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it('should handle non-string, non-HTMLElement inputs', () => {
|
|
215
|
-
// @ts-expect-error - testing invalid input
|
|
216
|
-
const result1 = resolveRootElement(123);
|
|
217
|
-
expect(result1).toBeInstanceOf(HTMLElement);
|
|
218
|
-
expect(result1.tagName).toBe('DIV');
|
|
219
|
-
|
|
220
|
-
// @ts-expect-error - testing invalid input
|
|
221
|
-
const result2 = resolveRootElement({});
|
|
222
|
-
expect(result2).toBeInstanceOf(HTMLElement);
|
|
223
|
-
expect(result2.tagName).toBe('DIV');
|
|
224
|
-
|
|
225
|
-
// @ts-expect-error - testing invalid input
|
|
226
|
-
const result3 = resolveRootElement([]);
|
|
227
|
-
expect(result3).toBeInstanceOf(HTMLElement);
|
|
228
|
-
expect(result3.tagName).toBe('DIV');
|
|
229
|
-
});
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
describe('Type safety tests', () => {
|
|
233
|
-
it('should return any type of element found', () => {
|
|
234
|
-
const svg = document.createElementNS(
|
|
235
|
-
'http://www.w3.org/2000/svg',
|
|
236
|
-
'svg'
|
|
237
|
-
);
|
|
238
|
-
svg.id = 'svg-element';
|
|
239
|
-
document.body.appendChild(svg);
|
|
240
|
-
|
|
241
|
-
const result = resolveRootElement('#svg-element');
|
|
242
|
-
|
|
243
|
-
expect(result).toBe(svg);
|
|
244
|
-
expect(result).toBeInstanceOf(SVGElement);
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
it('should handle non-element nodes like text nodes', () => {
|
|
248
|
-
// querySelector will not return text nodes, but this is for type safety verification.
|
|
249
|
-
const result = resolveRootElement('#non-existent-text');
|
|
250
|
-
|
|
251
|
-
expect(result).toBeInstanceOf(HTMLElement);
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
describe('MicroApp', () => {
|
|
257
|
-
let microApp: MicroApp;
|
|
258
|
-
|
|
259
|
-
beforeEach(() => {
|
|
260
|
-
microApp = new MicroApp();
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
afterEach(() => {
|
|
264
|
-
vi.clearAllMocks();
|
|
265
|
-
// Clean up the DOM
|
|
266
|
-
document.body.innerHTML = '';
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
describe('Initial state', () => {
|
|
270
|
-
it('should initialize with a null state', () => {
|
|
271
|
-
expect(microApp.app).toBeNull();
|
|
272
|
-
expect(microApp.root).toBeNull();
|
|
273
|
-
expect((microApp as any)._factory).toBeNull();
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
describe('_getNextFactory', () => {
|
|
278
|
-
it('should get the app name from route match and return the corresponding factory', () => {
|
|
279
|
-
const mockFactory = vi.fn();
|
|
280
|
-
const router = createMockRouter({
|
|
281
|
-
matched: [{ app: 'vue-app' }],
|
|
282
|
-
options: { apps: { 'vue-app': mockFactory } }
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
const factory = (microApp as any)._getNextFactory(router);
|
|
286
|
-
expect(factory).toBe(mockFactory);
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
it('should return null if the app name does not exist', () => {
|
|
290
|
-
const router = createMockRouter({
|
|
291
|
-
matched: [{ app: 'non-existent-app' }],
|
|
292
|
-
options: { apps: { 'vue-app': vi.fn() } }
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
const factory = (microApp as any)._getNextFactory(router);
|
|
296
|
-
expect(factory).toBeNull();
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
it('should handle function-type apps in the match result', () => {
|
|
300
|
-
const mockFactory = vi.fn();
|
|
301
|
-
const router = createMockRouter({
|
|
302
|
-
matched: [{ app: mockFactory }]
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
const factory = (microApp as any)._getNextFactory(router);
|
|
306
|
-
expect(factory).toBe(mockFactory);
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
it('should handle options.apps being a function', () => {
|
|
310
|
-
const mockFactory = vi.fn();
|
|
311
|
-
const router = createMockRouter({
|
|
312
|
-
matched: [{ app: 'any-app' }],
|
|
313
|
-
options: { apps: mockFactory }
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
const factory = (microApp as any)._getNextFactory(router);
|
|
317
|
-
expect(factory).toBe(mockFactory);
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
it('should return null when there are no match results', () => {
|
|
321
|
-
const router = createMockRouter({
|
|
322
|
-
matched: []
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
const factory = (microApp as any)._getNextFactory(router);
|
|
326
|
-
expect(factory).toBeNull();
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
it('should return null when options.apps is an empty object', () => {
|
|
330
|
-
const router = createMockRouter({
|
|
331
|
-
matched: [{ app: 'test-app' }],
|
|
332
|
-
options: { apps: {} }
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
const factory = (microApp as any)._getNextFactory(router);
|
|
336
|
-
expect(factory).toBeNull();
|
|
337
|
-
});
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
describe('_update method', () => {
|
|
341
|
-
it('should update the factory and create the application', () => {
|
|
342
|
-
const mockFactory = vi.fn().mockReturnValue(createMockApp());
|
|
343
|
-
const router = createMockRouter({
|
|
344
|
-
matched: [{ app: 'test-app' }],
|
|
345
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
microApp._update(router);
|
|
349
|
-
|
|
350
|
-
expect((microApp as any)._factory).toBe(mockFactory);
|
|
351
|
-
expect(mockFactory).toHaveBeenCalledWith(router);
|
|
352
|
-
expect(microApp.app).not.toBeNull();
|
|
353
|
-
expect(microApp.root).not.toBeNull();
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
it('should skip update if factory has not changed and force=false', () => {
|
|
357
|
-
const mockFactory = vi.fn().mockReturnValue(createMockApp());
|
|
358
|
-
const router = createMockRouter({
|
|
359
|
-
matched: [{ app: 'test-app' }],
|
|
360
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
microApp._update(router);
|
|
364
|
-
expect(mockFactory).toHaveBeenCalledTimes(1);
|
|
365
|
-
|
|
366
|
-
microApp._update(router);
|
|
367
|
-
expect(mockFactory).toHaveBeenCalledTimes(1);
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
it('should force update when force=true', () => {
|
|
371
|
-
const mockFactory = vi.fn().mockReturnValue(createMockApp());
|
|
372
|
-
const router = createMockRouter({
|
|
373
|
-
matched: [{ app: 'test-app' }],
|
|
374
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
microApp._update(router);
|
|
378
|
-
expect(mockFactory).toHaveBeenCalledTimes(1);
|
|
379
|
-
|
|
380
|
-
// Force update
|
|
381
|
-
microApp._update(router, true);
|
|
382
|
-
expect(mockFactory).toHaveBeenCalledTimes(2);
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
it('should set the application to null if there is no factory', () => {
|
|
386
|
-
const router = createMockRouter({
|
|
387
|
-
matched: [{ app: 'non-existent' }],
|
|
388
|
-
options: { apps: {} }
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
microApp._update(router);
|
|
392
|
-
|
|
393
|
-
expect(microApp.app).toBeNull();
|
|
394
|
-
expect((microApp as any)._factory).toBeNull();
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
it('should create a new root element and mount the application', () => {
|
|
398
|
-
const mockApp = createMockApp();
|
|
399
|
-
const mockFactory = vi.fn().mockReturnValue(mockApp);
|
|
400
|
-
|
|
401
|
-
const router = createMockRouter({
|
|
402
|
-
root: '#test-router',
|
|
403
|
-
matched: [{ app: 'test-app' }],
|
|
404
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
microApp._update(router);
|
|
408
|
-
|
|
409
|
-
expect(microApp.root).toBeInstanceOf(HTMLElement);
|
|
410
|
-
expect(microApp.root!.tagName).toBe('DIV');
|
|
411
|
-
expect(mockApp.mount).toHaveBeenCalledWith(microApp.root);
|
|
412
|
-
expect(document.body.contains(microApp.root!)).toBe(true);
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
it('should use an existing root element', () => {
|
|
416
|
-
const existingElement = document.createElement('div');
|
|
417
|
-
existingElement.id = 'test-router';
|
|
418
|
-
document.body.appendChild(existingElement);
|
|
419
|
-
|
|
420
|
-
const mockApp = createMockApp();
|
|
421
|
-
const mockFactory = vi.fn().mockReturnValue(mockApp);
|
|
422
|
-
|
|
423
|
-
const router = createMockRouter({
|
|
424
|
-
root: '#test-router',
|
|
425
|
-
matched: [{ app: 'test-app' }],
|
|
426
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
427
|
-
});
|
|
428
|
-
|
|
429
|
-
microApp._update(router);
|
|
430
|
-
|
|
431
|
-
expect(microApp.root).toBe(existingElement);
|
|
432
|
-
expect(mockApp.mount).toHaveBeenCalledWith(existingElement);
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
it('should use the already set root element', () => {
|
|
436
|
-
const existingRoot = document.createElement('div');
|
|
437
|
-
existingRoot.id = 'existing-root';
|
|
438
|
-
document.body.appendChild(existingRoot);
|
|
439
|
-
|
|
440
|
-
microApp.root = existingRoot;
|
|
441
|
-
|
|
442
|
-
const mockApp = createMockApp();
|
|
443
|
-
const mockFactory = vi.fn().mockReturnValue(mockApp);
|
|
444
|
-
|
|
445
|
-
const router = createMockRouter({
|
|
446
|
-
matched: [{ app: 'test-app' }],
|
|
447
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
microApp._update(router);
|
|
451
|
-
|
|
452
|
-
expect(microApp.root).toBe(existingRoot);
|
|
453
|
-
expect(mockApp.mount).toHaveBeenCalledWith(existingRoot);
|
|
454
|
-
});
|
|
455
|
-
|
|
456
|
-
it('should apply rootStyle styles', () => {
|
|
457
|
-
const mockApp = createMockApp();
|
|
458
|
-
const mockFactory = vi.fn().mockReturnValue(mockApp);
|
|
459
|
-
|
|
460
|
-
const router = createMockRouter({
|
|
461
|
-
matched: [{ app: 'test-app' }],
|
|
462
|
-
options: { apps: { 'test-app': mockFactory } },
|
|
463
|
-
parsedOptions: {
|
|
464
|
-
rootStyle: { color: 'red', fontSize: '16px' }
|
|
465
|
-
}
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
microApp._update(router);
|
|
469
|
-
|
|
470
|
-
expect(microApp.root!.style.color).toBe('red');
|
|
471
|
-
expect(microApp.root!.style.fontSize).toBe('16px');
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
it('should unmount the old application if it exists', () => {
|
|
475
|
-
const oldApp = createMockApp();
|
|
476
|
-
const newApp = createMockApp();
|
|
477
|
-
const oldFactory = vi.fn().mockReturnValue(oldApp);
|
|
478
|
-
const newFactory = vi.fn().mockReturnValue(newApp);
|
|
479
|
-
|
|
480
|
-
const router1 = createMockRouter({
|
|
481
|
-
matched: [{ app: 'old-app' }],
|
|
482
|
-
options: { apps: { 'old-app': oldFactory } }
|
|
483
|
-
});
|
|
484
|
-
microApp._update(router1);
|
|
485
|
-
|
|
486
|
-
expect(microApp.app).toBe(oldApp);
|
|
487
|
-
expect(oldApp.unmount).not.toHaveBeenCalled();
|
|
488
|
-
|
|
489
|
-
const router2 = createMockRouter({
|
|
490
|
-
matched: [{ app: 'new-app' }],
|
|
491
|
-
options: { apps: { 'new-app': newFactory } }
|
|
492
|
-
});
|
|
493
|
-
microApp._update(router2);
|
|
494
|
-
|
|
495
|
-
expect(oldApp.unmount).toHaveBeenCalled();
|
|
496
|
-
expect(microApp.app).toBe(newApp);
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
it('should append the root element to the body if not in DOM', () => {
|
|
500
|
-
const mockApp = createMockApp();
|
|
501
|
-
const mockFactory = vi.fn().mockReturnValue(mockApp);
|
|
502
|
-
|
|
503
|
-
const router = createMockRouter({
|
|
504
|
-
matched: [{ app: 'test-app' }],
|
|
505
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
microApp._update(router);
|
|
509
|
-
|
|
510
|
-
expect(document.body.contains(microApp.root!)).toBe(true);
|
|
511
|
-
});
|
|
512
|
-
|
|
513
|
-
it('should not re-append an element already in the DOM', () => {
|
|
514
|
-
const existingElement = document.createElement('div');
|
|
515
|
-
existingElement.id = 'test-router';
|
|
516
|
-
document.body.appendChild(existingElement);
|
|
517
|
-
|
|
518
|
-
const mockApp = createMockApp();
|
|
519
|
-
const mockFactory = vi.fn().mockReturnValue(mockApp);
|
|
520
|
-
|
|
521
|
-
const router = createMockRouter({
|
|
522
|
-
root: '#test-router',
|
|
523
|
-
matched: [{ app: 'test-app' }],
|
|
524
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
525
|
-
});
|
|
526
|
-
|
|
527
|
-
// Record the number of children in the body
|
|
528
|
-
const initialChildCount = document.body.children.length;
|
|
529
|
-
|
|
530
|
-
microApp._update(router);
|
|
531
|
-
|
|
532
|
-
expect(document.body.children.length).toBe(initialChildCount);
|
|
533
|
-
expect(microApp.root).toBe(existingElement);
|
|
534
|
-
});
|
|
535
|
-
|
|
536
|
-
it('should handle the case where the factory function is null', () => {
|
|
537
|
-
const router = createMockRouter({
|
|
538
|
-
matched: [],
|
|
539
|
-
options: { apps: {} }
|
|
540
|
-
});
|
|
541
|
-
|
|
542
|
-
microApp._update(router, true);
|
|
543
|
-
|
|
544
|
-
expect(microApp.app).toBeNull();
|
|
545
|
-
expect((microApp as any)._factory).toBeNull();
|
|
546
|
-
});
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
describe('destroy method', () => {
|
|
550
|
-
it('should destroy the application and clean up the state', () => {
|
|
551
|
-
const mockApp = createMockApp();
|
|
552
|
-
const mockRoot = document.createElement('div');
|
|
553
|
-
document.body.appendChild(mockRoot);
|
|
554
|
-
|
|
555
|
-
microApp.app = mockApp;
|
|
556
|
-
microApp.root = mockRoot;
|
|
557
|
-
(microApp as any)._factory = vi.fn();
|
|
558
|
-
|
|
559
|
-
microApp.destroy();
|
|
560
|
-
|
|
561
|
-
expect(mockApp.unmount).toHaveBeenCalled();
|
|
562
|
-
expect(document.body.contains(mockRoot)).toBe(false);
|
|
563
|
-
expect(microApp.app).toBeNull();
|
|
564
|
-
expect(microApp.root).toBeNull();
|
|
565
|
-
expect((microApp as any)._factory).toBeNull();
|
|
566
|
-
});
|
|
567
|
-
|
|
568
|
-
it('should safely handle empty state', () => {
|
|
569
|
-
expect(() => microApp.destroy()).not.toThrow();
|
|
570
|
-
expect(microApp.app).toBeNull();
|
|
571
|
-
expect(microApp.root).toBeNull();
|
|
572
|
-
expect((microApp as any)._factory).toBeNull();
|
|
573
|
-
});
|
|
574
|
-
|
|
575
|
-
it('should handle partial state', () => {
|
|
576
|
-
const mockApp = createMockApp();
|
|
577
|
-
microApp.app = mockApp;
|
|
578
|
-
// root and _factory remain null
|
|
579
|
-
|
|
580
|
-
expect(() => microApp.destroy()).not.toThrow();
|
|
581
|
-
expect(mockApp.unmount).toHaveBeenCalled();
|
|
582
|
-
expect(microApp.app).toBeNull();
|
|
583
|
-
});
|
|
584
|
-
});
|
|
585
|
-
|
|
586
|
-
describe('integration tests', () => {
|
|
587
|
-
it('should fully handle the application lifecycle', () => {
|
|
588
|
-
const mockApp1 = createMockApp();
|
|
589
|
-
const mockApp2 = createMockApp();
|
|
590
|
-
const factory1 = vi.fn().mockReturnValue(mockApp1);
|
|
591
|
-
const factory2 = vi.fn().mockReturnValue(mockApp2);
|
|
592
|
-
|
|
593
|
-
// Mount the first application
|
|
594
|
-
const router1 = createMockRouter({
|
|
595
|
-
root: '#app1',
|
|
596
|
-
matched: [{ app: 'app1' }],
|
|
597
|
-
options: { apps: { app1: factory1 } }
|
|
598
|
-
});
|
|
599
|
-
|
|
600
|
-
microApp._update(router1);
|
|
601
|
-
|
|
602
|
-
expect(factory1).toHaveBeenCalledWith(router1);
|
|
603
|
-
expect(mockApp1.mount).toHaveBeenCalledWith(microApp.root);
|
|
604
|
-
expect(microApp.app).toBe(mockApp1);
|
|
605
|
-
|
|
606
|
-
// Switch to the second application
|
|
607
|
-
const router2 = createMockRouter({
|
|
608
|
-
root: '#app2',
|
|
609
|
-
matched: [{ app: 'app2' }],
|
|
610
|
-
options: { apps: { app2: factory2 } }
|
|
611
|
-
});
|
|
612
|
-
|
|
613
|
-
microApp._update(router2);
|
|
614
|
-
|
|
615
|
-
expect(mockApp1.unmount).toHaveBeenCalled();
|
|
616
|
-
expect(factory2).toHaveBeenCalledWith(router2);
|
|
617
|
-
expect(mockApp2.mount).toHaveBeenCalledWith(microApp.root);
|
|
618
|
-
expect(microApp.app).toBe(mockApp2);
|
|
619
|
-
|
|
620
|
-
// Destroy
|
|
621
|
-
microApp.destroy();
|
|
622
|
-
|
|
623
|
-
expect(mockApp2.unmount).toHaveBeenCalled();
|
|
624
|
-
expect(document.body.contains(microApp.root!)).toBe(false);
|
|
625
|
-
expect(microApp.app).toBeNull();
|
|
626
|
-
expect(microApp.root).toBeNull();
|
|
627
|
-
});
|
|
628
|
-
|
|
629
|
-
it('should handle complex route application configuration', () => {
|
|
630
|
-
const mockApp = createMockApp();
|
|
631
|
-
const dynamicFactory: RouterMicroAppCallback = vi
|
|
632
|
-
.fn()
|
|
633
|
-
.mockReturnValue(mockApp);
|
|
634
|
-
|
|
635
|
-
// Test dynamic application factory
|
|
636
|
-
const router = createMockRouter({
|
|
637
|
-
matched: [{ app: dynamicFactory }],
|
|
638
|
-
options: { apps: {} }
|
|
639
|
-
});
|
|
640
|
-
|
|
641
|
-
microApp._update(router);
|
|
642
|
-
|
|
643
|
-
expect(dynamicFactory).toHaveBeenCalledWith(router);
|
|
644
|
-
expect(mockApp.mount).toHaveBeenCalledWith(microApp.root);
|
|
645
|
-
expect(microApp.app).toBe(mockApp);
|
|
646
|
-
});
|
|
647
|
-
|
|
648
|
-
it('should correctly handle rootStyle being false', () => {
|
|
649
|
-
const mockApp = createMockApp();
|
|
650
|
-
const mockFactory = vi.fn().mockReturnValue(mockApp);
|
|
651
|
-
|
|
652
|
-
const router = createMockRouter({
|
|
653
|
-
matched: [{ app: 'test-app' }],
|
|
654
|
-
options: { apps: { 'test-app': mockFactory } },
|
|
655
|
-
parsedOptions: { rootStyle: false }
|
|
656
|
-
});
|
|
657
|
-
|
|
658
|
-
microApp._update(router);
|
|
659
|
-
|
|
660
|
-
expect(microApp.root!.style.cssText).toBe('');
|
|
661
|
-
});
|
|
662
|
-
});
|
|
663
|
-
|
|
664
|
-
describe('boundary case tests', () => {
|
|
665
|
-
it('should handle the case where route.matched is an empty array', () => {
|
|
666
|
-
const router = createMockRouter({
|
|
667
|
-
matched: []
|
|
668
|
-
});
|
|
669
|
-
|
|
670
|
-
expect(() => microApp._update(router)).not.toThrow();
|
|
671
|
-
expect(microApp.app).toBeNull();
|
|
672
|
-
});
|
|
673
|
-
|
|
674
|
-
it('should handle the case where route.matched[0] does not have an app attribute', () => {
|
|
675
|
-
const router = createMockRouter({
|
|
676
|
-
matched: [{}]
|
|
677
|
-
});
|
|
678
|
-
|
|
679
|
-
expect(() => microApp._update(router)).not.toThrow();
|
|
680
|
-
expect(microApp.app).toBeNull();
|
|
681
|
-
});
|
|
682
|
-
|
|
683
|
-
it('should handle the case where the factory function returns null', () => {
|
|
684
|
-
const mockFactory = vi.fn().mockReturnValue(null);
|
|
685
|
-
const router = createMockRouter({
|
|
686
|
-
matched: [{ app: 'test-app' }],
|
|
687
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
688
|
-
});
|
|
689
|
-
|
|
690
|
-
microApp._update(router);
|
|
691
|
-
|
|
692
|
-
expect(mockFactory).toHaveBeenCalledWith(router);
|
|
693
|
-
expect(microApp.app).toBeNull();
|
|
694
|
-
});
|
|
695
|
-
|
|
696
|
-
it('should handle the case where the factory function throws an exception', () => {
|
|
697
|
-
const mockFactory = vi.fn().mockImplementation(() => {
|
|
698
|
-
throw new Error('Factory error');
|
|
699
|
-
});
|
|
700
|
-
const router = createMockRouter({
|
|
701
|
-
matched: [{ app: 'test-app' }],
|
|
702
|
-
options: { apps: { 'test-app': mockFactory } }
|
|
703
|
-
});
|
|
704
|
-
|
|
705
|
-
expect(() => microApp._update(router)).toThrow('Factory error');
|
|
706
|
-
});
|
|
707
|
-
});
|
|
708
|
-
});
|