[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
This commit is contained in:
Miguel Alatzar 2019-04-22 19:17:03 -07:00 committed by GitHub
parent 10196d5fad
commit a67863c5a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 4 deletions

5
app/constants/image.js Normal file
View file

@ -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;

View file

@ -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;

View file

@ -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);