Skip to content

Commit

Permalink
Merge pull request #309 from migalko94/feature/#126-Add-Unit-test-sup…
Browse files Browse the repository at this point in the history
…port-to-history-manager.business.ts

add tests to  can undo can redo perform undo perform redo
  • Loading branch information
brauliodiez authored Feb 14, 2024
2 parents ef4cfaf + fd0b0ad commit 82b4f31
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions src/common/undo-redo/history-manager.business.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import {
canUndo,
canRedo,
performUndo,
performRedo,
} from './history-manager.business';

describe('common/undo-redo/history-manager.business', () => {
describe('canUndo', () => {
it('should return true when current index is higher than zero', () => {
// Arrange
const currentIndex: number = 1;
const expectedResult: boolean = true;
// Act
const result = canUndo(currentIndex);
// Assert
expect(result).toEqual(expectedResult);
});

it('should return false when current index equals zero', () => {
// Arrange
const currentIndex: number = 0;
const expectedResult: boolean = false;
// Act
const result = canUndo(currentIndex);
// Assert
expect(result).toEqual(expectedResult);
});

it('should return false when current index is lower than zero', () => {
// Arrange
const currentIndex: number = -1;
const expectedResult: boolean = false;
// Act
const result = canUndo(currentIndex);
// Assert
expect(result).toEqual(expectedResult);
});
});

describe('canRedo', () => {
it('should return true when current index is lower than history length minus 1', () => {
// Arrange
const currentIndex: number = 1;
const historyLength: number = 3;
const expectedResult: boolean = true;
// Act
const result = canRedo(currentIndex, historyLength);
// Assert
expect(result).toEqual(expectedResult);
});

it('should return false when current index equals history length minus 1', () => {
// Arrange
const currentIndex: number = 1;
const historyLength: number = 2;
const expectedResult: boolean = false;
// Act
const result = canRedo(currentIndex, historyLength);
// Assert
expect(result).toEqual(expectedResult);
});

it('should return false when current index is higher than history length minus 1', () => {
// Arrange
const currentIndex: number = 2;
const historyLength: number = 2;
const expectedResult: boolean = false;
// Act
const result = canRedo(currentIndex, historyLength);
// Assert
expect(result).toEqual(expectedResult);
});
});

describe('performUndo', () => {
it('should return current index minus 1 when canUndo function with current index as parameter returns true', () => {
// Arrange
const currentIndex: number = 1;
const expectedResult: number = 0;
// Act
const result = performUndo(currentIndex);
// Assert
expect(result).toEqual(expectedResult);
});

it('should return current index when canUndo function with current index as parameter returns false', () => {
// Arrange
const currentIndex: number = 0;
const expectedResult: number = 0;
// Act
const result = performUndo(currentIndex);
// Assert
expect(result).toEqual(expectedResult);
});
});

describe('performRedo', () => {
it('should return current index plus 1 when canRedo function with current index and history length as parameters returns true', () => {
// Arrange
const currentIndex = 1;
const historyLength: number = 3;
const expectedResult: number = 2;
// Act
const result = performRedo(currentIndex, historyLength);
// Assert
expect(result).toEqual(expectedResult);
});

it('should return current index when canRedo function with current index and history length as parameters returns false', () => {
// Arrange
const currentIndex = 1;
const historyLength: number = 2;
const expectedResult: number = 1;
// Act
const result = performRedo(currentIndex, historyLength);
// Assert
expect(result).toEqual(expectedResult);
});
});
});

0 comments on commit 82b4f31

Please sign in to comment.