Skip to content

Commit

Permalink
test: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidvdn committed Oct 9, 2024
1 parent a6ef179 commit a7f75b7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/command/bad-practice-editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ describe('BadPracticeEditor', () => {
editor.undo();
expect(editor.getContent()).toBe("Hello World");
})

it('should do nothing if there is nothing to undo', () => {
const editor = new TextEditor();
editor.setContent("Hello World");
editor.undo();
expect(editor.getContent()).toBe("Hello World");
})

it('should make the text italic', () => {
const editor = new TextEditor();
editor.setContent("Hello World");
editor.italic(6, 5);
// add implementation here
})
});
2 changes: 1 addition & 1 deletion app/command/bad-practice-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TextEditor {
this.content = this.content.slice(0, start) + boldedText + this.content.slice(start + length);
}

public italic() { /* ... */ }
public italic(start: number, length: number) { /* ... */ }

// Undo the last operation (revert to the previous state)
public undo(): void {
Expand Down
16 changes: 15 additions & 1 deletion app/command/command-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { ICommand } from "./interface";
describe('Command Manager', () => {
const writeCommand: ICommand = {
execute: jest.fn(),
undo: jest.fn()
undo: jest.fn(),
};

afterEach(() => {
jest.clearAllMocks();
});

it('should execute command', () => {
const commandManager = new CommandManager();
commandManager.execute(writeCommand);
Expand All @@ -20,4 +24,14 @@ describe('Command Manager', () => {
expect(writeCommand.execute).toHaveBeenCalled();
expect(writeCommand.undo).toHaveBeenCalled();
})

it('should redo command', () => {
const commandManager = new CommandManager();
commandManager.execute(writeCommand);
commandManager.undo();
commandManager.redo();
expect(writeCommand.execute).toHaveBeenCalledTimes(2);
expect(writeCommand.undo).toHaveBeenCalled();

})
});

0 comments on commit a7f75b7

Please sign in to comment.