Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit c7bbf7e

Browse files
committed
Update testings
1 parent ac7fa09 commit c7bbf7e

File tree

2 files changed

+57
-48
lines changed

2 files changed

+57
-48
lines changed

framework/core/redirect_test.ts

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,76 @@
33
* see https://github.com/denoland/deno/blob/main/docs/runtime/location_api.md
44
* and explanation below.
55
*/
6+
67
import { assertEquals } from 'std/testing/asserts.ts'
8+
import events from './events.ts'
79
import { redirect } from './redirect.ts'
810

911
// mock history functions used in redirect()
10-
interface MockWindow extends Window {
11-
history: {
12-
replaceState: (state: object | null, title: string | '', url?: string) => void,
13-
pushState: (state: object | null, title: string | '', url?: string) => void
14-
}
15-
}
16-
declare let window: MockWindow
12+
Object.assign(window, {
13+
history: {
14+
replaceState: (url: string) => { stacks.replaceCalls++ },
15+
pushState: (url: string) => { stacks.pushCalls++ }
16+
}
17+
})
1718

1819
// track calls to history functions
19-
const calls = {
20-
pushCalls: 0, // tracks calls to pushState()
21-
replaceCalls: 0 // tracks calls to replaceState()
22-
}
23-
// create mock history impl
24-
window.history = {
25-
replaceState: (url) => { calls.replaceCalls++; return null },
26-
pushState: (url) => { calls.pushCalls++; return null }
20+
const stacks = {
21+
pushCalls: 0, // tracks calls to pushState()
22+
replaceCalls: 0 // tracks calls to replaceState()
2723
}
2824

29-
const resetCallCount = () => {
30-
calls.pushCalls = 0
31-
calls.replaceCalls = 0
25+
const resetStacks = () => {
26+
stacks.pushCalls = 0
27+
stacks.replaceCalls = 0
3228
}
3329

34-
Deno.test('redirect: replace=false should call history.pushState', () => {
35-
const url = '/foo/bar.ts'
30+
Deno.test('fw/core/redirect: replace=false should call history.pushState', () => {
31+
const url = '/foo/bar'
3632

37-
redirect(url)
38-
assertEquals(calls.pushCalls, 1)
39-
redirect(url)
40-
assertEquals(calls.pushCalls, 2)
41-
redirect(url)
42-
assertEquals(calls.pushCalls, 3)
33+
redirect(url)
34+
assertEquals(stacks.pushCalls, 1)
35+
redirect(url)
36+
assertEquals(stacks.pushCalls, 2)
37+
redirect(url)
38+
assertEquals(stacks.pushCalls, 3)
4339

44-
resetCallCount()
40+
resetStacks()
4541
})
4642

47-
Deno.test('redirect: replace=true should call history.replaceState', () => {
48-
const url = '/foo/bar2.ts'
43+
Deno.test('fw/core/redirect: replace=true should call history.replaceState', () => {
44+
const url = '/foo/bar'
4945

50-
redirect(url, true)
51-
assertEquals(calls.replaceCalls, 1)
52-
redirect(url, true)
46+
redirect(url, true)
47+
assertEquals(stacks.replaceCalls, 1)
48+
redirect(url, true)
49+
assertEquals(stacks.replaceCalls, 2)
5350

54-
assertEquals(calls.replaceCalls, 2)
5551

56-
resetCallCount()
52+
resetStacks()
5753
})
5854

59-
Deno.test('redirect: empty string url should not call history methods', () => {
60-
const url = ''
55+
Deno.test('fw/core/redirect: empty string url should not call history methods', () => {
56+
redirect('')
57+
58+
assertEquals(stacks.pushCalls, 0)
59+
assertEquals(stacks.replaceCalls, 0)
60+
})
61+
62+
Deno.test('fw/core/redirect: pre-redirect should emit "popstate" event deferredly', () => {
63+
let popstate: any = null
64+
65+
redirect('/foo/bar', true)
6166

62-
redirect(url)
67+
events.on('popstate', (e) => { popstate = e })
68+
assertEquals(popstate, null)
6369

64-
assertEquals(calls.pushCalls, 0)
65-
assertEquals(calls.replaceCalls, 0)
70+
events.emit('routerstate', { ready: true })
71+
assertEquals(popstate, { type: 'popstate', resetScroll: true })
72+
assertEquals(stacks.pushCalls, 0)
73+
assertEquals(stacks.replaceCalls, 1)
6674

75+
resetStacks()
6776
})
6877

6978
/**
@@ -72,7 +81,7 @@ Deno.test('redirect: empty string url should not call history methods', () => {
7281
* https://github.com/denoland/deno/blob/main/docs/runtime/location_api.md
7382
* This errors out on line 12 of redirect().
7483
*/
75-
// Deno.test('redirect: file url should set location.href', () => {
84+
// Deno.test('fw/core/redirect: file url should set location.href', () => {
7685
// const url = 'file://foo/file.ts'
7786

7887
// redirect(url)
@@ -81,5 +90,5 @@ Deno.test('redirect: empty string url should not call history methods', () => {
8190
// assertEquals(calls.pushCalls, 0)
8291
// assertEquals(calls.replaceCalls, 0)
8392

84-
// resetCallCount()
93+
// resetStacks()
8594
// })

server/helper_test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ Deno.test(`server/helper: toLocalUrl`, () => {
4646
)
4747
})
4848

49-
Deno.test('server/helper isLoaderPlugin false', () => {
49+
Deno.test('server/helper: isLoaderPlugin false', () => {
5050
const loader = {} as LoaderPlugin
5151
loader.type = 'foobar' as 'loader'
5252

5353
assert(!isLoaderPlugin(loader))
5454
})
5555

56-
Deno.test('server/helper isLoaderPlugin true', () => {
56+
Deno.test('server/helper: isLoaderPlugin true', () => {
5757
const loader = {} as LoaderPlugin
5858
loader.type = 'loader'
5959

6060
assert(isLoaderPlugin(loader))
6161
})
6262

63-
Deno.test('server/helper getAlephPkgUri dev', () => {
63+
Deno.test('server/helper: getAlephPkgUri dev', () => {
6464
const port = 1234
6565
Deno.env.set('ALEPH_DEV_PORT', port.toString())
6666

@@ -83,7 +83,6 @@ Deno.test('server/helper: getRelativePath', () => {
8383
assertEquals(getRelativePath('/baz/foobar', '/baz/aleph'), '../aleph')
8484
assertEquals(getRelativePath('baz/foobar', 'baz/aleph'), '../aleph')
8585
assertEquals(getRelativePath('baz/foobar', 'baz/foobar/aleph'), './aleph')
86-
8786
})
8887

8988
Deno.test('server/helper: toLocalUrl()', () => {
@@ -99,8 +98,7 @@ Deno.test('server/helper: computeHash', () => {
9998
assertEquals(computeHash(new Uint8Array([21, 31])), 'b0d04c3ac51b86296251d73c20f348e9ae0042a4')
10099
})
101100

102-
Deno.test('server/helper formatBytesWithColor', () => {
103-
101+
Deno.test('server/helper: formatBytesWithColor', () => {
104102
// 10 << 20 = 10485760 (10MB)
105103
const TenLeftShift20 = 10485760
106104
// 1 << 20 = 1048576 (1MB)
@@ -109,10 +107,12 @@ Deno.test('server/helper formatBytesWithColor', () => {
109107
const OneMb = OneLeftShift20
110108
// "\x1b[2m1018KB\x1b[22m"
111109
assertEquals(formatBytesWithColor(OneMb), "\x1b[2m1MB\x1b[22m")
110+
112111
const TwoMb = OneLeftShift20 + 1024
113112
// "\x1b[33m2MB\x1b[39m"
114113
assertEquals(formatBytesWithColor(TwoMb), "\x1b[33m2MB\x1b[39m")
114+
115115
const ElevenMb = TenLeftShift20 + 1024
116116
// "\x1b[31m11MB\x1b[39m"
117117
assertEquals(formatBytesWithColor(ElevenMb), "\x1b[31m11MB\x1b[39m")
118-
})
118+
})

0 commit comments

Comments
 (0)