From 82a398487bbacb2901eab2cdb591f8bde8b34ccf Mon Sep 17 00:00:00 2001 From: Chen-I Lim Date: Mon, 16 Nov 2020 16:48:52 -0800 Subject: [PATCH] Unit test: UndoManager --- .vscode/launch.json | 33 ++++++++++++++++++++++-- webapp/src/undoManager.test.ts | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 webapp/src/undoManager.test.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index ddd2a303e..b71485f81 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -20,6 +20,35 @@ "/**" ], "type": "pwa-node" - } - ] + }, + { + "type": "node", + "request": "launch", + "name": "Jest: run all tests", + "program": "${workspaceRoot}/webapp/node_modules/jest/bin/jest.js", + "cwd": "${workspaceRoot}/webapp", + "args": [ + "--verbose", + "-i", + "--no-cache" + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen" + }, + { + "type": "node", + "request": "launch", + "name": "Jest: run current file", + "program": "${workspaceRoot}/webapp/node_modules/jest/bin/jest.js", + "cwd": "${workspaceRoot}/webapp", + "args": [ + "${fileBasename}", + "--verbose", + "-i", + "--no-cache", + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen" + }, + ] } diff --git a/webapp/src/undoManager.test.ts b/webapp/src/undoManager.test.ts new file mode 100644 index 000000000..46bffc394 --- /dev/null +++ b/webapp/src/undoManager.test.ts @@ -0,0 +1,46 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import undoManager from './undomanager' +import {Utils} from './utils' + +test('Basic undo/redo', async () => { + expect(!undoManager.canUndo).toBe(true) + expect(!undoManager.canRedo).toBe(true) + + const values: string[] = [] + + await undoManager.perform( + async () => { + values.push('a') + }, + async () => { + values.pop() + }, + 'test', + ) + + expect(undoManager.canUndo).toBe(true) + expect(undoManager.canRedo).toBe(false) + expect(Utils.arraysEqual(values, ['a'])).toBe(true) + expect(undoManager.undoDescription).toBe('test') + expect(undoManager.redoDescription).toBe(undefined) + + await undoManager.undo() + expect(undoManager.canUndo).toBe(false) + expect(undoManager.canRedo).toBe(true) + expect(Utils.arraysEqual(values, [])).toBe(true) + expect(undoManager.undoDescription).toBe(undefined) + expect(undoManager.redoDescription).toBe('test') + + await undoManager.redo() + expect(undoManager.canUndo).toBe(true) + expect(undoManager.canRedo).toBe(false) + expect(Utils.arraysEqual(values, ['a'])).toBe(true) + + await undoManager.clear() + expect(undoManager.canUndo).toBe(false) + expect(undoManager.canRedo).toBe(false) + expect(undoManager.undoDescription).toBe(undefined) + expect(undoManager.redoDescription).toBe(undefined) +})