-
Notifications
You must be signed in to change notification settings - Fork 169
/
module_test.ts
52 lines (37 loc) · 1.45 KB
/
module_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
import { assertEquals, assertThrows } from 'https://deno.land/std@0.93.0/testing/asserts.ts'
import { trimModuleExt, toPagePath, importModule } from './module.ts'
// Augment the window object
interface MockWindow extends Window {
__ALEPH: { import: (src: string, refetch?: boolean) => Promise<string> }
}
declare let window: MockWindow
Deno.test('module toPagePath', () => {
assertEquals(toPagePath('/pages/bar.ts'), '/bar')
assertEquals(toPagePath('/foobar/index.ts'), '/foobar')
assertEquals(toPagePath(''), '/')
assertThrows(() => toPagePath({} as string))
})
Deno.test('module trimModuleExt', () => {
assertEquals(trimModuleExt('foobar.ts'), 'foobar')
assertEquals(trimModuleExt('baz.zip'), 'baz.zip')
assertEquals(trimModuleExt('barbaz.jsx.ts'), 'barbaz.jsx')
assertEquals(trimModuleExt('/index.ts'), '/index')
assertThrows(() => trimModuleExt({} as string))
assertEquals(trimModuleExt('😀'), '😀')
})
Deno.test('module importModule', async () => {
// mock __ALEPH.import
function foo(str: string, refetch: boolean = false) {
if (refetch) {
return Promise.resolve('refetching...')
}
return Promise.resolve(str)
}
window.__ALEPH = {
import: foo
}
const return1 = await importModule('/foo/bar', 'baz.ts')
assertEquals(return1, 'baz.ts')
const return2 = await importModule('/foo/bar', 'baz.ts', true)
assertEquals(return2, 'refetching...')
})