forked from lookfirst/wysihtml5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundo_manager_test.js
94 lines (78 loc) · 2.79 KB
/
undo_manager_test.js
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
if (wysihtml5.browser.supportsCommand(document, "insertHTML")) {
module("wysihtml5.UndoManager", {
setup: function() {
this.textareaElement = document.createElement("textarea");
this.textareaElement.value = "1";
document.body.appendChild(this.textareaElement);
},
teardown: function() {
var leftover;
while (leftover = document.querySelector("iframe.wysihtml5-sandbox, input[name='_wysihtml5_mode']")) {
leftover.parentNode.removeChild(leftover);
}
document.body.removeChild(this.textareaElement);
},
triggerUndo: function(editor) {
this.triggerKey(editor, 90);
},
triggerRedo: function(editor) {
this.triggerKey(editor, 89);
},
triggerKey: function(editor, keyCode) {
var event;
try {
event = editor.composer.sandbox.getDocument().createEvent("KeyEvents");
event.initKeyEvent("keydown", true, true, editor.composer.sandbox.getWindow(), true, false, false, false, keyCode, keyCode);
} catch(e) {
event = editor.composer.sandbox.getDocument().createEvent("Events");
event.initEvent("keydown", true, true);
event.ctrlKey = true;
event.keyCode = keyCode;
}
editor.composer.element.dispatchEvent(event);
}
});
asyncTest("Basic test", function() {
expect(5);
var that = this,
editor = new wysihtml5.Editor(this.textareaElement);
editor.on("load", function() {
editor.setValue("1 2").fire("newword:composer");
editor.setValue("1 2 3").fire("newword:composer");
editor.setValue("1 2 3 4").fire("newword:composer");
editor.setValue("1 2 3 4 5");
that.triggerUndo(editor);
equal(editor.getValue(), "1 2 3 4");
that.triggerRedo(editor);
that.triggerRedo(editor);
equal(editor.getValue(), "1 2 3 4 5");
that.triggerUndo(editor);
that.triggerUndo(editor);
equal(editor.getValue(), "1 2 3");
that.triggerUndo(editor);
that.triggerUndo(editor);
equal(editor.getValue(), "1");
that.triggerUndo(editor);
that.triggerUndo(editor);
equal(editor.getValue(), "1");
start();
});
});
asyncTest("Test commands", function() {
expect(3);
var that = this,
editor = new wysihtml5.Editor(this.textareaElement);
editor.on("load", function() {
editor.setValue("<b>1</b>").fire("beforecommand:composer");
editor.setValue("<i><b>1</b></i>").fire("beforecommand:composer");
that.triggerUndo(editor);
equal(editor.getValue(), "<b>1</b>");
that.triggerRedo(editor);
equal(editor.getValue(), "<i><b>1</b></i>");
that.triggerUndo(editor);
that.triggerUndo(editor);
equal(editor.getValue(), "1");
start();
});
});
}