Skip to content

Commit d07154b

Browse files
committed
Converts react-tree-walker to be promised based.
1 parent 888fde3 commit d07154b

File tree

8 files changed

+680
-1043
lines changed

8 files changed

+680
-1043
lines changed

.eslintrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
"root": true,
33
"parser": "babel-eslint",
44
"extends": "airbnb",
5-
"plugins": [
6-
"flowtype"
7-
],
85
"env": {
96
"browser": true,
107
"es6": true,

.flowconfig

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

package.json

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@
2626
"scripts": {
2727
"build": "babel-node ./tools/scripts/build.js",
2828
"check": "yarn run lint && yarn run test",
29-
"clean": "rimraf ./commonjs && rimraf ./umd && rimraf ./coverage && rimraf ./flow-coverage && rimraf ./umd",
30-
"example:web": "echo 'Make sure to `cd example/web && yarn install`' && cd example/web && yarn run start",
31-
"flow": "babel-node ./tools/scripts/flow",
32-
"flow:coverage": "flow-coverage-report -i 'src/**/*.js' -t html -t json -t text",
33-
"flow:defs": "flow-typed install --overwrite",
29+
"clean": "rimraf ./commonjs && rimraf ./umd && rimraf ./coverage && rimraf ./umd",
3430
"lint": "eslint src",
3531
"prepublish": "yarn run build",
3632
"test": "jest",
@@ -42,45 +38,41 @@
4238
},
4339
"devDependencies": {
4440
"app-root-dir": "1.0.2",
45-
"babel-cli": "6.22.2",
46-
"babel-core": "6.22.1",
41+
"babel-cli": "6.23.0",
42+
"babel-core": "6.23.1",
4743
"babel-eslint": "7.1.1",
48-
"babel-jest": "18.0.0",
49-
"babel-loader": "6.2.10",
50-
"babel-polyfill": "6.22.0",
51-
"babel-preset-env": "1.1.8",
44+
"babel-jest": "19.0.0",
45+
"babel-loader": "6.4.0",
46+
"babel-polyfill": "6.23.0",
47+
"babel-preset-env": "1.2.1",
5248
"babel-preset-latest": "6.22.0",
53-
"babel-preset-react": "6.22.0",
49+
"babel-preset-react": "6.23.0",
5450
"babel-preset-stage-3": "6.22.0",
55-
"babel-register": "6.22.0",
51+
"babel-register": "6.23.0",
5652
"codecov": "1.0.1",
57-
"cross-env": "3.1.4",
53+
"cross-env": "3.2.3",
5854
"enzyme": "2.7.1",
59-
"enzyme-to-json": "1.4.5",
60-
"eslint": "3.14.0",
61-
"eslint-config-airbnb": "14.0.0",
62-
"eslint-plugin-flowtype": "2.30.0",
55+
"enzyme-to-json": "1.5.0",
56+
"eslint": "3.17.1",
57+
"eslint-config-airbnb": "14.1.0",
6358
"eslint-plugin-import": "2.2.0",
64-
"eslint-plugin-jsx-a11y": "3.0.2",
65-
"eslint-plugin-react": "6.9.0",
66-
"flow-bin": "0.38.0",
67-
"flow-coverage-report": "0.2.0",
68-
"flow-typed": "2.0.0",
59+
"eslint-plugin-jsx-a11y": "4.0.0",
60+
"eslint-plugin-react": "6.10.0",
6961
"ghooks": "2.0.0",
7062
"gzip-size": "3.0.0",
7163
"in-publish": "2.0.0",
72-
"jest": "18.1.0",
64+
"jest": "19.0.2",
7365
"pretty-bytes": "4.0.2",
7466
"ramda": "0.23.0",
7567
"react": "15.4.2",
7668
"react-addons-test-utils": "15.4.2",
7769
"react-dom": "15.4.2",
78-
"readline-sync": "1.4.5",
79-
"rimraf": "2.5.4",
70+
"readline-sync": "1.4.6",
71+
"rimraf": "2.6.1",
8072
"sinon": "1.17.7",
81-
"webpack": "2.2.0-rc.3",
82-
"webpack-dev-middleware": "1.9.0",
83-
"webpack-hot-middleware": "2.15.0"
73+
"webpack": "2.2.1",
74+
"webpack-dev-middleware": "1.10.1",
75+
"webpack-hot-middleware": "2.17.1"
8476
},
8577
"config": {
8678
"ghooks": {
@@ -97,5 +89,8 @@
9789
"testPathIgnorePatterns": [
9890
"<rootDir>/(commonjs|coverage|flow-typed|node_modules|tools|umd)/"
9991
]
92+
},
93+
"dependencies": {
94+
"p-map-series": "^1.0.0"
10095
}
10196
}

src/__tests__/index.test.js

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* @flow */
21
/* eslint-disable react/sort-comp */
32
/* eslint-disable react/no-multi-comp */
43
/* eslint-disable react/prop-types */
@@ -12,8 +11,6 @@ const Bob = ({ children }) => <div>{children}</div>;
1211

1312
describe('reactTreeWalker', () => {
1413
class Foo extends Component {
15-
props: { children?: any, something: any }
16-
1714
constructor(props) {
1815
super(props);
1916
// $FlowIgnore
@@ -53,56 +50,36 @@ describe('reactTreeWalker', () => {
5350
</div>
5451
);
5552

56-
it('simple sync', () => {
53+
it('simple sync visitor', () => {
5754
const tree = createTree(false);
5855
const actual = [];
5956
// eslint-disable-next-line no-unused-vars
6057
const visitor = (element, instance, context) => {
61-
if (instance && typeof instance.getSomething) {
58+
if (instance && typeof instance.getSomething === 'function') {
6259
const something = instance.getSomething();
6360
actual.push(something);
6461
}
6562
};
66-
reactTreeWalker(tree, visitor);
67-
const expected = [1, 2, 4, 5, 3];
68-
expect(actual).toEqual(expected);
63+
return reactTreeWalker(tree, visitor).then(() => {
64+
const expected = [1, 2, 4, 5, 3];
65+
expect(actual).toEqual(expected);
66+
});
6967
});
7068

71-
it('complex async', () => {
72-
const treeRoot = createTree(true);
69+
it('promise based visitor', () => {
70+
const tree = createTree(true);
7371
const actual = [];
74-
75-
const doWalk = (el, ctx = {}, fetchRoot = false) => {
76-
const somethings = [];
77-
78-
// eslint-disable-next-line no-unused-vars
79-
const visitor = (element, instance, context) => {
80-
const skipRoot = !fetchRoot && (element === el);
81-
if (instance && typeof instance.getSomething === 'function' && !skipRoot) {
82-
const something = instance.getSomething();
83-
somethings.push({ something, element, context });
84-
return false;
85-
}
86-
return undefined;
87-
};
88-
89-
reactTreeWalker(el, visitor, ctx);
90-
91-
// eslint-disable-next-line arrow-body-style
92-
const promises = somethings.map(({ something, element, context }) => {
93-
return something.then((result) => {
94-
actual.push(result);
95-
return doWalk(element, context);
72+
// eslint-disable-next-line no-unused-vars
73+
const visitor = (element, instance, context) => {
74+
if (instance && typeof instance.getSomething === 'function') {
75+
return instance.getSomething().then((something) => {
76+
actual.push(something);
9677
});
97-
});
98-
99-
return promises.length > 0
100-
? Promise.all(promises)
101-
: Promise.resolve([]);
78+
}
79+
return true;
10280
};
103-
104-
return doWalk(treeRoot, {}, true).then(() => {
105-
const expected = [1, 2, 3, 4, 5];
81+
return reactTreeWalker(tree, visitor).then(() => {
82+
const expected = [1, 2, 4, 5, 3];
10683
expect(actual).toEqual(expected);
10784
});
10885
});
@@ -137,9 +114,10 @@ describe('reactTreeWalker', () => {
137114
actual = instance.getState();
138115
}
139116
};
140-
reactTreeWalker(tree, visitor);
141-
const expected = { foo: 'bar' };
142-
expect(actual).toMatchObject(expected);
117+
return reactTreeWalker(tree, visitor).then(() => {
118+
const expected = { foo: 'bar' };
119+
expect(actual).toMatchObject(expected);
120+
});
143121
});
144122

145123
it('getChildContext', () => {
@@ -163,9 +141,9 @@ describe('reactTreeWalker', () => {
163141
const tree = <Baz><Qux /></Baz>;
164142
// eslint-disable-next-line no-unused-vars
165143
const visitor = (element, instance, context) => undefined;
166-
reactTreeWalker(tree, visitor);
167-
168-
const expected = { foo: 'bar' };
169-
expect(actual).toMatchObject(expected);
144+
return reactTreeWalker(tree, visitor).then(() => {
145+
const expected = { foo: 'bar' };
146+
expect(actual).toMatchObject(expected);
147+
});
170148
});
171149
});

0 commit comments

Comments
 (0)