From b1b6d5b070697c2ef1078cd2fbe0cacf236791aa Mon Sep 17 00:00:00 2001 From: Chen-I Lim Date: Mon, 16 Nov 2020 17:00:10 -0800 Subject: [PATCH] Unit test: grouped undo/redo --- .vscode/launch.json | 4 -- webapp/src/undoManager.test.ts | 82 +++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index b71485f81..767f843f8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -29,8 +29,6 @@ "cwd": "${workspaceRoot}/webapp", "args": [ "--verbose", - "-i", - "--no-cache" ], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" @@ -44,8 +42,6 @@ "args": [ "${fileBasename}", "--verbose", - "-i", - "--no-cache", ], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" diff --git a/webapp/src/undoManager.test.ts b/webapp/src/undoManager.test.ts index 46bffc394..b01934781 100644 --- a/webapp/src/undoManager.test.ts +++ b/webapp/src/undoManager.test.ts @@ -5,8 +5,8 @@ import undoManager from './undomanager' import {Utils} from './utils' test('Basic undo/redo', async () => { - expect(!undoManager.canUndo).toBe(true) - expect(!undoManager.canRedo).toBe(true) + expect(undoManager.canUndo).toBe(false) + expect(undoManager.canRedo).toBe(false) const values: string[] = [] @@ -44,3 +44,81 @@ test('Basic undo/redo', async () => { expect(undoManager.undoDescription).toBe(undefined) expect(undoManager.redoDescription).toBe(undefined) }) + +test('Grouped undo/redo', async () => { + expect(undoManager.canUndo).toBe(false) + expect(undoManager.canRedo).toBe(false) + + const values: string[] = [] + const groupId = 'the group id' + + await undoManager.perform( + async () => { + values.push('a') + }, + async () => { + values.pop() + }, + 'insert a', + ) + + expect(undoManager.canUndo).toBe(true) + expect(undoManager.canRedo).toBe(false) + expect(Utils.arraysEqual(values, ['a'])).toBe(true) + expect(undoManager.undoDescription).toBe('insert a') + expect(undoManager.redoDescription).toBe(undefined) + + await undoManager.perform( + async () => { + values.push('b') + }, + async () => { + values.pop() + }, + 'insert b', + groupId, + ) + + expect(undoManager.canUndo).toBe(true) + expect(undoManager.canRedo).toBe(false) + expect(Utils.arraysEqual(values, ['a', 'b'])).toBe(true) + expect(undoManager.undoDescription).toBe('insert b') + expect(undoManager.redoDescription).toBe(undefined) + + await undoManager.perform( + async () => { + values.push('c') + }, + async () => { + values.pop() + }, + 'insert c', + groupId, + ) + + expect(undoManager.canUndo).toBe(true) + expect(undoManager.canRedo).toBe(false) + expect(Utils.arraysEqual(values, ['a', 'b', 'c'])).toBe(true) + expect(undoManager.undoDescription).toBe('insert c') + expect(undoManager.redoDescription).toBe(undefined) + + await undoManager.undo() + expect(undoManager.canUndo).toBe(true) + expect(undoManager.canRedo).toBe(true) + expect(Utils.arraysEqual(values, ['a'])).toBe(true) + expect(undoManager.undoDescription).toBe('insert a') + expect(undoManager.redoDescription).toBe('insert b') + + await undoManager.redo() + expect(undoManager.canUndo).toBe(true) + expect(undoManager.canRedo).toBe(false) + expect(Utils.arraysEqual(values, ['a', 'b', 'c'])).toBe(true) + expect(undoManager.undoDescription).toBe('insert c') + expect(undoManager.redoDescription).toBe(undefined) + + await undoManager.clear() + expect(undoManager.canUndo).toBe(false) + expect(undoManager.canRedo).toBe(false) + expect(undoManager.undoDescription).toBe(undefined) + expect(undoManager.redoDescription).toBe(undefined) +})