Mailspring/spec/undo-manager-spec.coffee

149 lines
4.4 KiB
CoffeeScript
Raw Normal View History

UndoManager = require "../src/undo-manager"
2015-03-06 07:41:33 +08:00
describe "UndoManager", ->
beforeEach ->
@undoManager = new UndoManager
afterEach ->
advanceClock(500)
2015-03-06 07:41:33 +08:00
it "Initializes empty", ->
expect(@undoManager._history.length).toBe 0
expect(@undoManager._markers.length).toBe 0
expect(@undoManager._historyIndex).toBe -1
expect(@undoManager._markerIndex).toBe -1
2015-03-06 07:41:33 +08:00
it "can push a history item onto the stack", ->
@undoManager.saveToHistory "A"
advanceClock(500)
2015-03-06 07:41:33 +08:00
expect(@undoManager._history[0]).toBe "A"
expect(@undoManager._history.length).toBe 1
expect(@undoManager.current()).toBe "A"
it "updates the historyIndex", ->
@undoManager.saveToHistory "A"
expect(@undoManager._historyIndex).toBe 0
it "updates the markerIndex", ->
@undoManager.saveToHistory "A"
advanceClock(500)
@undoManager.saveToHistory "AB"
@undoManager.saveToHistory "ABC"
advanceClock(500)
expect(@undoManager._markerIndex).toBe 1
expect(@undoManager._historyIndex).toBe 2
2015-03-06 07:41:33 +08:00
describe "when undoing", ->
beforeEach ->
@undoManager.saveToHistory "A"
advanceClock(500)
@undoManager.saveToHistory "AB"
@undoManager.saveToHistory "ABC"
2015-03-06 07:41:33 +08:00
it "returns the last item on the stack at the most recent marker", ->
expect(@undoManager.undo()).toBe "A"
2015-03-06 07:41:33 +08:00
it "doesn't change the size of the stack", ->
@undoManager.undo()
expect(@undoManager._history.length).toBe 3
it "set the historyIndex properly", ->
@undoManager.undo()
expect(@undoManager._historyIndex).toBe 0
it "set the markerIndex properly after a wait", ->
advanceClock(500)
2015-03-06 07:41:33 +08:00
@undoManager.undo()
expect(@undoManager._markerIndex).toBe 0
it "set the markerIndex properly when undo fires immediately", ->
@undoManager.undo()
expect(@undoManager._markerIndex).toBe 0
2015-03-06 07:41:33 +08:00
it "returns null when there's nothing to undo", ->
@undoManager.undo()
@undoManager.undo()
expect(@undoManager.undo()).toBe null
expect(@undoManager._markerIndex).toBe 0
2015-03-06 07:41:33 +08:00
describe "when redoing", ->
beforeEach ->
@undoManager.saveToHistory "X"
advanceClock(500)
@undoManager.saveToHistory "XY"
@undoManager.saveToHistory "XYZ"
2015-03-06 07:41:33 +08:00
it "returns the last item on the stack after a wait", ->
advanceClock(500)
@undoManager.undo()
advanceClock(500)
expect(@undoManager.redo()).toBe "XYZ"
it "returns the last item on the stack when fired immediately", ->
2015-03-06 07:41:33 +08:00
@undoManager.undo()
expect(@undoManager.redo()).toBe "XYZ"
it "doesn't change the size of the stack", ->
@undoManager.undo()
@undoManager.redo()
expect(@undoManager._history.length).toBe 3
it "set the historyIndex properly", ->
@undoManager.undo()
@undoManager.redo()
expect(@undoManager._historyIndex).toBe 2
it "set the markerIndex properly", ->
2015-03-06 07:41:33 +08:00
@undoManager.undo()
@undoManager.redo()
expect(@undoManager._markerIndex).toBe 1
2015-03-06 07:41:33 +08:00
it "returns null when there's nothing to redo", ->
expect(@undoManager.redo()).toBe null
expect(@undoManager.redo()).toBe null
expect(@undoManager._markerIndex).toBe 1
2015-03-06 07:41:33 +08:00
describe "when undoing and adding items", ->
beforeEach ->
@undoManager.saveToHistory "1"
advanceClock(500)
@undoManager.saveToHistory "12"
@undoManager.saveToHistory "123"
advanceClock(500)
@undoManager.saveToHistory "1234"
2015-03-06 07:41:33 +08:00
@undoManager.undo()
@undoManager.undo()
advanceClock(500)
@undoManager.saveToHistory "A"
advanceClock(500)
2015-03-06 07:41:33 +08:00
it "correctly sets the history", ->
expect(@undoManager._history).toEqual ["1", "A"]
2015-03-06 07:41:33 +08:00
it "correctly sets the length", ->
expect(@undoManager._history.length).toBe 2
2015-03-06 07:41:33 +08:00
it "puts the correct items on the stack", ->
@undoManager.undo()
expect(@undoManager.redo()).toBe "A"
it "sets the historyIndex correctly", ->
expect(@undoManager._historyIndex).toBe 1
2015-03-06 07:41:33 +08:00
describe "when the stack is full", ->
beforeEach ->
@undoManager._MAX_HISTORY_SIZE = 2
@undoManager.saveToHistory "A"
@undoManager.saveToHistory "AB"
@undoManager.saveToHistory "ABC"
@undoManager.saveToHistory "ABCD"
2015-03-06 07:41:33 +08:00
it "correctly sets the length", ->
expect(@undoManager._history.length).toBe 2
it "keeps the latest histories", ->
expect(@undoManager._history).toEqual ["ABC", "ABCD"]
it "updates the historyIndex", ->
expect(@undoManager._historyIndex).toBe 1