Skip to content

Commit 14890a8

Browse files
committed
fix(bug): Fixes an issue where props pass through was not propagating.
The SizeMe HOC had a shouldComponentUpdate implementation that should not have been there. This basically stopped any new props from being passed down into the wrapped component. Tests have been added for this case. closes #1
1 parent 67705d4 commit 14890a8

File tree

2 files changed

+91
-92
lines changed

2 files changed

+91
-92
lines changed

src/SizeMe.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ function SizeMe(config = defaultConfig) {
125125
this.handleDOMNode();
126126
}
127127

128-
shouldComponentUpdate(nextProps, nextState) {
129-
return this.hasSizeChanged(this.state, nextState);
130-
}
131-
132128
componentDidUpdate() {
133129
this.handleDOMNode();
134130
}

test/SizeMe.test.js

Lines changed: 91 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const html = `
2424
describeWithDOM(`Given the SizeMe library`, () => {
2525
let SizeMe;
2626
let resizeDetectorMock;
27+
const placeholderHtml = `<div style="width: 100%; height: 100%; position: relative;"></div>`;
2728

2829
beforeEach(() => {
2930
SizeMe = require(`../src/index.js`).default;
@@ -65,12 +66,8 @@ describeWithDOM(`Given the SizeMe library`, () => {
6566
describe(`And the resize detector`, () => {
6667
describe(`When mounting and unmounting the placeholder component`, () => {
6768
it(`Then the resizeDetector registration and deregistration should be called`, () => {
68-
const refreshRate = 20000;
69-
70-
const SizeAwareComponent = SizeMe({ refreshRate })(
71-
function ({ size: { width, height } }) {
72-
return <div>{width} x {height}</div>;
73-
}
69+
const SizeAwareComponent = SizeMe()(
70+
({ size: { width, height } }) => <div>{width} x {height}</div>
7471
);
7572

7673
const mounted = mount(<SizeAwareComponent />);
@@ -89,10 +86,8 @@ describeWithDOM(`Given the SizeMe library`, () => {
8986
it(`Then the resizeDetector registration and deregistration should be called`, (done) => {
9087
const refreshRate = 30;
9188

92-
const SizeAwareComponent = SizeMe({ refreshRate })(
93-
function ({ size: { width, height } }) {
94-
return <div>{width} x {height}</div>;
95-
}
89+
const SizeAwareComponent = SizeMe()(
90+
({ size: { width, height } }) => <div>{width} x {height}</div>
9691
);
9792

9893
const mounted = mount(<SizeAwareComponent />);
@@ -112,6 +107,7 @@ describeWithDOM(`Given the SizeMe library`, () => {
112107
// Wait for the render callback
113108
setTimeout(() => {
114109
// Our actual component should have mounted now
110+
expect(mounted.text()).to.equal(`100 x 100`);
115111
expect(resizeDetectorMock.listenTo.callCount).to.equal(2);
116112
expect(resizeDetectorMock.removeAllListeners.callCount).to.equal(1);
117113

@@ -132,43 +128,47 @@ describeWithDOM(`Given the SizeMe library`, () => {
132128
it(`Then the resizeDetector should be called appropriately`, (done) => {
133129
const refreshRate = 16;
134130

135-
const SizeAwareComponent = SizeMe({ refreshRate })(
136-
function ({ size: { width, height } }) {
137-
return <div>{width} x {height}</div>;
138-
}
131+
const SizeAwareComponent = SizeMe()(
132+
({ size: { width, height } }) => <div>{width} x {height}</div>
139133
);
140134

141-
mount(<SizeAwareComponent />);
135+
const mounted = mount(<SizeAwareComponent />);
142136

143-
// Wait for the render callback
137+
// The placeholder is mounted.
138+
expect(resizeDetectorMock.listenTo.callCount).to.equal(1);
139+
expect(resizeDetectorMock.removeAllListeners.callCount).to.equal(0);
140+
141+
// Get the callback for size changes
142+
const checkIfSizeChangedCallback = resizeDetectorMock.listenTo.args[0][1];
143+
checkIfSizeChangedCallback({
144+
getBoundingClientRect: () => ({ width: 100, height: 100 })
145+
});
146+
147+
// Ok we fired a fake dom update, now we need to wait for it to
148+
// be processed.
144149
setTimeout(() => {
145-
// Our actual component should have mounted now
146-
expect(resizeDetectorMock.listenTo.callCount).to.equal(1);
147-
expect(resizeDetectorMock.removeAllListeners.callCount).to.equal(0);
150+
// Our actual component should be mounted.
151+
expect(mounted.text()).to.equal(`100 x 100`);
152+
expect(resizeDetectorMock.listenTo.callCount).to.equal(2);
153+
expect(resizeDetectorMock.removeAllListeners.callCount).to.equal(1);
148154

149-
// Get the callback for size changes
150-
const checkIfSizeChangedCallback = resizeDetectorMock.listenTo.args[0][1];
155+
// Fire another size change.
151156
checkIfSizeChangedCallback({
152-
getBoundingClientRect: () => ({
153-
width: 100,
154-
height: 100
155-
})
157+
getBoundingClientRect: () => ({ width: 200, height: 200 })
156158
});
157159

158-
// Ok we fired a fake dom update, now we need to wait for it to
159-
// be processed.
160160
setTimeout(() => {
161-
expect(resizeDetectorMock.listenTo.callCount).to.equal(2);
162-
expect(resizeDetectorMock.removeAllListeners.callCount).to.equal(1);
161+
expect(mounted.text()).to.equal(`200 x 200`);
162+
expect(resizeDetectorMock.listenTo.callCount).to.equal(3);
163+
expect(resizeDetectorMock.removeAllListeners.callCount).to.equal(2);
163164

165+
// Fire another size change, but with no change in size.
164166
checkIfSizeChangedCallback({
165-
getBoundingClientRect: () => ({
166-
width: 200,
167-
height: 100
168-
})
167+
getBoundingClientRect: () => ({ width: 200, height: 200 })
169168
});
170169

171170
setTimeout(() => {
171+
expect(mounted.text()).to.equal(`200 x 200`);
172172
expect(resizeDetectorMock.listenTo.callCount).to.equal(3);
173173
expect(resizeDetectorMock.removeAllListeners.callCount).to.equal(2);
174174

@@ -184,93 +184,67 @@ describeWithDOM(`Given the SizeMe library`, () => {
184184
describe(`And no className or style has been provided`, () => {
185185
it(`Then it should render the default placeholder`, () => {
186186
const SizeAwareComponent = SizeMe()(
187-
function ({ size: { width, height } }) {
188-
return <div>{width} x {height}</div>;
189-
}
187+
({ size: { width, height } }) => <div>{width} x {height}</div>
190188
);
191189

192190
const mounted = mount(<SizeAwareComponent />);
193191

194-
const actual = mounted.html();
195-
const expected = `<div style="width: 100%; height: 100%; position: relative;"></div>`;
196-
197-
expect(actual).to.equal(expected);
192+
expect(mounted.html())
193+
.to.equal(placeholderHtml);
198194
});
199195
});
200196

201197
describe(`And only a className has been provided`, () => {
202198
it(`Then it should render a placeholder with the className`, () => {
203199
const SizeAwareComponent = SizeMe()(
204-
function ({ size: { width, height } }) {
205-
return <div>{width} x {height}</div>;
206-
}
200+
({ size: { width, height } }) => <div>{width} x {height}</div>
207201
);
208202

209203
const mounted = mount(<SizeAwareComponent className={`foo`} />);
210204

211-
const actual = mounted.html();
212-
const expected = `<div class="foo"></div>`;
213-
214-
expect(actual).to.equal(expected);
205+
expect(mounted.html())
206+
.to.equal(`<div class="foo"></div>`);
215207
});
216208
});
217209

218210
describe(`And only a style has been provided`, () => {
219211
it(`Then it should render a placeholder with the style`, () => {
220212
const SizeAwareComponent = SizeMe()(
221-
function ({ size: { width, height } }) {
222-
return <div>{width} x {height}</div>;
223-
}
213+
({ size: { width, height } }) => <div>{width} x {height}</div>
224214
);
225215

226216
const mounted = mount(<SizeAwareComponent style={{ height: `20px` }} />);
227217

228-
const actual = mounted.html();
229-
const expected = `<div style="height: 20px;"></div>`;
230-
231-
expect(actual).to.equal(expected);
218+
expect(mounted.html())
219+
.to.equal(`<div style="height: 20px;"></div>`);
232220
});
233221
});
234222

235223
describe(`And a className and style have been provided`, () => {
236224
it(`Then it should render a placeholder with both`, () => {
237225
const SizeAwareComponent = SizeMe()(
238-
function ({ size: { width, height } }) {
239-
return <div>{width} x {height}</div>;
240-
}
226+
({ size: { width, height } }) => <div>{width} x {height}</div>
241227
);
242228

243229
const mounted = mount(
244-
<SizeAwareComponent
245-
style={{ height: `20px` }}
246-
className={`foo`}
247-
/>
230+
<SizeAwareComponent style={{ height: `20px` }} className={`foo`} />
248231
);
249232

250-
const actual = mounted.html();
251-
const expected = `<div class="foo" style="height: 20px;"></div>`;
252-
253-
expect(actual).to.equal(expected);
233+
expect(mounted.html())
234+
.to.equal(`<div class="foo" style="height: 20px;"></div>`);
254235
});
255236
});
256237

257238
describe(`And the size event has occurred`, () => {
258239
it(`Then the actual size aware component should render`, (done) => {
259240
const refreshRate = 30;
260241

261-
const SizeAwareComponent = SizeMe({
262-
refreshRate
263-
})(
264-
function ({ size: { width, height } }) {
265-
return <div>{width} x {height}</div>;
266-
}
242+
const SizeAwareComponent = SizeMe({ refreshRate })(
243+
({ size: { width, height } }) => <div>{width} x {height}</div>
267244
);
268245

269246
const mounted = mount(<SizeAwareComponent />);
270247

271-
const placeholderHtml =
272-
`<div style="width: 100%; height: 100%; position: relative;"></div>`;
273-
274248
// Initial render should be as expected.
275249
expect(mounted.html()).to.equal(placeholderHtml);
276250

@@ -279,25 +253,54 @@ describeWithDOM(`Given the SizeMe library`, () => {
279253
expect(mounted.html()).to.equal(placeholderHtml);
280254
}, refreshRate - 5);
281255

256+
// Get the callback for size changes.
257+
const checkIfSizeChangedCallback = resizeDetectorMock.listenTo.args[0][1];
258+
checkIfSizeChangedCallback({
259+
getBoundingClientRect: () => ({ width: 100, height: 100 })
260+
});
261+
282262
// Wait till refresh rate gets hit
283263
setTimeout(() => {
284-
// Get the callback for size changes.
285-
const checkIfSizeChangedCallback = resizeDetectorMock.listenTo.args[0][1];
286-
checkIfSizeChangedCallback({
287-
getBoundingClientRect: () => ({
288-
width: 100,
289-
height: 100
290-
})
291-
});
264+
// Update should have occurred by now.
265+
expect(mounted.text()).to.equal(`100 x 100`);
292266

293-
setTimeout(() => {
294-
// Update should have occurred by now.
295-
expect(mounted.text()).to.equal(`100 x 100`);
296-
297-
done();
298-
}, refreshRate);
267+
done();
299268
}, refreshRate + 5);
300269
});
301270
});
271+
272+
describe(`And it receives new non-size props`, () => {
273+
it(`Then the new props should be passed into the component`, (done) => {
274+
const refreshRate = 16;
275+
276+
const SizeAwareComponent = SizeMe({ refreshRate })(
277+
function ({ size: { width, height }, otherProp }) {
278+
return <div>{width} x {height} & {otherProp}</div>;
279+
}
280+
);
281+
282+
const mounted = mount(<SizeAwareComponent otherProp="foo" />);
283+
284+
// Get the callback for size changes.
285+
const checkIfSizeChangedCallback = resizeDetectorMock.listenTo.args[0][1];
286+
checkIfSizeChangedCallback({
287+
getBoundingClientRect: () => ({ width: 100, height: 100 })
288+
});
289+
290+
// Wait for refresh to hit.
291+
setTimeout(() => {
292+
// Output should contain foo.
293+
expect(mounted.text()).to.equal(`100 x 100 & foo`);
294+
295+
// Update the other prop.
296+
mounted.setProps({ otherProp: `bar` });
297+
298+
// Output should contain foo.
299+
expect(mounted.text()).to.equal(`100 x 100 & bar`);
300+
301+
done();
302+
}, refreshRate + 10);
303+
});
304+
});
302305
});
303306
}, html);

0 commit comments

Comments
 (0)