From 48212c838484dec81e5344084fbeead0c9945261 Mon Sep 17 00:00:00 2001 From: JFH Date: Sat, 25 Jul 2020 19:33:44 +0200 Subject: [PATCH] fix bug in unapply for batch command --- cypress/integration/unit/history.js | 29 ++++++++++++++++++----------- src/svgcanvas/history.js | 4 ++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/cypress/integration/unit/history.js b/cypress/integration/unit/history.js index b4306b7d..32093621 100644 --- a/cypress/integration/unit/history.js +++ b/cypress/integration/unit/history.js @@ -20,11 +20,17 @@ describe('history', function () { // const svg = document.createElementNS(NS.SVG, 'svg'); let undoMgr = null; - class MockCommand { - constructor (optText) { this.text_ = optText; } - apply () { /* */ } // eslint-disable-line class-methods-use-this - unapply () { /* */ } // eslint-disable-line class-methods-use-this - getText () { return this.text_; } + class MockCommand extends hstory.Command { + constructor (optText) { + super(); + this.text = optText; + } + apply (handler) { + super.apply(handler, () => { /* */ }); + } + unapply (handler) { + super.unapply(handler, () => { /* */ }); + } elements () { return []; } // eslint-disable-line class-methods-use-this } @@ -482,17 +488,17 @@ describe('history', function () { it('Test BatchCommand', function () { let concatResult = ''; - MockCommand.prototype.apply = function () { concatResult += this.text_; }; + MockCommand.prototype.apply = function (handler) { concatResult += this.text; }; const batch = new hstory.BatchCommand(); assert.ok(batch.unapply); assert.ok(batch.apply); assert.ok(batch.addSubCommand); assert.ok(batch.isEmpty); - assert.equal(typeof batch.unapply, typeof function () { /* */ }); - assert.equal(typeof batch.apply, typeof function () { /* */ }); - assert.equal(typeof batch.addSubCommand, typeof function () { /* */ }); - assert.equal(typeof batch.isEmpty, typeof function () { /* */ }); + assert.equal(typeof batch.unapply, 'function'); + assert.equal(typeof batch.apply, 'function'); + assert.equal(typeof batch.addSubCommand, 'function'); + assert.equal(typeof batch.isEmpty, 'function'); assert.ok(batch.isEmpty()); @@ -506,8 +512,9 @@ describe('history', function () { assert.equal(concatResult, 'abc'); MockCommand.prototype.apply = function () { /* */ }; - MockCommand.prototype.unapply = function () { concatResult += this.text_; }; + MockCommand.prototype.unapply = function () { concatResult += this.text; }; concatResult = ''; + assert.ok(!concatResult); batch.unapply(); assert.equal(concatResult, 'cba'); diff --git a/src/svgcanvas/history.js b/src/svgcanvas/history.js index 22ff0a70..d79af47d 100644 --- a/src/svgcanvas/history.js +++ b/src/svgcanvas/history.js @@ -22,7 +22,7 @@ export const HistoryEventTypes = { /** * Base class for commands. */ -class Command { +export class Command { /** * @returns {string} */ @@ -434,7 +434,7 @@ export class BatchCommand extends Command { */ unapply (handler) { super.unapply(handler, () => { - this.stack.forEach((stackItem) => { + this.stack.reverse().forEach((stackItem) => { console.assert(stackItem, 'stack item should not be null'); stackItem && stackItem.unapply(handler); });