From a7f75b788cab4924e5a2dc6c0533d01f553a8ff0 Mon Sep 17 00:00:00 2001 From: Vahid Najafi Date: Wed, 9 Oct 2024 14:17:14 +0200 Subject: [PATCH] test: add more unit tests --- app/command/bad-practice-editor.spec.ts | 14 ++++++++++++++ app/command/bad-practice-editor.ts | 2 +- app/command/command-manager.spec.ts | 16 +++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/command/bad-practice-editor.spec.ts b/app/command/bad-practice-editor.spec.ts index b7a0467..11b5648 100644 --- a/app/command/bad-practice-editor.spec.ts +++ b/app/command/bad-practice-editor.spec.ts @@ -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 + }) }); diff --git a/app/command/bad-practice-editor.ts b/app/command/bad-practice-editor.ts index 3fb7db9..24acc0c 100644 --- a/app/command/bad-practice-editor.ts +++ b/app/command/bad-practice-editor.ts @@ -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 { diff --git a/app/command/command-manager.spec.ts b/app/command/command-manager.spec.ts index 1f2140d..b88771f 100644 --- a/app/command/command-manager.spec.ts +++ b/app/command/command-manager.spec.ts @@ -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); @@ -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(); + + }) });