Skip to content

Commit 2f29f50

Browse files
author
sergey.volkov
committed
test support for react component
1 parent 29a731e commit 2f29f50

File tree

7 files changed

+219
-16
lines changed

7 files changed

+219
-16
lines changed

Diff for: .babelrc

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
{
2-
"plugins": ["wildcard", "transform-decorators-legacy"],
3-
"presets": ["env", "react", "es2015", "stage-0"]
2+
"plugins": ["wildcard", "transform-decorators-legacy", "syntax-dynamic-import", "transform-es2015-modules-commonjs"],
3+
"presets": ["env", "react", "es2016", "stage-0"],
4+
5+
"env": {
6+
"test": {
7+
"presets":[
8+
"env",
9+
"es2016",
10+
"react",
11+
"stage-0"
12+
],
13+
"plugins": [
14+
"wildcard",
15+
"transform-decorators-legacy",
16+
"transform-es2015-modules-commonjs"
17+
]
18+
}
19+
}
20+
421
}

Diff for: .vscode/launch.json

+25-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@
2424
"webRoot": "${workspaceRoot}/src",
2525

2626
"sourceMaps": true
27-
}
27+
},
28+
{
29+
"type": "node",
30+
"request": "launch",
31+
"name": "Jest All",
32+
"program": "${workspaceFolder}/node_modules/.bin/jest",
33+
"args": ["--runInBand"],
34+
"console": "integratedTerminal",
35+
"internalConsoleOptions": "neverOpen",
36+
"windows": {
37+
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
38+
}
39+
},
40+
{
41+
"type": "node",
42+
"request": "launch",
43+
"name": "Jest Current File",
44+
"program": "${workspaceFolder}/node_modules/.bin/jest",
45+
"args": ["${relativeFile}"],
46+
"console": "integratedTerminal",
47+
"internalConsoleOptions": "neverOpen",
48+
"windows": {
49+
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
50+
}
51+
}
2852
]
2953
}

Diff for: config/setupTests.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Adapter from 'enzyme-adapter-react-16';
2+
import React from 'react';
3+
import { shallow, render, mount, configure } from 'enzyme';
4+
5+
configure({ 'adapter': new Adapter() });
6+
7+
global.shallow = shallow;
8+
global.render = render;
9+
global.mount = mount;
10+
11+
//mock store for redux
12+
import configureMockStore from "redux-mock-store";
13+
global.mockStore = function (store) { const mock = configureMockStore(); return mock(store); };
14+
15+

Diff for: package.json

