Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit a4f6c2e

Browse files
committed
Replace deprecated/unsafe componentWillReceiveProps method with componentDidUpdate and update test to check source gets updated
1 parent 15bf6fb commit a4f6c2e

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

src/imageCacheHoc.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ export default function imageCacheHoc(Image, options = {}) {
191191
* Enables caching logic to work if component source prop is updated (that is, the image url changes without mounting a new component).
192192
* See: https://github.com/billmalarky/react-native-image-cache-hoc/pull/15
193193
*
194-
* @param nextProps {Object} - Props that will be passed to component.
194+
* @param prevProps {Object} - Previous props.
195195
*/
196-
async componentWillReceiveProps(nextProps) {
196+
async componentDidUpdate(prevProps) {
197197
// Set urls from source prop data
198-
const url = traverse(this.props).get(['source', 'uri']);
199-
const nextUrl = traverse(nextProps).get(['source', 'uri']);
198+
const url = traverse(prevProps).get(['source', 'uri']);
199+
const nextUrl = traverse(this.props).get(['source', 'uri']);
200200

201201
// Do nothing if url has not changed.
202202
if (url === nextUrl) return;

tests/CacheableImage.test.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -328,23 +328,39 @@ describe('CacheableImage', function () {
328328
console.warn = consoleWarnCache; // eslint-disable-line no-console
329329
});
330330

331-
it('componentWillReceiveProps should not throw any uncaught errors.', () => {
331+
it('componentDidUpdate should not throw any uncaught errors.', (done) => {
332+
RNFetchBlob.fetch
333+
.mockResolvedValueOnce({
334+
path: () => {
335+
return 'A.jpg';
336+
},
337+
})
338+
.mockResolvedValueOnce({
339+
path: () => {
340+
return 'B.jpg';
341+
},
342+
});
343+
332344
const CacheableImage = imageCacheHoc(Image);
333345

334-
const cacheableImage = new CacheableImage({
335-
// eslint-disable-line no-unused-vars
336-
source: {
337-
uri:
338-
'https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png',
339-
},
340-
});
346+
const wrapper = shallow(
347+
<CacheableImage {...mockData.mockCacheableImageProps} />
348+
);
341349

342-
cacheableImage.componentWillReceiveProps({
343-
// eslint-disable-line no-unused-vars
344-
source: {
345-
uri:
346-
'https://img.wennermedia.com/wallpaper1-39bd413b-cb85-4af0-9f33-3507f272e562.jpg',
347-
},
350+
setImmediate(() => {
351+
expect(wrapper.prop('source')).toStrictEqual({
352+
uri: 'A.jpg',
353+
});
354+
355+
wrapper.setProps({ source: { uri: 'https://example.com/B.jpg' } });
356+
357+
setImmediate(() => {
358+
expect(wrapper.prop('source')).toStrictEqual({
359+
uri: 'B.jpg',
360+
});
361+
362+
done();
363+
});
348364
});
349365
});
350366

0 commit comments

Comments
 (0)