summaryrefslogtreecommitdiff
path: root/test/09-test-undo.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-08-13 17:56:31 -0400
committerJules Laplace <jules@okfoc.us>2014-08-13 17:56:31 -0400
commitc4c45b64c2c0fc109f4c21effe7f73f5c46a1ae9 (patch)
treef6e5e703af41a53fe3a9daa3de23e671a1c6ab74 /test/09-test-undo.js
parent466ccfdccd2d761f31ba78a74a40544b77b358e5 (diff)
parentb7b881a00a9b73ba54cc3a62edc402a903ec9142 (diff)
merge
Diffstat (limited to 'test/09-test-undo.js')
-rw-r--r--test/09-test-undo.js164
1 files changed, 164 insertions, 0 deletions
diff --git a/test/09-test-undo.js b/test/09-test-undo.js
new file mode 100644
index 0000000..dbca90e
--- /dev/null
+++ b/test/09-test-undo.js
@@ -0,0 +1,164 @@
+var assert = require("assert")
+var UndoStack = require("../public/assets/javascripts/rectangles/util/undo.js")
+UndoStack.debug = false
+
+describe('undo', function(){
+
+ var state = "zero"
+
+ describe('#register()', function(){
+
+ UndoStack.register({
+ type: "demo",
+ undo: function(myState){
+ state = myState
+ },
+ redo: function(myState){
+ state = myState
+ },
+ })
+
+ it('registers undoable actions', function(){
+ assert( UndoStack.types.hasOwnProperty("demo") )
+ })
+ })
+
+ describe('#push()', function(){
+
+ it('starts empty', function(){
+ assert.equal(0, UndoStack.stack.length)
+ assert.equal(-1, UndoStack.pointer)
+ })
+
+ it('pushes some actions', function(){
+
+ UndoStack.push({
+ type: "demo",
+ undo: state,
+ redo: "one"
+ })
+ state = "one"
+
+ UndoStack.push({
+ type: "demo",
+ undo: state,
+ redo: "two"
+ })
+ state = "two"
+
+ UndoStack.push({
+ type: "demo",
+ undo: state,
+ redo: "three"
+ })
+ state = "three"
+
+ assert.equal(3, UndoStack.stack.length)
+ assert.equal(2, UndoStack.pointer)
+ })
+ })
+
+ describe('#undo()', function(){
+
+ it('retrieves old state', function(){
+ assert.equal("three", state)
+ UndoStack.undo()
+ assert.equal("two", state)
+ assert.equal(1, UndoStack.pointer)
+ })
+
+ it('can only undo so far', function(){
+ var canUndo
+
+ canUndo = UndoStack.undo()
+ assert.equal("one", state)
+ assert.equal(0, UndoStack.pointer)
+ assert.equal(true, canUndo)
+
+ canUndo = UndoStack.undo()
+ assert.equal("zero", state)
+ assert.equal(-1, UndoStack.pointer)
+ assert.equal(false, canUndo)
+
+ canUndo = UndoStack.undo()
+ assert.equal("zero", state)
+ assert.equal(-1, UndoStack.pointer)
+ assert.equal(false, canUndo)
+ })
+
+ })
+
+ describe('#redo()', function(){
+
+ it('reassigns new state', function(){
+ UndoStack.redo()
+ assert.equal("one", state)
+ })
+
+ it('can only redo so far', function(){
+ var canRedo
+
+ canRedo = UndoStack.redo()
+ assert.equal("two", state)
+ assert.equal(true, canRedo)
+
+ canRedo = UndoStack.redo()
+ assert.equal("three", state)
+ assert.equal(false, canRedo)
+
+ canRedo = UndoStack.redo()
+ assert.equal("three", state)
+ assert.equal(false, canRedo)
+ })
+
+ it('clobbers old state if we undo then do something else', function(){
+ UndoStack.undo()
+ assert.equal("two", state)
+ assert.equal(1, UndoStack.pointer)
+
+ UndoStack.undo()
+ assert.equal("one", state)
+ assert.equal(0, UndoStack.pointer)
+ assert.equal(3, UndoStack.stack.length)
+
+ UndoStack.push({
+ type: "demo",
+ undo: state,
+ redo: "four"
+ })
+ state = "four"
+
+ assert.equal(1, UndoStack.pointer)
+ assert.equal(2, UndoStack.stack.length)
+
+ UndoStack.undo()
+ assert.equal("one", state)
+ assert.equal(0, UndoStack.pointer)
+ assert.equal(2, UndoStack.stack.length)
+
+ UndoStack.redo()
+ assert.equal("four", state)
+ assert.equal(1, UndoStack.pointer)
+ assert.equal(2, UndoStack.stack.length)
+ })
+ })
+
+})
+
+// 1) push action
+// 2) push action
+// 3) push action
+// undo 3
+// undo 2
+// undo 1
+// undo * can't undo anymore
+// redo 1
+// redo 2
+// redo 3
+// redo * can't redo anymore
+// undo 3
+// 4) push action (clobbers action 3)
+// undo 4
+// undo 2
+// redo 2
+// redo 4