+31-8
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@
1717
"jest": {
1818
"verbose": true,
1919
"testEnvironment": "<rootDir>/__mocks__/setupEnvironment.js",
20-
"collectCoverage": true,
20+
"setupTestFrameworkScriptFile": "<rootDir>/config/setupTests.js",
21+
"collectCoverage": false,
2122
"collectCoverageFrom": [
2223
"src/**/*.{js,jsx}",
2324
"!**/node_modules/**"
2425
],
26+
"transform": {
27+
"^.+\\.js$": "babel-jest",
28+
"^.+\\.jsx$": "babel-jest"
29+
},
30+
"transformIgnorePatterns": [
31+
"node_modules/(?!(react-native|react-native-button|redux)/)"
32+
],
2533
"globals": {
2634
"__DEV__": true
2735
},
@@ -32,23 +40,29 @@
3240
"moduleDirectories": [
3341
"node_modules"
3442
],
43+
"unmockedModulePathPatterns": [
44+
"<rootDir>/node_modules/react"
45+
],
3546
"modulePaths": [
3647
"src",
3748
"config"
3849
],
3950
"testPathIgnorePatterns": [
40-
"<rootDir>/node_modules/"
51+
"<rootDir>/node_modules/",
52+
"<rootDir>/config/",
53+
"<rootDir>/webpack/"
4154
],
4255
"moduleNameMapper": {
56+
"^config$": "<rootDir>/config/config.common.js",
57+
"ress|css-loading": "<rootDir>/__mocks__/fileMock.js",
4358
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
44-
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
59+
"\\.(css|less|styl)$": "<rootDir>/__mocks__/styleMock.js"
4560
}
4661
},
4762
"dependencies": {
4863
"autoprefixer": "^6.7.7",
4964
"axios": "^0.17.0",
5065
"babel-cli": "^6.26.0",
51-
"babel-core": "^6.26.3",
5266
"babel-jest": "^23.6.0",
5367
"babel-loader": "^7.0.0",
5468
"babel-plugin-transform-class-properties": "^6.10.2",
@@ -57,11 +71,8 @@
5771
"babel-plugin-transform-object-assign": "^6.5.0",
5872
"babel-plugin-transform-runtime": "^6.5.2",
5973
"babel-plugin-wildcard": "^5.0.0",
60-
"babel-polyfill": "^6.26.0",
6174
"babel-preset-env": "^1.6.1",
6275
"babel-preset-es2015": "^6.0.14",
63-
"babel-preset-react": "^6.11.1",
64-
"babel-preset-stage-0": "^6.24.1",
6576
"babel-runtime": "^6.26.0",
6677
"classnames": "^2.2.5",
6778
"config": "^1.29.2",
@@ -152,21 +163,33 @@
152163
"zenscroll": "^4.0.2"
153164
},
154165
"devDependencies": {
166+
"@babel/preset-react": "^7.0.0",
155167
"@types/react": "^16.4.14",
168+
"babel": "^6.23.0",
169+
"babel-core": "^6.26.3",
156170
"babel-minify-webpack-plugin": "^0.2.0",
157171
"babel-plugin-add-module-exports": "^0.2.1",
172+
"babel-plugin-dynamic-import-node": "^2.1.0",
158173
"babel-plugin-transform-decorators-legacy": "^1.3.5",
159174
"babel-plugin-transform-es2015-arrow-functions": "^6.22.0",
160175
"babel-plugin-transform-es2015-block-scoping": "^6.26.0",
161176
"babel-plugin-transform-es2015-for-of": "^6.23.0",
162177
"babel-plugin-transform-object-rest-spread": "^6.26.0",
163178
"babel-plugin-wildcard": "^5.0.0",
179+
"babel-polyfill": "^6.26.0",
180+
"babel-preset-es2016": "^6.24.1",
164181
"babel-preset-minify": "^0.2.0",
182+
"babel-preset-react": "^6.24.1",
183+
"babel-preset-stage-0": "^6.24.1",
165184
"clean-webpack-plugin": "^0.1.19",
185+
"enzyme": "^3.7.0",
186+
"enzyme-adapter-react-16": "^1.6.0",
166187
"gh-pages": "^1.2.0",
167188
"optimize-css-assets-webpack-plugin": "^3.2.0",
189+
"react-addons-test-utils": "^15.6.2",
168190
"redux-devtools-extension": "^2.13.5",
169-
"rollup-plugin-commonjs": "^8.3.0",
191+
"redux-mock-store": "^1.5.3",
192+
"rollup-plugin-commonjs": "^8.4.1",
170193
"rollup-plugin-replace": "^2.0.0",
171194
"rollup-plugin-uglify": "^3.0.0",
172195
"style-loader": "^0.19.1",

Diff for: src/Services/CacheService/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class CacheServiceClass {
2525
return value;
2626
}
2727
}
28+
29+
clearCache = () => {
30+
this.cache = {};
31+
}
2832

2933
}
3034

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from 'react';
2+
import { call, put } from 'redux-saga/effects';
3+
import assert from 'assert';
4+
5+
import {LoaderComponent } from './Loader/component';
6+
7+
8+
const mockNotifications = {
9+
showLoader: false,
10+
message: '',
11+
progressTick: 0,
12+
notifications: []
13+
};
14+
15+
16+
test( 'GlobalNotifaction, test Loader hide', () => {
17+
18+
const store = mockStore({
19+
notifications: mockNotifications
20+
});
21+
22+
const wrapper = mount(
23+
<LoaderComponent
24+
store={store}
25+
/>
26+
);
27+
28+
expect(wrapper.exists('.loader')).toEqual(false);
29+
30+
} );
31+
32+
test( 'GlobalNotifaction, test Loader show', () => {
33+
34+
const store = mockStore({
35+
notifications: {
36+
...mockNotifications,
37+
showLoader: true
38+
}
39+
});
40+
41+
const wrapper = mount(
42+
<LoaderComponent
43+
store={store}
44+
/>
45+
);
46+
47+
const p = wrapper.find('.loader');
48+
expect(p.length).toBe(1);
49+
50+
} );
51+

