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

Commit 44ded95

Browse files
authored
Merge pull request #268 from cdoremus/add-test-redirect
test: Add tests for redirect.ts
2 parents 1d58b17 + 066b651 commit 44ded95

File tree

3 files changed

+93
-21
lines changed

3 files changed

+93
-21
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You will need [Deno](https://deno.land/) 1.8+.
1818
4. Change code then run the examples.
1919
5. Push your branch to Github after all tests passed.
2020
6. Make a [pull request](https://github.com/alephjs/aleph.js/pulls).
21-
7. Marge to master branch by our maintainers.
21+
7. Merge to master branch by our maintainers.
2222

2323
```bash
2424
# ssr/development with HMR
@@ -31,7 +31,7 @@ ALEPH_DEV=true deno run -A --unstable --import-map=./import_map.json cli.ts star
3131
ALEPH_DEV=true deno run -A --unstable --import-map=./import_map.json cli.ts build ./examples/hello-world -L debug
3232

3333
# run all tests
34-
deno test -A --unstable --import-map=./import_map.json
34+
deno test -A --unstable --location=http://127.0.0.1 --import-map=./import_map.json
3535
```
3636

3737
## Project Structure

framework/core/redirect_test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* NOTE: This test needs the --location cli flag set
3+
* see https://github.com/denoland/deno/blob/main/docs/runtime/location_api.md
4+
* and explanation below.
5+
*/
6+
import { assertEquals } from 'std/testing/asserts.ts'
7+
import { redirect } from './redirect.ts'
8+
9+
// 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
17+
18+
// 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 }
27+
}
28+
29+
const resetCallCount = () => {
30+
calls.pushCalls = 0
31+
calls.replaceCalls = 0
32+
}
33+
34+
Deno.test('redirect: replace=false should call history.pushState', () => {
35+
const url = '/foo/bar.ts'
36+
37+
redirect(url)
38+
assertEquals(calls.pushCalls, 1)
39+
redirect(url)
40+
assertEquals(calls.pushCalls, 2)
41+
redirect(url)
42+
assertEquals(calls.pushCalls, 3)
43+
44+
resetCallCount()
45+
})
46+
47+
Deno.test('redirect: replace=true should call history.replaceState', () => {
48+
const url = '/foo/bar2.ts'
49+
50+
redirect(url, true)
51+
assertEquals(calls.replaceCalls, 1)
52+
redirect(url, true)
53+
54+
assertEquals(calls.replaceCalls, 2)
55+
56+
resetCallCount()
57+
})
58+
59+
Deno.test('redirect: empty string url should not call history methods', () => {
60+
const url = ''
61+
62+
redirect(url)
63+
64+
assertEquals(calls.pushCalls, 0)
65+
assertEquals(calls.replaceCalls, 0)
66+
67+
})
68+
69+
/**
70+
* This fails because setting location.href is not allowed in a
71+
* non-browser environment. see
72+
* https://github.com/denoland/deno/blob/main/docs/runtime/location_api.md
73+
* This errors out on line 12 of redirect().
74+
*/
75+
// Deno.test('redirect: file url should set location.href', () => {
76+
// const url = 'file://foo/file.ts'
77+
78+
// redirect(url)
79+
80+
// assertEquals(window.location.href, url)
81+
// assertEquals(calls.pushCalls, 0)
82+
// assertEquals(calls.replaceCalls, 0)
83+
84+
// resetCallCount()
85+
// })

shared/fs_test.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from './fs.ts'
99

1010

11-
Deno.test(`fs existsDirSync`, () => {
11+
Deno.test(`fs: existsDirSync`, () => {
1212
// true test cases
1313
const dir = Deno.makeTempDirSync()
1414
assert(existsDirSync(dir))
@@ -22,7 +22,7 @@ Deno.test(`fs existsDirSync`, () => {
2222
})
2323

2424

25-
Deno.test(`fs async existsDir`, async () => {
25+
Deno.test(`fs: async existsDir`, async () => {
2626
// true test cases
2727
assert(await existsDir(await Deno.realPath(getStandardFolder())))
2828
const dir = await Deno.makeTempDir()
@@ -31,13 +31,9 @@ Deno.test(`fs async existsDir`, async () => {
3131
assertEquals(await existsDir(`${dir}${SEP}foobar`), false)
3232
const file = await Deno.makeTempFile()
3333
assertEquals(await existsDir(file), false)
34-
// error test cases
35-
existsDir({} as string).then(err => {
36-
assert(err instanceof Error)
37-
}).catch(e => console.error(e))
3834
})
3935

40-
Deno.test(`fs existsFileSync`, () => {
36+
Deno.test(`fs: existsFileSync`, () => {
4137
// true test cases
4238
const file = Deno.makeTempFileSync()
4339
assert(existsFileSync(file))
@@ -49,21 +45,17 @@ Deno.test(`fs existsFileSync`, () => {
4945
assertThrows(() => existsDirSync({} as string), Error)
5046
})
5147

52-
Deno.test(`fs async existsFile`, async () => {
48+
Deno.test(`fs: async existsFile`, async () => {
5349
// true test cases
5450
const file = await Deno.makeTempFile()
5551
assert(await existsFile(file))
5652
// false test cases
5753
const dir = Deno.makeTempDirSync()
5854
assertEquals(await existsFile(dir), false)
5955
assertEquals(await existsFileSync(`${dir}${SEP}llksdafzxc.ts`), false)
60-
// error test cases
61-
existsFile({} as string).then(err => {
62-
assert(err instanceof Error)
63-
}).catch(e => console.error(e))
6456
})
6557

66-
Deno.test('ensureTextFile', async () => {
58+
Deno.test('fs: ensureTextFile', async () => {
6759
// true test case
6860
const dirPath = await Deno.makeTempDir()
6961
const textFilePath = `${dirPath}${SEP}test.txt`
@@ -84,7 +76,7 @@ Deno.test('ensureTextFile', async () => {
8476
}
8577
})
8678

87-
Deno.test('lazyRemove', async () => {
79+
Deno.test('fs: lazyRemove', async () => {
8880
// true test case
8981
const filePath = await Deno.makeTempFile()
9082
await lazyRemove(filePath)
@@ -93,11 +85,6 @@ Deno.test('lazyRemove', async () => {
9385
const dirPath = await Deno.makeTempDir()
9486
await lazyRemove(`${dirPath}${SEP}asdfsdf.txt`)
9587
assert(await existsDir(dirPath))
96-
// error test
97-
lazyRemove({} as string).then(err => {
98-
assert(err instanceof Error)
99-
}).catch(e => console.error(e))
100-
10188
})
10289

10390

0 commit comments

Comments
 (0)