Allow History#{perform,replace} to run multiple actions

This commit is contained in:
John Firebaugh
2012-12-06 16:13:52 -05:00
parent 7fea779e8c
commit 27af384a3f
2 changed files with 58 additions and 4 deletions

View File

@@ -19,16 +19,28 @@ iD.History = function() {
}
},
perform: function (action) {
perform: function () {
stack = stack.slice(0, index + 1);
stack.push(action(this.graph()));
var graph = this.graph();
for (var i = 0; i < arguments.length; i++) {
graph = arguments[i](graph);
}
stack.push(graph);
index++;
maybeChange();
},
replace: function (action) {
replace: function () {
// assert(index == stack.length - 1)
stack[index] = action(this.graph());
var graph = this.graph();
for (var i = 0; i < arguments.length; i++) {
graph = arguments[i](graph);
}
stack[index] = graph;
maybeChange();
},

View File

@@ -36,6 +36,48 @@ describe("History", function () {
history.perform(iD.actions.Noop);
expect(spy).not.to.have.been.called;
});
it("performs multiple actions", function () {
var action1 = sinon.stub().returns(graph),
action2 = sinon.stub().returns(graph);
history.perform(action1, action2);
expect(action1).to.have.been.called;
expect(action2).to.have.been.called;
});
});
describe("#replace", function () {
it("updates the graph", function () {
history.replace(action);
expect(history.graph()).to.equal(graph);
});
it("replaces the undo stack", function () {
history.perform(action);
history.replace(action);
history.undo();
expect(history.undoAnnotation()).to.be.undefined;
});
it("emits a change event", function () {
history.on('change', spy);
history.replace(action);
expect(spy).to.have.been.called;
});
it("does not emit a change event when performing a noop", function () {
history.on('change', spy);
history.replace(iD.actions.Noop);
expect(spy).not.to.have.been.called;
});
it("performs multiple actions", function () {
var action1 = sinon.stub().returns(graph),
action2 = sinon.stub().returns(graph);
history.replace(action1, action2);
expect(action1).to.have.been.called;
expect(action2).to.have.been.called;
});
});
describe("#undo", function () {