-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.test.mjs
More file actions
62 lines (54 loc) · 2.19 KB
/
Copy pathbundle.test.mjs
File metadata and controls
62 lines (54 loc) · 2.19 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
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, it } from 'node:test';
import vm from 'node:vm';
const here = dirname(fileURLToPath(import.meta.url));
const bundlePath = join(here, '..', 'dist', 'scroll-anim-fallback.umd.js');
/**
* Evaluate the UMD bundle the way a browser `<script>` tag would, in a sandbox
* with no module system, and return the global it attaches itself to.
*
* @returns {Promise<any>} The `ScrollAnimFallback` global.
*/
async function loadAsScript () {
const code = await readFile(bundlePath, 'utf8');
const sandbox = {};
vm.createContext(sandbox);
vm.runInContext(code, sandbox);
return sandbox.ScrollAnimFallback;
}
describe('UMD bundle', () => {
it('attaches a global when no module system is present', async () => {
const lib = await loadAsScript();
assert.ok(lib, 'expected a ScrollAnimFallback global');
});
it('exposes the public API surface', async () => {
const lib = await loadAsScript();
for (const name of ['init', 'destroy', 'refresh', 'observe', 'supportsNative']) {
assert.equal(typeof lib[name], 'function', `missing ${name}`);
}
});
it('re-exports the range math so it can be reused directly', async () => {
const lib = await loadAsScript();
// The sandbox is a separate realm, so compare by value rather than identity.
assert.equal(
JSON.stringify(lib.resolveNamedRange('cover', { subjectOffset: 1000, subjectSize: 200, scrollportSize: 800 })),
JSON.stringify({ start: 200, end: 1200 })
);
assert.equal(lib.progressAt(700, { start: 200, end: 1200 }), 0.5);
});
it('reports no native support when CSS is unavailable', async () => {
const lib = await loadAsScript();
assert.equal(lib.supportsNative(), false);
});
it('refuses to initialise without a manifest', async () => {
const lib = await loadAsScript();
assert.throws(() => lib.init({}), /manifest/);
});
it('contains no leftover ES module syntax', async () => {
const code = await readFile(bundlePath, 'utf8');
assert.doesNotMatch(code, /^\s*(import|export)\s/m);
});
});