Skip to content

Commit 8a3a380

Browse files
author
zhaozhiwen
committed
test: 测试集成
1 parent 473a945 commit 8a3a380

File tree

8 files changed

+1449
-51
lines changed

8 files changed

+1449
-51
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<Alert /> should render alert with type 1`] = `
4+
<div
5+
class="happy-alert"
6+
style="background: rgb(83, 82, 237);"
7+
>
8+
info
9+
</div>
10+
`;
11+
12+
exports[`<Alert /> should render alert with type 2`] = `
13+
<div
14+
class="happy-alert"
15+
style="background: rgb(255, 165, 2);"
16+
>
17+
warning
18+
</div>
19+
`;
20+
21+
exports[`<Alert /> should render alert with type 3`] = `
22+
<div
23+
class="happy-alert"
24+
style="background: rgb(46, 213, 115);"
25+
>
26+
positive
27+
</div>
28+
`;
29+
30+
exports[`<Alert /> should render alert with type 4`] = `
31+
<div
32+
class="happy-alert"
33+
style="background: rgb(255, 71, 87);"
34+
>
35+
negative
36+
</div>
37+
`;
38+
39+
exports[`<Alert /> should render default 1`] = `
40+
<div>
41+
<div
42+
class="happy-alert"
43+
style="background: rgb(83, 82, 237);"
44+
>
45+
default
46+
</div>
47+
</div>
48+
`;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import Alert from '../alert';
4+
5+
describe('<Alert />', () => {
6+
test('should render default', () => {
7+
const { container } = render(<Alert>default</Alert>);
8+
expect(container).toMatchSnapshot();
9+
});
10+
11+
test('should render alert with type', () => {
12+
const kinds: any[] = ['info', 'warning', 'positive', 'negative'];
13+
14+
const { getByText } = render(
15+
<>
16+
{kinds.map(k => (
17+
<Alert kind={k} key={k}>
18+
{k}
19+
</Alert>
20+
))}
21+
</>,
22+
);
23+
24+
kinds.forEach(k => {
25+
expect(getByText(k)).toMatchSnapshot();
26+
});
27+
});
28+
});

components/alert/style/index.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
padding: 20px;
55
color: white;
66
background: white;
7-
border-radius: 3;
7+
border-radius: 3px;
88
}

gulpfile.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ const paths = {
1212
dist: 'dist',
1313
},
1414
styles: 'components/**/*.less',
15-
scripts: ['components/**/*.{ts,tsx}', '!components/**/demo/*.{ts,tsx}'],
15+
scripts: [
16+
'components/**/*.{ts,tsx}',
17+
'!components/**/demo/*.{ts,tsx}',
18+
'!components/**/__tests__/*.{ts,tsx}',
19+
],
1620
};
1721

1822
/**

jest.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
verbose: true,
3+
roots: ['<rootDir>/components'],
4+
moduleNameMapper: {
5+
'\\.(css|less|scss)$': 'identity-obj-proxy',
6+
'^components$': '<rootDir>/components/index.tsx',
7+
'^components(.*)$': '<rootDir>/components/$1',
8+
},
9+
testRegex: '(/test/.*|\\.(test|spec))\\.(ts|tsx|js)$',
10+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
11+
testPathIgnorePatterns: ['/node_modules/', '/lib/', '/esm/', '/dist/'],
12+
preset: 'ts-jest',
13+
testEnvironment: 'jsdom',
14+
};

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
"build:types": "tsc --emitDeclarationOnly",
1515
"build": "npm run clean && npm run build:types && gulp",
1616
"commit": "git-cz",
17-
"test": "echo \"Error: no test specified\" && exit 1"
17+
"test": "jest",
18+
"test:watch": "jest --watch",
19+
"test:coverage": "jest --coverage",
20+
"test:update": "jest --updateSnapshot"
1821
},
1922
"repository": {
2023
"type": "git",
@@ -47,6 +50,10 @@
4750
"@babel/preset-typescript": "^7.7.7",
4851
"@commitlint/cli": "^8.2.0",
4952
"@commitlint/config-conventional": "^8.2.0",
53+
"@testing-library/jest-dom": "^4.2.4",
54+
"@testing-library/react": "^9.4.0",
55+
"@types/jest": "^24.0.25",
56+
"@types/testing-library__react": "^9.1.2",
5057
"@types/vfile-message": "^2.0.0",
5158
"@umijs/fabric": "^1.2.12",
5259
"antd": "^3.26.3",
@@ -62,6 +69,8 @@
6269
"gulp-cssnano": "^2.1.3",
6370
"gulp-less": "^4.0.1",
6471
"husky": "^3.1.0",
72+
"identity-obj-proxy": "^3.0.0",
73+
"jest": "^24.9.0",
6574
"less": "^3.10.3",
6675
"lint-staged": "^9.5.0",
6776
"prettier": "^1.19.1",
@@ -73,13 +82,16 @@
7382
"react-simple-code-editor": "^0.10.0",
7483
"react-use": "^13.12.2",
7584
"rimraf": "^3.0.0",
85+
"snapshot-diff": "^0.6.1",
7686
"through2": "^3.0.1",
87+
"ts-jest": "^24.2.0",
7788
"typescript": "^3.7.3"
7889
},
7990
"lint-staged": {
8091
"components/**/*.ts?(x)": [
8192
"prettier --write",
8293
"eslint --fix",
94+
"jest --bail --findRelatedTests",
8395
"git add"
8496
],
8597
"components/**/*.less": [

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"esModuleInterop": true
1313
},
1414
"include": ["components"],
15-
"exclude": ["components/**/demo"]
15+
"exclude": ["components/**/demo", "components/**/__tests__"]
1616
}

0 commit comments

Comments
 (0)