Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#65 unit tests to zindex #88

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 255 additions & 0 deletions src/core/providers/canvas/zindex.util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
import { ShapeModel } from '@/core/model';
import {
moveZIndexDownOneLevel,
moveZIndexToBottom,
moveZIndexTopOneLevel,
moveZIndexToTop,
} from './zindex.util';

describe('moveZIndexToTop', () => {
it('should move the shape to the end of the array', () => {
// Arrange
const selectedShapeId: string = '2';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexToTop(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual<ShapeModel[]>([
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
]);
});

it('should return the same array if the shape is already at the end', () => {
// Arrange
const selectedShapeId: string = '2';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
];
// Act
const result = moveZIndexToTop(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual(shapes);
});

it('should return an empty array if there is no shapes created', () => {
// Arrange
const selectedShapeId: string = '2';
const shapes: ShapeModel[] = [];
// Act
const result = moveZIndexToTop(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual<ShapeModel[]>([]);
});

it('should return the same array if the shape is not found', () => {
// Arrange
const selectedShapeId: string = '5';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexToTop(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual(shapes);
});
});

describe('moveZIndexToBottom', () => {
it('should move the shape to the head of the array', () => {
// Arrange
const selectedShapeId: string = '3';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexToBottom(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual<ShapeModel[]>([
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
]);
});

it('should return the same array if the shape is already at the head', () => {
// Arrange
const selectedShapeId: string = '2';
const shapes: ShapeModel[] = [
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexToBottom(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual(shapes);
});

it('should return an empty array if there is no shapes created', () => {
// Arrange
const selectedShapeId: string = '2';
const shapes: ShapeModel[] = [];
// Act
const result = moveZIndexToBottom(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual<ShapeModel[]>([]);
});

it('should return the same array if the shape is not found', () => {
// Arrange
const selectedShapeId: string = '5';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexToBottom(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual(shapes);
});
});

describe('moveZIndexDownOneLevel', () => {
it('should move the shape one level towards the start of the array', () => {
// Arrange
const selectedShapeId: string = '3';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexDownOneLevel(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual<ShapeModel[]>([
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
]);
});

it('should return the same array if the shape is already at the start', () => {
// Arrange
const selectedShapeId: string = '1';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexDownOneLevel(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual(shapes);
});

it('should return an empty array if there is no shapes created', () => {
// Arrange
const selectedShapeId: string = '2';
const shapes: ShapeModel[] = [];
// Act
const result = moveZIndexDownOneLevel(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual<ShapeModel[]>([]);
});

it('should return the same array if the shape is not found', () => {
// Arrange
const selectedShapeId: string = '5';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexDownOneLevel(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual(shapes);
});
});

describe('moveZIndexTopOneLevel', () => {
it('should move the shape one level towards the end of the array', () => {
// Arrange
const selectedShapeId: string = '2';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexTopOneLevel(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual<ShapeModel[]>([
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
]);
});

it('should return the same array if the shape is already at the end', () => {
// Arrange
const selectedShapeId: string = '4';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexTopOneLevel(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual(shapes);
});

it('should return an empty array if there is no shapes created', () => {
// Arrange
const selectedShapeId: string = '2';
const shapes: ShapeModel[] = [];
// Act
const result = moveZIndexTopOneLevel(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual<ShapeModel[]>([]);
});

it('should return the same array if the shape is not found', () => {
// Arrange
const selectedShapeId: string = '5';
const shapes: ShapeModel[] = [
{ id: '1', x: 0, y: 0, width: 0, height: 0, type: 'button' },
{ id: '2', x: 0, y: 0, width: 0, height: 0, type: 'input' },
{ id: '3', x: 0, y: 0, width: 0, height: 0, type: 'combobox' },
{ id: '4', x: 0, y: 0, width: 0, height: 0, type: 'checkbox' },
];
// Act
const result = moveZIndexTopOneLevel(selectedShapeId, shapes);
// Assert
expect(result).toStrictEqual(shapes);
});
});
8 changes: 4 additions & 4 deletions src/core/providers/canvas/zindex.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ShapeModel } from '@/core/model';
import { ZIndexAction } from './canvas.model';

// TOO Add Unit tests to all these methods: #65
const moveZIndexToTop = (
export const moveZIndexToTop = (
selectedShapeId: string,
shapeCollection: ShapeModel[]
): ShapeModel[] => {
Expand All @@ -17,7 +17,7 @@ const moveZIndexToTop = (
: shapeCollection;
};

const moveZIndexToBottom = (
export const moveZIndexToBottom = (
selectedShapeId: string,
shapeCollection: ShapeModel[]
): ShapeModel[] => {
Expand All @@ -32,7 +32,7 @@ const moveZIndexToBottom = (
: shapeCollection;
};

const moveZIndexDownOneLevel = (
export const moveZIndexDownOneLevel = (
selectedShapeId: string,
shapeCollection: ShapeModel[]
): ShapeModel[] => {
Expand All @@ -55,7 +55,7 @@ const moveZIndexDownOneLevel = (
: shapeCollection;
};

const moveZIndexTopOneLevel = (
export const moveZIndexTopOneLevel = (
selectedShapeId: string,
shapeCollection: ShapeModel[]
): ShapeModel[] => {
Expand Down
Loading