Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.

Commit 9fa7f3d

Browse files
committed
test(tools): add testcase
1 parent e10da51 commit 9fa7f3d

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

src/utils/tools.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const isRelativeURL = (path: string) => {
1414
export const makeResourceReg = (str: string): RegExp => {
1515
return {
1616
script: /\<script\s+\S?src\=\"([^"]*)\"/g,
17-
style: /\<link\s+\S?\s?href\=\"([^"]*.css)\"/,
17+
style: /\<link\s+\S?\s?href\=\"([^"]*.css)\"/g,
1818
}[str]
1919
}
2020

@@ -32,14 +32,17 @@ export const filterResources = (source: string, type: string): string[] => {
3232

3333
export const listenImageLoad = (images: HTMLImageElement[], done: (url: string) => void)
3434
: void => {
35-
const isCompleted = (imgs: HTMLImageElement[]) => !imgs.length
36-
const timer: number = window.setInterval(() => {
37-
images = images.map(img => {
38-
if (!img.complete) return img
39-
done(img.src)
40-
return null
41-
})
42-
.filter(v => !!v)
43-
isCompleted(images) && clearInterval(timer)
44-
}, 300)
35+
images.forEach(img => img.onload = () => done(img.src))
36+
// const isCompleted = (imgs: HTMLImageElement[]) => !imgs.length
37+
// const timer: number = window.setInterval(() => {
38+
//
39+
// // images = images.map(img => {
40+
// // console.log(img)
41+
// // if (!img.complete) return img
42+
// // done(img.src)
43+
// // return null
44+
// // })
45+
// // .filter(v => !!v)
46+
// isCompleted(images) && clearInterval(timer)
47+
// }, 300)
4548
}

test/utils/check.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Check from '../../src/utils/check'
22
import { expect } from 'chai'
33

4-
describe('Utils function test', () => {
4+
describe('Check function test', () => {
55

66
let _warn: (msg?: any, ...p: any[]) => void
77
before(() => {

test/utils/tools.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { expect } from 'chai'
2+
import { filterResources, isRelativeURL } from '../../src/utils/tools'
3+
4+
const scriptMock = `
5+
<body>
6+
<script src="host.com/static/hello.js"></script>
7+
<script src="../static/hello.js"></script>
8+
<scrip src="../static/error.js"></scrip>
9+
<script link="../static/error.js"></script>
10+
</body>
11+
`
12+
const scriptResult = 'host.com/static/hello.js'
13+
14+
const styleMock = `
15+
<head>
16+
<link href="host.com/static/hello.css">
17+
<link href="host.com/static/hello.js">
18+
<link src="host.com/static/hello.css">
19+
<lin src="../static/error.css">
20+
<link link="../static/error.css">
21+
</head>
22+
`
23+
const styleResult = 'host.com/static/hello.css'
24+
25+
describe('Tool function test', () => {
26+
27+
it('should inspect relative url and return boolean', () => {
28+
expect(isRelativeURL('./static/a.html')).to.be.eq(true)
29+
expect(isRelativeURL('../static/a.html')).to.be.eq(true)
30+
expect(isRelativeURL('.../static/a.html')).to.be.eq(true)
31+
expect(isRelativeURL('/static/a.html')).to.be.eq(false)
32+
expect(isRelativeURL('www.host.com/static/a.html')).to.be.eq(false)
33+
})
34+
35+
it('should filter and find scripts', () => {
36+
expect(filterResources(scriptMock, 'script'))
37+
.to.be.lengthOf(1)
38+
.to.be.eqls([scriptResult])
39+
})
40+
41+
it('should filter and find styles', () => {
42+
expect(filterResources(styleMock, 'style'))
43+
.to.be.lengthOf(1)
44+
.to.be.eqls([styleResult])
45+
})
46+
47+
})

0 commit comments

Comments
 (0)