Files
svgedit/test/history_test.js
Brett Zamir 8c9e40d349 - Breaking change: Rename config file to svgedit-config-iife.js (or for the module version, svgedit-config-es.js);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
    the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
    better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
   to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
    make Node build routine for converting modular source to non-modular,
    use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-25 20:03:14 +08:00

576 lines
16 KiB
JavaScript

/* eslint-env qunit */
import {NS} from '../editor/svgedit.js';
import * as transformlist from '../editor/svgtransformlist.js';
import * as utilities from '../editor/svgutils.js';
import * as history from '../editor/history.js';
// TODO(codedread): Write tests for handling history events.
// Mocked out methods.
transformlist.changeRemoveElementFromListMap(function (elem) {});
utilities.mock({
getHref (elem) { return '#foo'; },
setHref (elem, val) {},
getRotationAngle (elem) { return 0; }
});
// log function
QUnit.log(function (details) {
if (window.console && window.console.log) {
window.console.log(details.result + ' :: ' + details.message);
}
});
// const svg = document.createElementNS(NS.SVG, 'svg');
let undoMgr = null;
const divparent = document.getElementById('divparent');
const div1 = document.getElementById('div1');
const div2 = document.getElementById('div2');
const div3 = document.getElementById('div3');
const div4 = document.getElementById('div4');
const div5 = document.getElementById('div5');
QUnit.module('svgedit.history');
class MockCommand {
constructor (optText) { this.text_ = optText; }
apply () {}
unapply () {}
getText () { return this.text_; }
elements () { return []; }
}
/*
class MockHistoryEventHandler {
handleHistoryEvent (eventType, command) {}
}
*/
function setUp () {
undoMgr = new history.UndoManager();
}
function tearDown () {
undoMgr = null;
}
QUnit.test('Test svgedit.history package', function (assert) {
assert.expect(13);
assert.ok(history);
assert.ok(history.MoveElementCommand);
assert.ok(history.InsertElementCommand);
assert.ok(history.ChangeElementCommand);
assert.ok(history.RemoveElementCommand);
assert.ok(history.BatchCommand);
assert.ok(history.UndoManager);
assert.equal(typeof history.MoveElementCommand, typeof function () {});
assert.equal(typeof history.InsertElementCommand, typeof function () {});
assert.equal(typeof history.ChangeElementCommand, typeof function () {});
assert.equal(typeof history.RemoveElementCommand, typeof function () {});
assert.equal(typeof history.BatchCommand, typeof function () {});
assert.equal(typeof history.UndoManager, typeof function () {});
});
QUnit.test('Test UndoManager methods', function (assert) {
assert.expect(14);
setUp();
assert.ok(undoMgr);
assert.ok(undoMgr.addCommandToHistory);
assert.ok(undoMgr.getUndoStackSize);
assert.ok(undoMgr.getRedoStackSize);
assert.ok(undoMgr.resetUndoStack);
assert.ok(undoMgr.getNextUndoCommandText);
assert.ok(undoMgr.getNextRedoCommandText);
assert.equal(typeof undoMgr, typeof {});
assert.equal(typeof undoMgr.addCommandToHistory, typeof function () {});
assert.equal(typeof undoMgr.getUndoStackSize, typeof function () {});
assert.equal(typeof undoMgr.getRedoStackSize, typeof function () {});
assert.equal(typeof undoMgr.resetUndoStack, typeof function () {});
assert.equal(typeof undoMgr.getNextUndoCommandText, typeof function () {});
assert.equal(typeof undoMgr.getNextRedoCommandText, typeof function () {});
tearDown();
});
QUnit.test('Test UndoManager.addCommandToHistory() function', function (assert) {
assert.expect(3);
setUp();
assert.equal(undoMgr.getUndoStackSize(), 0);
undoMgr.addCommandToHistory(new MockCommand());
assert.equal(undoMgr.getUndoStackSize(), 1);
undoMgr.addCommandToHistory(new MockCommand());
assert.equal(undoMgr.getUndoStackSize(), 2);
tearDown();
});
QUnit.test('Test UndoManager.getUndoStackSize() and getRedoStackSize() functions', function (assert) {
assert.expect(18);
setUp();
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.addCommandToHistory(new MockCommand());
assert.equal(undoMgr.getUndoStackSize(), 3);
assert.equal(undoMgr.getRedoStackSize(), 0);
undoMgr.undo();
assert.equal(undoMgr.getUndoStackSize(), 2);
assert.equal(undoMgr.getRedoStackSize(), 1);
undoMgr.undo();
assert.equal(undoMgr.getUndoStackSize(), 1);
assert.equal(undoMgr.getRedoStackSize(), 2);
undoMgr.undo();
assert.equal(undoMgr.getUndoStackSize(), 0);
assert.equal(undoMgr.getRedoStackSize(), 3);
undoMgr.undo();
assert.equal(undoMgr.getUndoStackSize(), 0);
assert.equal(undoMgr.getRedoStackSize(), 3);
undoMgr.redo();
assert.equal(undoMgr.getUndoStackSize(), 1);
assert.equal(undoMgr.getRedoStackSize(), 2);
undoMgr.redo();
assert.equal(undoMgr.getUndoStackSize(), 2);
assert.equal(undoMgr.getRedoStackSize(), 1);
undoMgr.redo();
assert.equal(undoMgr.getUndoStackSize(), 3);
assert.equal(undoMgr.getRedoStackSize(), 0);
undoMgr.redo();
assert.equal(undoMgr.getUndoStackSize(), 3);
assert.equal(undoMgr.getRedoStackSize(), 0);
tearDown();
});
QUnit.test('Test UndoManager.resetUndoStackSize() function', function (assert) {
assert.expect(4);
setUp();
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.undo();
assert.equal(undoMgr.getUndoStackSize(), 2);
assert.equal(undoMgr.getRedoStackSize(), 1);
undoMgr.resetUndoStack();
assert.equal(undoMgr.getUndoStackSize(), 0);
assert.equal(undoMgr.getRedoStackSize(), 0);
tearDown();
});
QUnit.test('Test UndoManager.getNextUndoCommandText() function', function (assert) {
assert.expect(9);
setUp();
assert.equal(undoMgr.getNextUndoCommandText(), '');
undoMgr.addCommandToHistory(new MockCommand('First'));
undoMgr.addCommandToHistory(new MockCommand('Second'));
undoMgr.addCommandToHistory(new MockCommand('Third'));
assert.equal(undoMgr.getNextUndoCommandText(), 'Third');
undoMgr.undo();
assert.equal(undoMgr.getNextUndoCommandText(), 'Second');
undoMgr.undo();
assert.equal(undoMgr.getNextUndoCommandText(), 'First');
undoMgr.undo();
assert.equal(undoMgr.getNextUndoCommandText(), '');
undoMgr.redo();
assert.equal(undoMgr.getNextUndoCommandText(), 'First');
undoMgr.redo();
assert.equal(undoMgr.getNextUndoCommandText(), 'Second');
undoMgr.redo();
assert.equal(undoMgr.getNextUndoCommandText(), 'Third');
undoMgr.redo();
assert.equal(undoMgr.getNextUndoCommandText(), 'Third');
tearDown();
});
QUnit.test('Test UndoManager.getNextRedoCommandText() function', function (assert) {
assert.expect(8);
setUp();
assert.equal(undoMgr.getNextRedoCommandText(), '');
undoMgr.addCommandToHistory(new MockCommand('First'));
undoMgr.addCommandToHistory(new MockCommand('Second'));
undoMgr.addCommandToHistory(new MockCommand('Third'));
assert.equal(undoMgr.getNextRedoCommandText(), '');
undoMgr.undo();
assert.equal(undoMgr.getNextRedoCommandText(), 'Third');
undoMgr.undo();
assert.equal(undoMgr.getNextRedoCommandText(), 'Second');
undoMgr.undo();
assert.equal(undoMgr.getNextRedoCommandText(), 'First');
undoMgr.redo();
assert.equal(undoMgr.getNextRedoCommandText(), 'Second');
undoMgr.redo();
assert.equal(undoMgr.getNextRedoCommandText(), 'Third');
undoMgr.redo();
assert.equal(undoMgr.getNextRedoCommandText(), '');
tearDown();
});
QUnit.test('Test UndoManager.undo() and redo() functions', function (assert) {
assert.expect(10);
setUp();
let lastCalled = null;
const cmd1 = new MockCommand();
const cmd2 = new MockCommand();
const cmd3 = new MockCommand();
cmd1.apply = function () { lastCalled = 'cmd1.apply'; };
cmd2.apply = function () { lastCalled = 'cmd2.apply'; };
cmd3.apply = function () { lastCalled = 'cmd3.apply'; };
cmd1.unapply = function () { lastCalled = 'cmd1.unapply'; };
cmd2.unapply = function () { lastCalled = 'cmd2.unapply'; };
cmd3.unapply = function () { lastCalled = 'cmd3.unapply'; };
undoMgr.addCommandToHistory(cmd1);
undoMgr.addCommandToHistory(cmd2);
undoMgr.addCommandToHistory(cmd3);
assert.ok(!lastCalled);
undoMgr.undo();
assert.equal(lastCalled, 'cmd3.unapply');
undoMgr.redo();
assert.equal(lastCalled, 'cmd3.apply');
undoMgr.undo();
undoMgr.undo();
assert.equal(lastCalled, 'cmd2.unapply');
undoMgr.undo();
assert.equal(lastCalled, 'cmd1.unapply');
lastCalled = null;
undoMgr.undo();
assert.ok(!lastCalled);
undoMgr.redo();
assert.equal(lastCalled, 'cmd1.apply');
undoMgr.redo();
assert.equal(lastCalled, 'cmd2.apply');
undoMgr.redo();
assert.equal(lastCalled, 'cmd3.apply');
lastCalled = null;
undoMgr.redo();
assert.ok(!lastCalled);
tearDown();
});
QUnit.test('Test MoveElementCommand', function (assert) {
assert.expect(26);
setUp();
let move = new history.MoveElementCommand(div3, div1, divparent);
assert.ok(move.unapply);
assert.ok(move.apply);
assert.equal(typeof move.unapply, typeof function () {});
assert.equal(typeof move.apply, typeof function () {});
move.unapply();
assert.equal(divparent.firstElementChild, div3);
assert.equal(divparent.firstElementChild.nextElementSibling, div1);
assert.equal(divparent.lastElementChild, div2);
move.apply();
assert.equal(divparent.firstElementChild, div1);
assert.equal(divparent.firstElementChild.nextElementSibling, div2);
assert.equal(divparent.lastElementChild, div3);
move = new history.MoveElementCommand(div1, null, divparent);
move.unapply();
assert.equal(divparent.firstElementChild, div2);
assert.equal(divparent.firstElementChild.nextElementSibling, div3);
assert.equal(divparent.lastElementChild, div1);
move.apply();
assert.equal(divparent.firstElementChild, div1);
assert.equal(divparent.firstElementChild.nextElementSibling, div2);
assert.equal(divparent.lastElementChild, div3);
move = new history.MoveElementCommand(div2, div5, div4);
move.unapply();
assert.equal(divparent.firstElementChild, div1);
assert.equal(divparent.firstElementChild.nextElementSibling, div3);
assert.equal(divparent.lastElementChild, div3);
assert.equal(div4.firstElementChild, div2);
assert.equal(div4.firstElementChild.nextElementSibling, div5);
move.apply();
assert.equal(divparent.firstElementChild, div1);
assert.equal(divparent.firstElementChild.nextElementSibling, div2);
assert.equal(divparent.lastElementChild, div3);
assert.equal(div4.firstElementChild, div5);
assert.equal(div4.lastElementChild, div5);
tearDown();
});
QUnit.test('Test InsertElementCommand', function (assert) {
assert.expect(20);
setUp();
let insert = new history.InsertElementCommand(div3);
assert.ok(insert.unapply);
assert.ok(insert.apply);
assert.equal(typeof insert.unapply, typeof function () {});
assert.equal(typeof insert.apply, typeof function () {});
insert.unapply();
assert.equal(divparent.childElementCount, 2);
assert.equal(divparent.firstElementChild, div1);
assert.equal(div1.nextElementSibling, div2);
assert.equal(divparent.lastElementChild, div2);
insert.apply();
assert.equal(divparent.childElementCount, 3);
assert.equal(divparent.firstElementChild, div1);
assert.equal(div1.nextElementSibling, div2);
assert.equal(div2.nextElementSibling, div3);
insert = new history.InsertElementCommand(div2);
insert.unapply();
assert.equal(divparent.childElementCount, 2);
assert.equal(divparent.firstElementChild, div1);
assert.equal(div1.nextElementSibling, div3);
assert.equal(divparent.lastElementChild, div3);
insert.apply();
assert.equal(divparent.childElementCount, 3);
assert.equal(divparent.firstElementChild, div1);
assert.equal(div1.nextElementSibling, div2);
assert.equal(div2.nextElementSibling, div3);
tearDown();
});
QUnit.test('Test RemoveElementCommand', function (assert) {
assert.expect(22);
setUp();
const div6 = document.createElement('div');
div6.id = 'div6';
let remove = new history.RemoveElementCommand(div6, null, divparent);
assert.ok(remove.unapply);
assert.ok(remove.apply);
assert.equal(typeof remove.unapply, typeof function () {});
assert.equal(typeof remove.apply, typeof function () {});
remove.unapply();
assert.equal(divparent.childElementCount, 4);
assert.equal(divparent.firstElementChild, div1);
assert.equal(div1.nextElementSibling, div2);
assert.equal(div2.nextElementSibling, div3);
assert.equal(div3.nextElementSibling, div6);
remove.apply();
assert.equal(divparent.childElementCount, 3);
assert.equal(divparent.firstElementChild, div1);
assert.equal(div1.nextElementSibling, div2);
assert.equal(div2.nextElementSibling, div3);
remove = new history.RemoveElementCommand(div6, div2, divparent);
remove.unapply();
assert.equal(divparent.childElementCount, 4);
assert.equal(divparent.firstElementChild, div1);
assert.equal(div1.nextElementSibling, div6);
assert.equal(div6.nextElementSibling, div2);
assert.equal(div2.nextElementSibling, div3);
remove.apply();
assert.equal(divparent.childElementCount, 3);
assert.equal(divparent.firstElementChild, div1);
assert.equal(div1.nextElementSibling, div2);
assert.equal(div2.nextElementSibling, div3);
tearDown();
});
QUnit.test('Test ChangeElementCommand', function (assert) {
assert.expect(26);
setUp();
div1.setAttribute('title', 'new title');
let change = new history.ChangeElementCommand(div1,
{title: 'old title', class: 'foo'});
assert.ok(change.unapply);
assert.ok(change.apply);
assert.equal(typeof change.unapply, typeof function () {});
assert.equal(typeof change.apply, typeof function () {});
change.unapply();
assert.equal(div1.getAttribute('title'), 'old title');
assert.equal(div1.getAttribute('class'), 'foo');
change.apply();
assert.equal(div1.getAttribute('title'), 'new title');
assert.ok(!div1.getAttribute('class'));
div1.textContent = 'inner text';
change = new history.ChangeElementCommand(div1,
{'#text': null});
change.unapply();
assert.ok(!div1.textContent);
change.apply();
assert.equal(div1.textContent, 'inner text');
div1.textContent = '';
change = new history.ChangeElementCommand(div1,
{'#text': 'old text'});
change.unapply();
assert.equal(div1.textContent, 'old text');
change.apply();
assert.ok(!div1.textContent);
// TODO(codedread): Refactor this #href stuff in history.js and svgcanvas.js
const rect = document.createElementNS(NS.SVG, 'rect');
let justCalled = null;
let gethrefvalue = null;
let sethrefvalue = null;
utilities.mock({
getHref (elem) {
assert.equal(elem, rect);
justCalled = 'getHref';
return gethrefvalue;
},
setHref (elem, val) {
assert.equal(elem, rect);
assert.equal(val, sethrefvalue);
justCalled = 'setHref';
},
getRotationAngle (elem) { return 0; }
});
gethrefvalue = '#newhref';
change = new history.ChangeElementCommand(rect,
{'#href': '#oldhref'});
assert.equal(justCalled, 'getHref');
justCalled = null;
sethrefvalue = '#oldhref';
change.unapply();
assert.equal(justCalled, 'setHref');
justCalled = null;
sethrefvalue = '#newhref';
change.apply();
assert.equal(justCalled, 'setHref');
const line = document.createElementNS(NS.SVG, 'line');
line.setAttributeNS(null, 'class', 'newClass');
change = new history.ChangeElementCommand(line, {class: 'oldClass'});
assert.ok(change.unapply);
assert.ok(change.apply);
assert.equal(typeof change.unapply, typeof function () {});
assert.equal(typeof change.apply, typeof function () {});
change.unapply();
assert.equal(line.getAttributeNS(null, 'class'), 'oldClass');
change.apply();
assert.equal(line.getAttributeNS(null, 'class'), 'newClass');
tearDown();
});
QUnit.test('Test BatchCommand', function (assert) {
assert.expect(13);
setUp();
let concatResult = '';
MockCommand.prototype.apply = function () { concatResult += this.text_; };
const batch = new history.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.ok(batch.isEmpty());
batch.addSubCommand(new MockCommand('a'));
assert.ok(!batch.isEmpty());
batch.addSubCommand(new MockCommand('b'));
batch.addSubCommand(new MockCommand('c'));
assert.ok(!concatResult);
batch.apply();
assert.equal(concatResult, 'abc');
MockCommand.prototype.apply = function () {};
MockCommand.prototype.unapply = function () { concatResult += this.text_; };
concatResult = '';
batch.unapply();
assert.equal(concatResult, 'cba');
MockCommand.prototype.unapply = function () {};
tearDown();
});