Skip to content

Commit 538b46b

Browse files
committed
feat(queries): Expose "props" to the query functions.
The additional props being provided to the wrapped component are now exposed to the query functions as a secondary parameter.
1 parent f29398a commit 538b46b

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/ComponentQueries.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,16 @@ function componentQueries(...params) {
100100
}
101101

102102
componentWillMount() {
103-
this.runQueries(this.props.size);
103+
const { size, ...otherProps } = this.props;
104+
this.runQueries(size, otherProps);
104105
}
105106

106107
componentWillReceiveProps(nextProps) {
107108
const { size } = this.props;
108-
const { size: nextSize } = nextProps;
109+
const { size: nextSize, ...nextOtherProps } = nextProps;
109110

110111
if (!shallowEqual(size, nextSize)) {
111-
this.runQueries(nextSize);
112+
this.runQueries(nextSize, nextOtherProps);
112113
}
113114
}
114115

@@ -127,14 +128,17 @@ function componentQueries(...params) {
127128
|| !shallowEqual(this.state.queryResult, nextState.queryResult);
128129
}
129130

130-
runQueries({ width, height }) {
131+
runQueries({ width, height }, otherProps) {
131132
const queryResult = queries.reduce((acc, cur) =>
132133
mergeWith(
133134
acc,
134-
cur({
135-
width: sizeMeConfig.monitorWidth ? width : null,
136-
height: sizeMeConfig.monitorHeight ? height : null,
137-
}),
135+
cur(
136+
{
137+
width: sizeMeConfig.monitorWidth ? width : null,
138+
height: sizeMeConfig.monitorHeight ? height : null,
139+
},
140+
otherProps
141+
),
138142
mergeWithCustomizer
139143
)
140144
, {});

test/ComponentQueries.test.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,35 @@ describeWithDOM('Given the ComponentQueries library', () => {
207207
const renderSpy = sinonSandbox.spy(instance, 'render');
208208
expect(renderSpy.callCount).equals(0);
209209

210-
// Change the width so that the queries produce a new result.
210+
// Set the props causes a rerender.
211211
mounted.setProps({ size: { width: 150 }, foo: 'bar' });
212212
expect(renderSpy.callCount).equals(1);
213213

214-
// Change the width so that the queries produce the same result.
215-
mounted.setProps({ size: { width: 120 }, foo: 'bar' });
214+
// Set the same props causes a rerender.
215+
mounted.setProps({ size: { width: 150 }, foo: 'bar' });
216216
expect(renderSpy.callCount).equals(2);
217217
});
218218

219+
it('Then it should pass the "other" props to the queries', () => {
220+
let actualProps;
221+
222+
const ComponentQueriedComponent = componentQueries(
223+
(_, props) => { actualProps = props; return {}; }
224+
)(() => <div />);
225+
226+
// Initial mount should call queries.
227+
const expectedMountProps = { foo: 'bar', baz: 1 };
228+
const mounted = mount(
229+
<ComponentQueriedComponent size={{ width: 50 }} {...expectedMountProps} />
230+
);
231+
expect(actualProps).eql(expectedMountProps);
232+
233+
// Update should call queries with updated props.
234+
const expectedUpdateProps = { foo: 'bob', baz: 2 };
235+
mounted.setProps(Object.assign({}, { size: { width: 100 } }, expectedUpdateProps));
236+
expect(actualProps).eql(expectedUpdateProps);
237+
});
238+
219239
it('Then height should be undefined if we are not monitoring height', () => {
220240
let actualHeight;
221241

0 commit comments

Comments
 (0)