From a67863c5a255b12503c5018a321e6d3c227ed90b Mon Sep 17 00:00:00 2001 From: Miguel Alatzar Date: Mon, 22 Apr 2019 19:17:03 -0700 Subject: [PATCH] [MM-14669] Return null dimensions when height or width is falsy (#2681) * Return 50x50 dimensions when height or width is 0 * Refactor image constants * Return null dimensions when height or width is falsy --- app/constants/image.js | 5 +++++ app/utils/images.js | 14 ++++++++++++-- app/utils/images.test.js | 24 ++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 app/constants/image.js diff --git a/app/constants/image.js b/app/constants/image.js new file mode 100644 index 000000000..e5e61c557 --- /dev/null +++ b/app/constants/image.js @@ -0,0 +1,5 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +export const IMAGE_MAX_HEIGHT = 350; +export const IMAGE_MIN_DIMENSION = 50; diff --git a/app/utils/images.js b/app/utils/images.js index 070fb6397..7a6ea019a 100644 --- a/app/utils/images.js +++ b/app/utils/images.js @@ -1,11 +1,21 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import { + IMAGE_MAX_HEIGHT, + IMAGE_MIN_DIMENSION, +} from 'app/constants/image'; + let previewComponents; -const IMAGE_MAX_HEIGHT = 350; -const IMAGE_MIN_DIMENSION = 50; export const calculateDimensions = (height, width, viewPortWidth = 0, viewPortHeight = 0) => { + if (!height || !width) { + return { + height: null, + width: null, + }; + } + const ratio = height / width; const heightRatio = width / height; diff --git a/app/utils/images.test.js b/app/utils/images.test.js index 554018c9c..d0866603d 100644 --- a/app/utils/images.test.js +++ b/app/utils/images.test.js @@ -2,12 +2,32 @@ // See LICENSE.txt for license information. import {calculateDimensions} from 'app/utils/images'; +import { + IMAGE_MAX_HEIGHT, + IMAGE_MIN_DIMENSION, +} from 'app/constants/image'; const PORTRAIT_VIEWPORT = 315; -const IMAGE_MAX_HEIGHT = 350; -const IMAGE_MIN_DIMENSION = 50; describe('Images calculateDimensions', () => { + it('image with falsy height should return null height and width', () => { + const falsyHeights = [0, null, undefined, NaN, '', false]; + falsyHeights.forEach((falsyHeight) => { + const {height, width} = calculateDimensions(falsyHeight, 20, PORTRAIT_VIEWPORT); + expect(height).toEqual(null); + expect(width).toEqual(null); + }); + }); + + it('image with falsy width should return null height and width', () => { + const falsyWidths = [0, null, undefined, NaN, '', false]; + falsyWidths.forEach((falsyWidth) => { + const {height, width} = calculateDimensions(20, falsyWidth, PORTRAIT_VIEWPORT); + expect(height).toEqual(null); + expect(width).toEqual(null); + }); + }); + it('image smaller than 50x50 should return 50x50', () => { const {height, width} = calculateDimensions(20, 20, PORTRAIT_VIEWPORT); expect(height).toEqual(IMAGE_MIN_DIMENSION);