|
| 1 | +import { PixelRatio } from 'react-native'; |
| 2 | + |
| 3 | +import { getResizedImageUrl } from '../getResizedImageUrl'; |
| 4 | + |
| 5 | +describe('getResizedImageUrl', () => { |
| 6 | + it('should return same url if its not cloudfront cdn', () => { |
| 7 | + const testUrl = 'http://foo.com/blah_(wikipedia)#cite-1'; |
| 8 | + |
| 9 | + const resizedUrl = getResizedImageUrl({ height: 100, url: testUrl, width: 100 }); |
| 10 | + expect(resizedUrl).toEqual(testUrl); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should append given height, width and resize mode to url', () => { |
| 14 | + const testUrl = 'http://us-east.stream-io-cdn.com/blah'; |
| 15 | + |
| 16 | + const testConfigs = [ |
| 17 | + { height: 100, resize: 'scale', width: 100 }, |
| 18 | + { height: 200, resize: 'fill', width: 300 }, |
| 19 | + { height: 1000, resize: 'clip', width: 10 }, |
| 20 | + ]; |
| 21 | + |
| 22 | + testConfigs.forEach(({ height, resize, width }) => { |
| 23 | + const resizedUrl = getResizedImageUrl({ height, resize, url: testUrl, width }); |
| 24 | + const parsedUrl = new URL(resizedUrl); |
| 25 | + expect(parsedUrl.searchParams.get('h')).toEqual( |
| 26 | + `${PixelRatio.getPixelSizeForLayoutSize(height)}`, |
| 27 | + ); |
| 28 | + expect(parsedUrl.searchParams.get('w')).toEqual( |
| 29 | + `${PixelRatio.getPixelSizeForLayoutSize(width)}`, |
| 30 | + ); |
| 31 | + expect(parsedUrl.searchParams.get('resize')).toEqual(resize); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should replace wildcards with given height, width and resize mode within url', () => { |
| 36 | + const testUrl = 'http://us-east.stream-io-cdn.com/blah?h=*&w=*&resize=*'; |
| 37 | + |
| 38 | + const testConfigs = [ |
| 39 | + { height: 100, resize: 'scale', width: 100 }, |
| 40 | + { height: 200, resize: 'fill', width: 300 }, |
| 41 | + { height: 1000, resize: 'clip', width: 10 }, |
| 42 | + ]; |
| 43 | + |
| 44 | + testConfigs.forEach(({ height, resize, width }) => { |
| 45 | + const resizedUrl = getResizedImageUrl({ height, resize, url: testUrl, width }); |
| 46 | + const parsedUrl = new URL(resizedUrl); |
| 47 | + expect(parsedUrl.searchParams.get('h')).toEqual( |
| 48 | + `${PixelRatio.getPixelSizeForLayoutSize(height)}`, |
| 49 | + ); |
| 50 | + expect(parsedUrl.searchParams.get('w')).toEqual( |
| 51 | + `${PixelRatio.getPixelSizeForLayoutSize(width)}`, |
| 52 | + ); |
| 53 | + expect(parsedUrl.searchParams.get('resize')).toEqual(resize); |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments