Skip to content

Commit 1e84488

Browse files
committed
test: unit and integ test for html menu item and command name
1 parent 53bb0b7 commit 1e84488

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed

src/command/CommandManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ define(function (require, exports, module) {
212212
let changed = this._name !== name;
213213
this._name = name;
214214

215-
if (htmlName && this._options.htmlName !== htmlName) {
215+
if (this._options.htmlName !== htmlName) {
216216
changed = true;
217217
this._options.htmlName = htmlName;
218218
}

test/spec/CommandManager-test.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,115 @@ define(function (require, exports, module) {
173173
CommandManager.execute(commandID);
174174
expect(receivedEvent).not.toBeDefined();
175175
});
176+
177+
it("register command with htmlName option", function () {
178+
var htmlName = "Phoenix menu<i class='fa fa-car' style='margin-left: 4px;'></i>";
179+
var command = CommandManager.register("test command", "test-htmlname-command", testCommandFn, {
180+
htmlName: htmlName
181+
});
182+
expect(command).toBeTruthy();
183+
expect(command.getName()).toBe("test command");
184+
expect(command.getOptions().htmlName).toBe(htmlName);
185+
});
186+
187+
it("getOptions should return empty object when no options provided", function () {
188+
var command = CommandManager.register("test command", "test-no-options-command", testCommandFn);
189+
expect(command).toBeTruthy();
190+
expect(command.getOptions()).toEql({});
191+
});
192+
193+
it("getOptions should return options when provided", function () {
194+
var options = {
195+
eventSource: true,
196+
htmlName: "Test <b>HTML</b> Name"
197+
};
198+
var command = CommandManager.register("test command", "test-with-options-command", testCommandFn, options);
199+
expect(command).toBeTruthy();
200+
expect(command.getOptions()).toEql(options);
201+
});
202+
203+
it("setName with htmlName parameter and trigger nameChange", function () {
204+
var eventTriggered = false;
205+
var command = CommandManager.register("test command", "test-setname-html-command", testCommandFn);
206+
command.on("nameChange", function () {
207+
eventTriggered = true;
208+
});
209+
210+
var newName = "new command name";
211+
var htmlName = "New <i class='fa fa-star'></i> Name";
212+
command.setName(newName, htmlName);
213+
214+
expect(eventTriggered).toBeTruthy();
215+
expect(command.getName()).toBe(newName);
216+
expect(command.getOptions().htmlName).toBe(htmlName);
217+
});
218+
219+
it("setName should trigger nameChange when only htmlName changes", function () {
220+
var eventTriggered = false;
221+
var command = CommandManager.register("test command", "test-setname-htmlonly-command", testCommandFn, {
222+
htmlName: "original html"
223+
});
224+
command.on("nameChange", function () {
225+
eventTriggered = true;
226+
});
227+
228+
var newHtmlName = "Updated <span>HTML</span> Name";
229+
command.setName(command.getName(), newHtmlName);
230+
231+
expect(eventTriggered).toBeTruthy();
232+
expect(command.getOptions().htmlName).toBe(newHtmlName);
233+
});
234+
235+
it("setName should not trigger nameChange when name and htmlName are unchanged", function () {
236+
var eventTriggered = false;
237+
var htmlName = "Same HTML Name";
238+
var command = CommandManager.register("test command", "test-setname-same-command", testCommandFn, {
239+
htmlName: htmlName
240+
});
241+
command.on("nameChange", function () {
242+
eventTriggered = true;
243+
});
244+
245+
command.setName(command.getName(), htmlName);
246+
247+
expect(eventTriggered).toBeFalsy();
248+
});
249+
250+
it("should handle edge cases for htmlName", function () {
251+
// Test with empty string htmlName
252+
var command1 = CommandManager.register("test command", "test-empty-html-command", testCommandFn, {
253+
htmlName: ""
254+
});
255+
expect(command1.getOptions().htmlName).toBe("");
256+
257+
// Test with null htmlName
258+
var command2 = CommandManager.register("test command", "test-null-html-command", testCommandFn, {
259+
htmlName: null
260+
});
261+
expect(command2.getOptions().htmlName).toBe(null);
262+
263+
// Test with undefined htmlName (should not be set)
264+
var command3 = CommandManager.register("test command", "test-undefined-html-command", testCommandFn, {
265+
htmlName: undefined
266+
});
267+
expect(command3.getOptions().htmlName).toBe(undefined);
268+
});
269+
270+
it("setName should handle edge cases for htmlName parameter", function () {
271+
var command = CommandManager.register("test command", "test-setname-edge-command", testCommandFn);
272+
273+
// Test setting htmlName to empty string
274+
command.setName("test name", "");
275+
expect(command.getOptions().htmlName).toBe("");
276+
277+
// Test setting htmlName to null (should still trigger change if it was different)
278+
var eventTriggered = false;
279+
command.on("nameChange", function () {
280+
eventTriggered = true;
281+
});
282+
command.setName("test name", null);
283+
expect(command.getOptions().htmlName).toBe(null);
284+
expect(eventTriggered).toBeTruthy();
285+
});
176286
});
177287
});

