Repository navigation
Expand file tree
/
Copy pathCommandManager-test.js
More file actions
287 lines (251 loc) · 12.2 KB
/
Copy pathCommandManager-test.js
File metadata and controls
287 lines (251 loc) · 12.2 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global describe, beforeEach, afterEach, it, expect */
define(function (require, exports, module) {
// Load dependent modules
var CommandManager = require("command/CommandManager");
describe("CommandManager", function () {
var commandID = "commandID";
var executed;
var testCommandFn = function () { executed = true; };
beforeEach(function () {
executed = false;
CommandManager._testReset();
});
afterEach(function () {
CommandManager._testRestore();
});
it("register and get a command and validate parameters", function () {
var command = CommandManager.register("test command", commandID, testCommandFn);
expect(command).toBeTruthy();
expect(command.getName()).toBe("test command");
expect(command.getID()).toBe(commandID);
expect(command.getEnabled()).toBeTruthy();
expect(command.getChecked()).toBe(undefined);
expect(command._commandFn).toBe(testCommandFn);
// duplicate command
expect(CommandManager.register("test command", commandID, testCommandFn)).toBeFalsy();
// missing arguments
expect(CommandManager.register(null, "test-command-id2", testCommandFn)).toBe(null);
expect(CommandManager.register("test command", null, testCommandFn)).toBe(null);
expect(CommandManager.register("test command", "test-command-id2", null)).toBe(null);
});
it("execute a command", function () {
var command = CommandManager.register("test command", commandID, testCommandFn);
command.execute();
expect(executed).toBeTruthy();
});
it("not execute a disabled command", function () {
var command = CommandManager.register("test command", commandID, testCommandFn);
command.setEnabled(false);
command.execute();
expect(executed).toBeFalsy();
});
it("set enabled state and trigger enabledStateChange", function () {
var eventTriggered = false;
var command = CommandManager.register("test command", commandID, testCommandFn);
command.on("enabledStateChange", function () {
eventTriggered = true;
});
command.setEnabled(false);
expect(eventTriggered).toBeTruthy();
expect(command.getEnabled()).toBeFalsy();
});
it("set checked state and trigger checkedStateChange", function () {
var eventTriggered = false;
var command = CommandManager.register("test command", commandID, testCommandFn);
command.on("checkedStateChange", function () {
eventTriggered = true;
});
command.setChecked(true);
expect(eventTriggered).toBeTruthy();
expect(command.getChecked()).toBeTruthy();
});
it("rename command trigger nameChange", function () {
var eventTriggered = false;
var command = CommandManager.register("test command", commandID, testCommandFn);
command.on("nameChange", function () {
eventTriggered = true;
});
command.setName("newName");
expect(eventTriggered).toBeTruthy();
expect(command.getName()).toBe("newName");
});
it("execute a command with optional source event", function () {
let event = {eventSource: "hello"};
let receivedEvent;
let command = CommandManager.register("test command", commandID, (event)=>{
receivedEvent = event;
}, {eventSource: true});
command.execute(event);
expect(receivedEvent).toBe(event);
// now do with command manager exec
receivedEvent = null;
CommandManager.execute(commandID, event);
expect(receivedEvent).toBe(event);
});
it("execute a command with optional source event and additional exec args", function () {
let event = {eventSource: "hello"};
let receivedEvent, receiveArg1, receiveArg2;
let command = CommandManager.register("test command", commandID, (event, arg1, arg2)=>{
receivedEvent = event;
receiveArg1 = arg1;
receiveArg2 = arg2;
}, {eventSource: true});
command.execute(event, 42, "arg2");
expect(receivedEvent).toBe(event);
expect(receiveArg1).toBe(42);
expect(receiveArg2).toBe("arg2");
// now do with command manager exec
receivedEvent = null;
CommandManager.execute(commandID, event, 55, "argx");
expect(receivedEvent).toBe(event);
expect(receiveArg1).toBe(55);
expect(receiveArg2).toBe("argx");
});
it("execute a command registered with eventSource should get event even if execute misses the source", function () {
let receivedEvent;
let command = CommandManager.register("test command", commandID, (event)=>{
receivedEvent = event;
}, {eventSource: true});
command.execute();
expect(receivedEvent).toEql({ eventSource: 'otherExecAction' });
// now do with command manager exec
receivedEvent = null;
CommandManager.execute(commandID);
expect(receivedEvent).toEql({ eventSource: 'otherExecAction' });
});
it("execute a command will not get eventSource if eventSource option set to false", function () {
let receivedEvent;
let command = CommandManager.register("test command", commandID, (event)=>{
receivedEvent = event;
}, {eventSource: false});
command.execute();
expect(receivedEvent).not.toBeDefined();
// now do with command manager exec
receivedEvent = null;
CommandManager.execute(commandID);
expect(receivedEvent).not.toBeDefined();
});
it("execute a command will not get eventSource if eventSource option not given", function () {
let receivedEvent;
let command = CommandManager.register("test command", commandID, (event)=>{receivedEvent = event;});
command.execute();
expect(receivedEvent).not.toBeDefined();
// now do with command manager exec
receivedEvent = null;
CommandManager.execute(commandID);
expect(receivedEvent).not.toBeDefined();
});
it("register command with htmlName option", function () {
var htmlName = "Phoenix menu<i class='fa fa-car' style='margin-left: 4px;'></i>";
var command = CommandManager.register("test command", "test-htmlname-command", testCommandFn, {
htmlName: htmlName
});
expect(command).toBeTruthy();
expect(command.getName()).toBe("test command");
expect(command.getOptions().htmlName).toBe(htmlName);
});
it("getOptions should return empty object when no options provided", function () {
var command = CommandManager.register("test command", "test-no-options-command", testCommandFn);
expect(command).toBeTruthy();
expect(command.getOptions()).toEql({});
});
it("getOptions should return options when provided", function () {
var options = {
eventSource: true,
htmlName: "Test <b>HTML</b> Name"
};
var command = CommandManager.register("test command", "test-with-options-command", testCommandFn, options);
expect(command).toBeTruthy();
expect(command.getOptions()).toEql(options);
});
it("setName with htmlName parameter and trigger nameChange", function () {
var eventTriggered = false;
var command = CommandManager.register("test command", "test-setname-html-command", testCommandFn);
command.on("nameChange", function () {
eventTriggered = true;
});
var newName = "new command name";
var htmlName = "New <i class='fa fa-star'></i> Name";
command.setName(newName, htmlName);
expect(eventTriggered).toBeTruthy();
expect(command.getName()).toBe(newName);
expect(command.getOptions().htmlName).toBe(htmlName);
});
it("setName should trigger nameChange when only htmlName changes", function () {
var eventTriggered = false;
var command = CommandManager.register("test command", "test-setname-htmlonly-command", testCommandFn, {
htmlName: "original html"
});
command.on("nameChange", function () {
eventTriggered = true;
});
var newHtmlName = "Updated <span>HTML</span> Name";
command.setName(command.getName(), newHtmlName);
expect(eventTriggered).toBeTruthy();
expect(command.getOptions().htmlName).toBe(newHtmlName);
});
it("setName should not trigger nameChange when name and htmlName are unchanged", function () {
var eventTriggered = false;
var htmlName = "Same HTML Name";
var command = CommandManager.register("test command", "test-setname-same-command", testCommandFn, {
htmlName: htmlName
});
command.on("nameChange", function () {
eventTriggered = true;
});
command.setName(command.getName(), htmlName);
expect(eventTriggered).toBeFalsy();
});
it("should handle edge cases for htmlName", function () {
// Test with empty string htmlName
var command1 = CommandManager.register("test command", "test-empty-html-command", testCommandFn, {
htmlName: ""
});
expect(command1.getOptions().htmlName).toBe("");
// Test with null htmlName
var command2 = CommandManager.register("test command", "test-null-html-command", testCommandFn, {
htmlName: null
});
expect(command2.getOptions().htmlName).toBe(null);
// Test with undefined htmlName (should not be set)
var command3 = CommandManager.register("test command", "test-undefined-html-command", testCommandFn, {
htmlName: undefined
});
expect(command3.getOptions().htmlName).toBe(undefined);
});
it("setName should handle edge cases for htmlName parameter", function () {
var command = CommandManager.register("test command", "test-setname-edge-command", testCommandFn);
// Test setting htmlName to empty string
command.setName("test name", "");
expect(command.getOptions().htmlName).toBe("");
// Test setting htmlName to null (should still trigger change if it was different)
var eventTriggered = false;
command.on("nameChange", function () {
eventTriggered = true;
});
command.setName("test name", null);
expect(command.getOptions().htmlName).toBe(null);
expect(eventTriggered).toBeTruthy();
});
});
});