Skip to content

Commit 143e87b

Browse files
committed
feat(test): add jest and test case
1 parent 1c05be9 commit 143e87b

File tree

11 files changed

+13009
-37
lines changed

11 files changed

+13009
-37
lines changed

.babelrc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"modules": false,
7+
"useBuiltIns": "entry",
8+
"targets": {
9+
"browsers": ["last 2 versions"]
10+
}
11+
}
12+
],
13+
"@babel/preset-react"
14+
],
15+
"plugins": [["@babel/proposal-class-properties", { "loose": true }]],
16+
"env": {
17+
"development": {
18+
"plugins": ["react-hot-loader/babel"]
19+
},
20+
"test": {
21+
"presets": ["@babel/preset-env", "@babel/preset-react"]
22+
}
23+
}
24+
}

.babelrc.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = {
1212
node: true,
1313
commonjs: true,
1414
es6: true,
15-
amd: true
15+
amd: true,
16+
"jest": true
1617
},
1718
plugins: [],
1819
rules: {

package.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"build:rollup": "cross-env NODE_ENV=production rollup -c",
2222
"build:webpack": "cross-env NODE_ENV=production webpack --config=./build/prod.conf.js",
2323
"dev": "cross-env NODE_ENV=development webpack-serve --config=./build/dev.conf.js --host 0.0.0.0 --port 2222",
24-
"pub": "npm run lint && npm run build:rollup && npm publish --registry http://registry.npmjs.org --access=public"
24+
"pub": "npm run lint && npm run build:rollup && npm publish --registry http://registry.npmjs.org --access=public",
25+
"test": "cross-env NODE_ENV=test jest",
26+
"test-watch": "cross-env NODE_ENV=test jest --watchAll"
2527
},
2628
"author": "zhengzwing@gmail.com",
2729
"license": "MIT",
@@ -91,5 +93,18 @@
9193
"path": "./node_modules/cz-conventional-changelog"
9294
}
9395
},
94-
"dependencies": {}
96+
"dependencies": {
97+
"babel-core": "^7.0.0-bridge.0",
98+
"babel-jest": "^23.6.0",
99+
"enzyme": "^3.7.0",
100+
"enzyme-adapter-react-16": "^1.6.0",
101+
"jest": "^23.6.0",
102+
"react-test-renderer": "^16.5.2"
103+
},
104+
"jest": {
105+
"moduleNameMapper": {
106+
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|scss)$": "<rootDir>/test/utils.js"
107+
},
108+
"setupTestFrameworkScriptFile": "<rootDir>/test/setup.js"
109+
}
95110
}

rollup.config.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import sass from 'node-sass'
88
import autoprefixer from 'autoprefixer'
99
import postcssurl from 'postcss-url'
1010
import pkg from './package.json'
11-
const babelrc = require('./.babelrc.js')
1211
export default {
1312
input: 'src/index.js',
1413
output: [
@@ -25,9 +24,9 @@ export default {
2524
external(),
2625
postcss({
2726
preprocessor: (content, id) => new Promise(res => {
28-
const result = sass.renderSync({ file: id })
29-
res({ code: result.css.toString() })
30-
}),
27+
const result = sass.renderSync({ file: id })
28+
res({ code: result.css.toString() })
29+
}),
3130
plugins: [autoprefixer, postcssurl({ url: 'inline' })],
3231
minimize: true,
3332
// extract: true,
@@ -39,8 +38,7 @@ export default {
3938
emitFiles: true // defaults to true
4039
}),
4140
babel({
42-
exclude: 'node_modules/**',
43-
...babelrc
41+
exclude: 'node_modules/**'
4442
// externalHelpers: true
4543
}),
4644
resolve({
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react'
2+
import ErrorIcon from '..'
3+
import { shallow, mount, render } from 'enzyme'
4+
// import renderer from 'react-test-renderer'
5+
describe('<ErrorIcon />', () => {
6+
it('ErrorIcon mounted', () => {
7+
const result = shallow(<ErrorIcon />)
8+
expect(result.children().length).toEqual(0)
9+
})
10+
it('ErrorIcon style', () => {
11+
const style = { width: '100px'}
12+
const result = shallow(<ErrorIcon style={style} />)
13+
expect(result.prop('style')).toEqual({
14+
backgroundImage: 'url(test-file-stub)',
15+
backgroundPosition: 'center',
16+
backgroundRepeat: 'no-repeat',
17+
backgroundSize: '65% 65%',
18+
border: '1px dashed #dadada',
19+
height: '100%',
20+
// width: '100px'
21+
...style
22+
})
23+
})
24+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import Image from '..'
3+
import { shallow } from 'enzyme'
4+
5+
describe('Image', () => {
6+
it('Image Mounted', () => {
7+
const result = shallow(<Image src='test.png'/>)
8+
expect(result.instance().props.src).toEqual('test.png')
9+
})
10+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
import ErrorIcon from '..'
3+
import { shallow } from 'enzyme'
4+
// import renderer from 'react-test-renderer'
5+
describe('<LoadingIcon />', () => {
6+
it('LoadingIcon mounted', () => {
7+
const result = shallow(<ErrorIcon/>)
8+
expect(result.children().length).toEqual(4)
9+
})
10+
it('LoadingIcon size', () => {
11+
const result = shallow(<ErrorIcon size='sm'/>)
12+
expect(result.hasClass('lds-ring')).toEqual(true)
13+
expect(result.hasClass('sm')).toEqual(true)
14+
expect(result.hasClass('lg')).toEqual(false)
15+
})
16+
})

test/setup.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Enzyme from 'enzyme'
2+
import Adapter from 'enzyme-adapter-react-16'
3+
4+
Enzyme.configure({ adapter: new Adapter() })

test/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'test-file-stub'

0 commit comments

Comments
 (0)