Skip to content

Commit

Permalink
Add unit tests for fitSizeToShapeSizeRestrictions function
Browse files Browse the repository at this point in the history
  • Loading branch information
LourdesRsdp committed Jul 31, 2024
1 parent e1a92d2 commit 6b2781d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/common/utils/shapes/shape-restrictions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { fitSizeToShapeSizeRestrictions } from './shape-restrictions';
import { ShapeSizeRestrictions } from './shape-restrictions';

//Mock data
const restrictions: ShapeSizeRestrictions = {
minWidth: 50,
minHeight: 30,
maxWidth: 500,
maxHeight: 300,
defaultWidth: 100,
defaultHeight: 100,
};
const restrictions2: ShapeSizeRestrictions = {
minWidth: 50,
minHeight: 30,
maxWidth: -1,
maxHeight: -1,
defaultWidth: 100,
defaultHeight: 100,
};

describe('./pods/canvas/canvas.util', () => {
it('should set width to minimum width if width is 0', () => {
// Arrange
const width = 0;
const height = 50;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 50, height: 50 });
});
it('should set width to minimum width if width is 1', () => {
// Arrange
const width = 1;
const height = 50;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 50, height: 50 });
});
it('should return width within bounds if width is within the minimum and maximum limits', () => {
// Arrange
const width = 300;
const height = 150;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 300, height: 150 });
});
it('should return max width if width is above max bound', () => {
// Arrange
const width = 950;
const height = 150;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 500, height: 150 });
});
it('should return minWidth if width is under min bound', () => {
// Arrange
const width = 30;
const height = 150;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 50, height: 150 });
});
it('should return maxHeight if height is above max bound', () => {
// Arrange
const width = 80;
const height = 500;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 80, height: 300 });
});
it('should return minHeight if height is under min bound', () => {
// Arrange
const width = 80;
const height = 15;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 80, height: 30 });
});
it('should return maxWidth and maxHeight if both are above max bound', () => {
// Arrange
const width = 800;
const height = 500;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 500, height: 300 });
});
it('should return minWidth and minHeight if both are under min bound', () => {
// Arrange
const width = 30;
const height = 10;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 50, height: 30 });
});
it('should return width and height as is when maxWidth and maxHeight are -1', () => {
// Arrange
const width = 400;
const height = 250;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions2, width, height);

//Assert
expect(result).toEqual({ width: 400, height: 250 });
});
});
2 changes: 2 additions & 0 deletions src/common/utils/shapes/shape-restrictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export const fitSizeToShapeSizeRestrictions = (
height: Math.max(newHeight, shapeSizeRestrictions.minHeight),
};
};

export type { ShapeSizeRestrictions };

0 comments on commit 6b2781d

Please sign in to comment.