Skip to content

Commit c877de3

Browse files
authored
Merge pull request #11 from BiYuqi/improve-test-cases
Improve test cases
2 parents 4874854 + a474e34 commit c877de3

File tree

4 files changed

+108
-21
lines changed

4 files changed

+108
-21
lines changed

karma.conf.js

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,6 @@ module.exports = function(config) {
6666
// level of logging
6767
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
6868
logLevel: config.LOG_INFO,
69-
70-
// 设置此项 报错信息不提示
71-
// client: {
72-
// captureConsole: true,
73-
// mocha: {
74-
// bail: true
75-
// }
76-
// },
7769

7870
// enable / disable watching file and executing tests whenever any file changes
7971
autoWatch: true,
@@ -101,6 +93,11 @@ module.exports = function(config) {
10193
loader: 'babel-loader'
10294
}
10395
},
96+
{
97+
test: /\.ejs$/,
98+
include: resolve('src'),
99+
loader: 'ejs-loader'
100+
},
104101
{
105102
test: /\.(sa|sc|c)ss$/,
106103
use: [
@@ -114,22 +111,47 @@ module.exports = function(config) {
114111
loader: 'sass-loader'
115112
}
116113
]
114+
},
115+
{
116+
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
117+
loader: 'url-loader',
118+
options: {
119+
limit: 10000,
120+
name: 'image/[name].[hash:7].[ext]'
121+
}
122+
},
123+
{
124+
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
125+
loader: 'url-loader',
126+
options: {
127+
limit: 10000,
128+
name: 'media/[name].[hash:7].[ext]'
129+
}
130+
},
131+
{
132+
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
133+
loader: 'url-loader',
134+
options: {
135+
limit: 10000,
136+
name: 'fonts/[name].[hash:7].[ext]'
137+
}
117138
}
118139
]
119140
},
120141
resolve: {
121-
extensions: ['.js'],
142+
extensions: ['.js', '.json'],
143+
// 配置项目文件别名
122144
alias: {
123-
'@': resolve('./src'),
124-
utils: resolve('./src/utils')
145+
'@': resolve('src'),
146+
assets: resolve('src/common/assets'),
147+
utils: resolve('src/common/utils'),
148+
layout: resolve('src/layout'),
149+
build: resolve('build')
125150
}
126151
}
127152
},
128153
webpackServer: {
129154
noInfo: true
130155
}
131-
// webpackMiddleware: {
132-
// stats: 'errors-only'
133-
// }
134156
})
135157
}

src/common/utils/tools.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@ export const isEmptyObject = obj => {
88
}
99
return true
1010
}
11+
12+
export const pickBy = object => {
13+
if (!object) return
14+
const result = {}
15+
Object.keys(object).forEach(key => {
16+
if (object[key]) {
17+
result[key] = object[key]
18+
}
19+
})
20+
return result
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expect } from 'chai'
2+
import EjsTest from '@/templates/index.test.ejs'
3+
4+
describe('Test Ejs Template', () => {
5+
const data = {
6+
index: [
7+
{
8+
link: 'html/test-demo.html',
9+
key: '测试页面',
10+
icon: 'iconfont icon-rocket'
11+
}
12+
]
13+
}
14+
const html = EjsTest(data)
15+
const classReg = /class="([^"]+)"/gm
16+
const hrefReg = /href="([^"]+)"/
17+
18+
it('should render correct className ejs-dynamic-item', () => {
19+
expect(classReg.exec(html)[1]).to.equal('ejs-dynamic-item')
20+
})
21+
22+
it('should render correct icon className iconfont icon-rocket', () => {
23+
expect(classReg.exec(html)[1]).to.equal('iconfont icon-rocket')
24+
})
25+
26+
it('should render correct href link', () => {
27+
expect(hrefReg.exec(html)[1]).to.equal('html/test-demo.html')
28+
})
29+
})

test/utils-test.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
import { expect } from 'chai'
2-
import { isEmptyObject } from '@/common/utils/tools.js'
2+
import { isEmptyObject, pickBy } from '@/common/utils/tools.js'
33

4-
describe('Test project tools\'s method', () => {
5-
it('isEmptyObject\'s param empty object should be return true', () => {
6-
expect(isEmptyObject({})).to.equal(true)
4+
describe('Test utils', () => {
5+
describe('isEmptyObject', () => {
6+
it('should return true', () => {
7+
expect(isEmptyObject({})).to.equal(true)
8+
})
9+
10+
it('should return false', () => {
11+
expect(isEmptyObject({ a: 1 })).to.equal(false)
12+
})
713
})
814

9-
it('isEmptyObject\'s param not empty object should be return false', () => {
10-
expect(isEmptyObject({a: 1})).to.equal(false)
15+
describe('pickBy', () => {
16+
it('should return correct value', () => {
17+
const testCaseOne = {
18+
testA: 'testA',
19+
testB: null,
20+
testC: false
21+
}
22+
expect(pickBy(testCaseOne)).to.deep.equal({
23+
testA: 'testA'
24+
})
25+
})
26+
27+
it('should return correct value', () => {
28+
const testCaseTwo = {
29+
testD: 'testD',
30+
testE: NaN
31+
}
32+
expect(pickBy(testCaseTwo)).to.deep.equal({
33+
testD: 'testD'
34+
})
35+
})
1136
})
12-
})
37+
})

0 commit comments

Comments
 (0)