Unit test: UndoManager

This commit is contained in:
Chen-I Lim 2020-11-16 16:48:52 -08:00
parent 44e5b625a9
commit 82a398487b
2 changed files with 77 additions and 2 deletions

31
.vscode/launch.json vendored
View file

@ -20,6 +20,35 @@
"<node_internals>/**"
],
"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"
},
]
}

View file

@ -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)
})