Skip to content

Commit 017f12d

Browse files
committed
Optimize test coverage
1 parent 2c8fa80 commit 017f12d

File tree

7 files changed

+90
-37
lines changed

7 files changed

+90
-37
lines changed

src/util/pattern.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,18 @@ export function extractHostname(url: string): HostInfo {
9393
* @since 0.7.0
9494
*/
9595
export function extractFileHost(url: string): string {
96+
url = url?.trim?.()
97+
if (!url) {
98+
return undefined
99+
}
96100
if (!url.startsWith("file://")) {
97101
return undefined
98102
}
99103
const dotIdx = url.lastIndexOf(".")
100104
if (dotIdx < 0) {
101105
return undefined
102106
}
103-
const suffix = url.substring(dotIdx + 1)?.toLowerCase()
107+
const suffix = url.substring(dotIdx + 1).toLowerCase()
104108
return suffix ? SUFFIX_HOST_MAP[suffix] : undefined
105109
}
106110

src/util/site.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ const SPECIAL_MAP = {
2222
* @since 0.5.1
2323
*/
2424
export function extractSiteName(title: string, host?: string) {
25+
title = title?.trim?.()
2526
if (!title) {
2627
return undefined
2728
}
2829
if (host && SPECIAL_MAP[host]) {
2930
return SPECIAL_MAP[host]
3031
}
3132
return title
32-
.split?.(SEPARATORS)
33-
.filter?.(s => !INVALID_SITE_NAME.test(s))
34-
.sort?.((a, b) => a.length - b.length)[0]
35-
?.trim()
36-
|| undefined
33+
.split(SEPARATORS)
34+
.filter(s => !INVALID_SITE_NAME.test(s))
35+
.sort((a, b) => a.length - b.length)[0]
36+
.trim()
3737
}

test/util/array.test.ts

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,47 @@
55
* https://opensource.org/licenses/MIT
66
*/
77

8-
import { groupBy, rotate } from "@util/array"
8+
import { groupBy, rotate, sum } from "@util/array"
99

10-
test('group by', () => {
11-
const arr = [
12-
[1, 2],
13-
[1, 3],
14-
[2, 3],
15-
[3, 4],
16-
[2, 9]
17-
]
18-
// Find the max value of each group
19-
const maxMap = groupBy(arr, a => a[0], arr => Math.max(...arr.map(a => a[1])))
20-
expect(maxMap).toEqual({
21-
1: 3,
22-
2: 9,
23-
3: 4
10+
describe("util/array", () => {
11+
12+
test('group by', () => {
13+
const arr = [
14+
[1, 2],
15+
[1, 3],
16+
[2, 3],
17+
[3, 4],
18+
[2, 9]
19+
]
20+
// Find the max value of each group
21+
const maxMap = groupBy(arr, a => a[0], arr => Math.max(...arr.map(a => a[1])))
22+
expect(maxMap).toEqual({
23+
1: 3,
24+
2: 9,
25+
3: 4
26+
})
27+
const allVal = groupBy(arr, _ => undefined, arr => arr.map(a => a[1]))
28+
expect(allVal).toEqual({ undefined: [2, 3, 3, 4, 9] })
29+
})
30+
31+
test("rotate", () => {
32+
const arr = [1, 2, 3, 4, 5, 6]
33+
// Left rotate for 1 time
34+
rotate(arr)
35+
expect(arr).toEqual([2, 3, 4, 5, 6, 1])
36+
// Left rotate again for 2 times
37+
rotate(arr, 2, false)
38+
expect(arr).toEqual([4, 5, 6, 1, 2, 3])
39+
// Right rotate for 3 times
40+
rotate(arr, 3, true)
41+
expect(arr).toEqual([1, 2, 3, 4, 5, 6])
2442
})
25-
const allVal = groupBy(arr, _ => undefined, arr => arr.map(a => a[1]))
26-
expect(allVal).toEqual({ undefined: [2, 3, 3, 4, 9] })
27-
})
2843

29-
test("rotate", () => {
30-
const arr = [1, 2, 3, 4, 5, 6]
31-
// Left rotate for 1 time
32-
rotate(arr)
33-
expect(arr).toEqual([2, 3, 4, 5, 6, 1])
34-
// Left rotate again for 2 times
35-
rotate(arr, 2, false)
36-
expect(arr).toEqual([4, 5, 6, 1, 2, 3])
37-
// Right rotate for 3 times
38-
rotate(arr, 3, true)
39-
expect(arr).toEqual([1, 2, 3, 4, 5, 6])
40-
})
44+
test("sum", () => {
45+
let arr: number[] = [1, undefined, 2, 3, 4, null, NaN]
46+
expect(10).toEqual(sum(arr))
47+
48+
arr = undefined
49+
expect(0).toEqual(sum(arr))
50+
})
51+
})

test/util/pattern.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { extractHostname, isBrowserUrl, isHomepage, isIpAndPort, isValidHost } from "@util/pattern"
1+
import { JSON_HOST, PDF_HOST, PIC_HOST, TXT_HOST } from "@util/constant/remain-host"
2+
import { extractFileHost, extractHostname, isBrowserUrl, isHomepage, isIpAndPort, isValidHost } from "@util/pattern"
23

34
test('browser url', () => {
45
// chrome
@@ -58,4 +59,23 @@ test("homepage", () => {
5859
expect(isHomepage("https://baidu.com/a")).toBeFalsy()
5960
expect(isHomepage("https://baidu.com/a?a=2")).toBeFalsy()
6061
expect(isHomepage("https://baidu.com?a=2")).toBeFalsy()
62+
})
63+
64+
test("extractFileHost", () => {
65+
expect(extractFileHost("file://123.json")).toEqual(JSON_HOST)
66+
expect(extractFileHost("file://123.jpeg")).toEqual(PIC_HOST)
67+
expect(extractFileHost("file://123.txt")).toEqual(TXT_HOST)
68+
expect(extractFileHost("file://123.jpeg ")).toEqual(PIC_HOST)
69+
expect(extractFileHost(" file://123.jpeg ")).toEqual(PIC_HOST)
70+
expect(extractFileHost("file://123.pdf")).toEqual(PDF_HOST)
71+
72+
expect(extractFileHost(undefined)).toEqual(undefined)
73+
expect(extractFileHost("")).toEqual(undefined)
74+
expect(extractFileHost(" ")).toEqual(undefined)
75+
expect(extractFileHost(null)).toEqual(undefined)
76+
expect(extractFileHost("file:/123.json")).toEqual(undefined)
77+
expect(extractFileHost("file://123. jpeg")).toEqual(undefined)
78+
expect(extractFileHost("file://123json")).toEqual(undefined)
79+
expect(extractFileHost("file://123.html")).toEqual(undefined)
80+
expect(extractFileHost("file://123.")).toEqual(undefined)
6181
})

test/util/site.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import { extractSiteName } from "@util/site"
99

1010
test('extract site name', () => {
11+
expect(extractSiteName("")).toEqual(undefined)
12+
expect(extractSiteName(undefined)).toEqual(undefined)
13+
expect(extractSiteName(null)).toEqual(undefined)
14+
expect(extractSiteName(" ")).toEqual(undefined)
1115
expect(extractSiteName('Product Hunt – The best new products in tech.')).toEqual('Product Hunt')
1216
expect(extractSiteName('Product Hunt – The - best new products in tech.')).toEqual('The')
1317

test/util/stat.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Copyright (c) 2022 Hengyang Zhang
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
18
import { mergeResult, resultOf, createZeroResult } from "@util/stat"
29

310
test('default values of WastePerDay', () => {

test/util/time.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Copyright (c) 2021 Hengyang Zhang
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
18
import { daysAgo, formatPeriod, formatPeriodCommon, formatTime, getMonthTime, getStartOfDay, getWeeksAgo, getWeekTime, isSameDay } from "@util/time"
29

310
test('time', () => {

0 commit comments

Comments
 (0)