Skip to content

Commit 9d6d541

Browse files
committed
Support for dataProp
1 parent 5205fbd commit 9d6d541

7 files changed

+80
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import {withQuery} from 'meteor/cultofcoders:grapher-react';
3+
import {postsList} from '../../bootstrap/namedQueries';
4+
import PostWithDataProp from '../dumb/PostWithDataProp';
5+
6+
export default withQuery(() => {
7+
return postsList.clone()
8+
}, {
9+
single: true,
10+
reactive: true,
11+
dataProp: 'post',
12+
})(PostWithDataProp);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import {withQuery} from 'meteor/cultofcoders:grapher-react';
3+
import {postsList} from '../../bootstrap/namedQueries';
4+
import PostWithDataProp from '../dumb/PostWithDataProp';
5+
6+
export default withQuery(() => {
7+
return postsList.clone()
8+
}, {
9+
single: true,
10+
dataProp: 'post',
11+
})(PostWithDataProp);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import Loading from './Loading';
3+
import Error from './Error';
4+
5+
const PostWithDataProp = ({post, isLoading, error}) => {
6+
if (isLoading) {
7+
return <Loading />;
8+
}
9+
10+
if (error) {
11+
return <Error error={error} />;
12+
}
13+
14+
return <div className="title">{post.title}</div>
15+
};
16+
17+
export default PostWithDataProp;

__tests__/main.client.js

100644100755
+35-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import {withQuery} from 'meteor/cultofcoders:grapher-react';
99
import {expect} from 'chai';
1010

1111
import PostItemContainer from './components/containers/PostItem';
12+
import PostItemWithDataPropContainer from './components/containers/PostItemWithDataProp';
13+
import PostItemReactiveWithDataPropContainer from './components/containers/PostItemReactiveWithDataProp';
1214
import PostItemPollingContainer from './components/containers/PostItemPolling';
1315
import PostItemReactiveContainer from './components/containers/PostItemReactive';
1416
import PostItemErrorContainer from './components/containers/PostItemError';
1517
import PostItemReactiveErrorContainer from './components/containers/PostItemReactiveError';
1618
import Post from './components/dumb/Post';
19+
import PostWithDataProp from './components/dumb/PostWithDataProp';
1720
import Loading from './components/dumb/Loading';
1821
import Error from './components/dumb/Error';
1922

@@ -48,6 +51,37 @@ describe('withTracker()', function () {
4851
}, 100)
4952
});
5053

54+
it('[Static] Should work with dataProp', function (done) {
55+
const wrapper = mount(<PostItemWithDataPropContainer />);
56+
let loadingComponent = wrapper.html();
57+
58+
expect(wrapper.find('PostWithDataProp').length).to.equal(1);
59+
expect(wrapper.find('Loading').length).to.equal(1);
60+
61+
setTimeout(function () {
62+
let html = wrapper.html();
63+
expect(html).to.equal('<div class="title">Post 0</div>');
64+
65+
done();
66+
}, 100)
67+
});
68+
69+
it('[Reactive] Should work with dataProp', function (done) {
70+
const wrapper = mount(<PostItemReactiveWithDataPropContainer />);
71+
let loadingComponent = wrapper.html();
72+
73+
// expect(wrapper.find('PostWithDataProp').length).to.equal(1);
74+
// expect(wrapper.find('Loading').length).to.equal(1);
75+
76+
setTimeout(function () {
77+
wrapper.update();
78+
79+
let html = wrapper.html();
80+
expect(html).to.equal('<div class="title">Post 0</div>');
81+
82+
done();
83+
}, 100)
84+
});
5185

5286
it('[Reactive] Should load the date after mounting', function (done) {
5387
const wrapper = mount(<PostItemReactiveContainer />);
@@ -118,4 +152,4 @@ describe('withTracker()', function () {
118152
}, 200);
119153
}, 100);
120154
});
121-
});
155+
});

defaults.js

100644100755
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export default {
22
reactive: false,
33
single: false,
4-
}
4+
dataProp: 'data',
5+
}

lib/checkOptions.js

100644100755
+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ export default function (options) {
88
pollingMs: Match.Maybe(Number),
99
errorComponent: Match.Maybe(React.Component),
1010
loadingComponent: Match.Maybe(React.Component),
11+
dataProp: Match.Maybe(String),
1112
});
1213

1314
if (options.reactive && options.poll) {
1415
throw new Meteor.Error(`You cannot have a query that is reactive and it is with polling`)
1516
}
16-
}
17+
}

lib/withQueryContainer.js

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function withQueryContainer(WrappedComponent) {
3939
...props,
4040
isLoading: error ? false : isLoading,
4141
error,
42-
data: config.single ? data[0] : data,
42+
[config.dataProp]: config.single ? data[0] : data,
4343
query
4444
})
4545
};

0 commit comments

Comments
 (0)