test/spec/Menu-integ-test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,85 @@ define(function (require, exports, module) {
403403
hideCommand.setEnabled(true);
404404
expect(element.getElementsByClassName("forced-hidden").length).toBe(0);
405405
});
406+
407+
it("should display htmlName in menu item when provided", async function () {
408+
const utMenuID = "menuitem-htmlname-test";
409+
const testCmd = "Menu-test.htmlname-command";
410+
const htmlName = "Phoenix menu<i class='fa fa-car' style='margin-left: 4px;'></i>";
411+
412+
// Register command with htmlName option
413+
CommandManager.register("Plain Text Name", testCmd, function () {}, {
414+
htmlName: htmlName
415+
});
416+
417+
const menu = Menus.addMenu("HTML Name Test Menu", utMenuID);
418+
const menuItem = menu.addMenuItem(testCmd);
419+
expect(menuItem).toBeTruthy();
420+
421+
const listSelector = "#menuitem-htmlname-test > ul";
422+
const $listItems = testWindow.$(listSelector).children();
423+
expect($listItems.length).toBe(1);
424+
425+
// Check that the menu item contains the HTML content
426+
const $menuLink = $($listItems[0]).find("a .menu-name");
427+
expect($menuLink.html().includes("Phoenix menu")).toBeTrue();
428+
expect($menuLink.html().includes("fa fa-car")).toBeTrue();
429+
expect($menuLink.html().includes("margin-left: 4px;")).toBeTrue();
430+
431+
// Verify the HTML is rendered (not just as text)
432+
expect($menuLink.find("i.fa.fa-car").length).toBe(1);
433+
});
434+
435+
it("should fall back to regular name when htmlName is not provided", async function () {
436+
const utMenuID = "menuitem-fallback-test";
437+
const testCmd = "Menu-test.fallback-command";
438+
const plainName = "Plain Menu Name";
439+
440+
// Register command without htmlName option
441+
CommandManager.register(plainName, testCmd, function () {});
442+
443+
const menu = Menus.addMenu("Fallback Test Menu", utMenuID);
444+
const menuItem = menu.addMenuItem(testCmd);
445+
expect(menuItem).toBeTruthy();
446+
447+
const listSelector = "#menuitem-fallback-test > ul";
448+
const $listItems = testWindow.$(listSelector).children();
449+
expect($listItems.length).toBe(1);
450+
451+
// Check that the menu item displays the regular name (no HTML)
452+
const $menuLink = $($listItems[0]).find("a .menu-name");
453+
expect($menuLink.text()).toBe(plainName);
454+
});
455+
456+
it("should update menu display when htmlName is changed via setName", async function () {
457+
const utMenuID = "menuitem-setname-test";
458+
const testCmd = "Menu-test.setname-command";
459+
const originalName = "Original Name";
460+
const newName = "Updated Name";
461+
const newHtmlName = "Updated <b>Bold</b> Name";
462+
463+
// Register command without htmlName initially
464+
const command = CommandManager.register(originalName, testCmd, function () {});
465+
466+
const menu = Menus.addMenu("SetName Test Menu", utMenuID);
467+
const menuItem = menu.addMenuItem(testCmd);
468+
expect(menuItem).toBeTruthy();
469+
470+
const listSelector = "#menuitem-setname-test > ul";
471+
const $listItems = testWindow.$(listSelector).children();
472+
const $menuLink = $($listItems[0]).find("a .menu-name");
473+
474+
// Initially should show regular text name
475+
expect($menuLink.text()).toBe(originalName);
476+
477+
// Update name with htmlName
478+
command.setName(newName, newHtmlName);
479+
480+
// Should now display HTML content
481+
expect($menuLink.html()).toBe(newHtmlName);
482+
expect($menuLink.find("b").length).toBe(1);
483+
expect($menuLink.find("b").text()).toBe("Bold");
484+
});
406485
});
407486

408487

0 commit comments

Comments
 (0)