summaryrefslogtreecommitdiff
path: root/test/09-test-undo.js
blob: f774c047cae3c3a6ca37a3cbdb9d078e04e0d58b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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",
        prev: state,
        next: "one"
      })
      state = "one"

      UndoStack.push({
        type: "demo",
        prev: state,
        next: "two"
      })
      state = "two"

      UndoStack.push({
        type: "demo",
        prev: state,
        next: "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",
        prev: state,
        next: "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