-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathAsyncLoad.test.ts
More file actions
83 lines (76 loc) · 2 KB
/
AsyncLoad.test.ts
File metadata and controls
83 lines (76 loc) · 2 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
import { describe, test, expect } from '@jest/globals';
import { asyncLoad, resolvePath } from '#js/util/AsyncLoad.js';
import { mathjax } from '#js/mathjax.js';
describe('asyncLoad()', () => {
test('asyncLoad()', async () => {
//
// Throws error if not set in mathjax
//
expect(
asyncLoad('x.js')
.then(() => false)
.catch(() => true)
).resolves.toBe(true);
//
// mathjax.asyncLoad returns value
//
mathjax.asyncLoad = (_name: string) => 'success';
await expect(asyncLoad('x.js')).resolves.toBe('success');
//
// mathjax.asyncLoad throws error
//
mathjax.asyncLoad = (_name: string) => {
throw 'fail';
};
await expect(asyncLoad('x.js')).rejects.toBe('fail');
//
// mathjax.asyncLoad returns promise
//
mathjax.asyncLoad = (_name: string) =>
Promise.resolve().then(() => 'success');
await expect(asyncLoad('x.js')).resolves.toBe('success');
//
// mathjax.asyncLoad returns promise that rejects
//
mathjax.asyncLoad = (_name: string) =>
Promise.reject().catch(() => {
throw 'fail';
});
await expect(asyncLoad('x.js')).rejects.toBe('fail');
});
test('resolvePath', () => {
//
// Test resolvePath woth Pacakge path resolution
//
(global as any).MathJax = {
_: {
components: {
package: {
Package: {
resolvePath: (file: string) => 'test:' + file,
},
},
},
},
};
expect(resolvePath('[x]/y.js', (file) => file)).toBe('test:[x]/y.js');
//
// Remove MathJax._ and test relative and absolute paths
//
(global as any).MathJax = {};
expect(
resolvePath(
'./x.js',
(file) => `rel:${file.substring(2)}`,
(file) => `abs:${file}`
)
).toBe('rel:x.js');
expect(
resolvePath(
'x.js',
(file) => `rel:${file.substring(2)}`,
(file) => `abs:${file}`
)
).toBe('abs:x.js');
});
});