Diff for: src/components/SecuritySelector/securitySelector.test.js

+74-5
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,105 @@ import { call, put } from 'redux-saga/effects';
44
import assert from 'assert';
55

66
import { securitySearchFunc } from './sagas';
7-
import { SecurityService } from 'Services';
7+
import { SecurityService, CacheService } from 'Services';
8+
import {SecuritySelectorComponent } from './component';
89
import { ACTIONS } from './actions';
10+
import {Map} from 'immutable';
911

1012

11-
test( 'test secirity search OK', () => {
13+
const mockSuggestions = {
14+
suggestions: {
15+
securitiesSuggestionsList: [{
16+
id: 76089,
17+
securityId: 'GBPRUB_TOM',
18+
shortName: 'GBPRUB_TOM',
19+
regNumber: null,
20+
name: 'GBP/RUB_TOM - GBP/РУБ',
21+
isin: null,
22+
isTraded: 1,
23+
type: 'currency',
24+
group: 'currency_selt',
25+
primaryBoardId: 'CETS',
26+
marketPriceBoardId: null,
27+
providerCode: 'MOEX',
28+
currency: 'RUB'
29+
}]
30+
},
31+
suggestionList: [],
32+
references: {
33+
securityTypes: [{
34+
id: 3,
35+
engineId: 1,
36+
engineName: 'stock',
37+
engineTitle: 'Фондовый рынок и рынок депозитов',
38+
typeName: 'common_share',
39+
typeTitle: 'Акция обыкновенная'
40+
}],
41+
securityGroups: [{
42+
id: 12,
43+
name: 'stock_index',
44+
title: 'Индексы',
45+
isHidden: 0
46+
}, {
47+
id: 4,
48+
name: 'stock_shares',
49+
title: 'Акции',
50+
isHidden: 0
51+
}]
52+
}
53+
};
54+
55+
56+
test( 'securitySelector component', () => {
57+
58+
const store = mockStore({
59+
...mockSuggestions
60+
});
61+
62+
const wrapper = mount(
63+
<SecuritySelectorComponent
64+
store={store}
65+
/>
66+
);
67+
68+
const p = wrapper.find(".react-autosuggest");
69+
expect(p.length).toBe(1);
70+
71+
} );
72+
73+
test( 'securitySelector search OK', () => {
74+
75+
CacheService.clearCache();
76+
1277
const iterator = securitySearchFunc( { text: 'ind' } );
78+
//expect(iterator.next()).toEqual(call(CacheService.getOrAdd));
1379

1480
iterator.next();
1581
iterator.next();
1682

1783
// expects a call instruction
1884
assert.deepEqual(
1985
iterator.next().value,
20-
call( SecurityService.findSecurity, 'ind' ),
86+
put( {data: {suggestionsList: undefined}, type: ACTIONS.SECURITY_SEARCH_SUCCEEDED} ),
2187
'securitySearchFunc should yield an Effect findSecurity'
2288
);
2389

2490
} );
2591

26-
test( 'test secirity search FAIL', () => {
92+
test( 'securitySelector search FAIL', () => {
2793
const iterator = securitySearchFunc( { text: 'ind' } );
2894

95+
iterator.next();
2996
iterator.next();
3097

3198
// create a fake error
3299
const error = {};
33100

101+
iterator.throw( error );
102+
34103
// expects a dispatch instruction
35104
assert.deepEqual(
36-
iterator.throw( error ).value,
105+
iterator.next().value, // skip loader action
37106
put( { type: ACTIONS.SECURITY_SEARCH_FAILED, error } ),
38107
`securitySearchFunc should yield an Effect put({ type: '${ ACTIONS.SECURITY_SEARCH_FAILED }', error })`
39108
);

0 commit comments

Comments
 (0)