Skip to content

Commit 7f52a82

Browse files
committed
Add and fix tests
1 parent 1ba0b9e commit 7f52a82

7 files changed

+151
-272
lines changed

Gruntfile.coffee

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = (grunt) ->
4646
jshintrc: ".jshintrc"
4747

4848
qunit:
49-
all: ["test/index.html", "test/index-jquery-ui-1-11.html"]
49+
all: ["test/index.html", "test/index-jquery-ui-1-10.html"]
5050

5151
# replace: # grunt-text-replace
5252
# production:
@@ -89,11 +89,13 @@ module.exports = (grunt) ->
8989
{ browserName: "firefox", platform: "Linux" }
9090
{ browserName: "internet explorer", version: "6", platform: "Windows XP" }
9191
{ browserName: "internet explorer", version: "7", platform: "Windows XP" }
92-
{ browserName: "internet explorer", version: "8", platform: "Windows XP" }
92+
{ browserName: "internet explorer", version: "8", platform: "Windows 7" }
9393
{ browserName: "internet explorer", version: "9", platform: "Windows 7" }
9494
{ browserName: "internet explorer", version: "10", platform: "Windows 8" }
9595
{ browserName: "internet explorer", version: "11", platform: "Windows 8.1" }
96-
{ browserName: "safari", platform: "OS X 10.8" }
96+
{ browserName: "safari", version: "6", platform: "OS X 10.8" }
97+
{ browserName: "safari", version: "7", platform: "OS X 10.9" }
98+
{ browserName: "safari", version: "8", platform: "OS X 10.10" }
9799
]
98100
testname: "jquery.ui-contextmenu qunit tests"
99101

jquery.ui-contextmenu.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -189,24 +189,26 @@ $.widget("moogle.contextmenu", {
189189
// Prevent browser from opening the system context menu
190190
event.preventDefault();
191191

192+
this.currentTarget = event.target;
193+
192194
if ( !recursive ) {
193195
res = this._trigger("beforeOpen", event, ui);
194196
promise = (ui.result && $.isFunction(ui.result.promise)) ? ui.result : null;
195197
ui.result = null;
196198
if ( res === false ) {
197-
// this.currentTarget = null;
199+
this.currentTarget = null;
198200
return false;
199201
} else if ( promise ) {
200202
// Handler returned a Deferred or Promise. Delay menu open until
201203
// the promise is resolved
202204
promise.done(function() {
203205
self._openMenu(event, true);
204206
});
207+
this.currentTarget = null;
205208
return false;
206209
}
207210
ui.menu = this.$menu; // Might have changed in beforeOpen
208211
}
209-
this.currentTarget = event.target;
210212

211213
// Register global event handlers that close the dropdown-menu
212214
$(document).bind("keydown" + this.eventNamespace, function(event) {

test/index-jquery-ui-1-11.html renamed to test/index-jquery-ui-1-10.html

+13-10
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
<head>
44
<meta charset="utf-8">
55
<title>jquery.ui-contextmenu Test Suite</title>
6-
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.10.0.css">
6+
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.15.0.css">
77
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
8-
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
9-
<script src="http://code.jquery.com/qunit/qunit-1.10.0.js"></script>
8+
<!--
9+
<script src="jquery-ui.js"></script>
10+
-->
11+
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
12+
<script src="http://code.jquery.com/qunit/qunit-1.15.0.js"></script>
1013
<script src="../jquery.ui-contextmenu.js"></script>
1114
<script src="tests.js"></script>
1215
</head>
1316
<body>
14-
<h1 id="qunit-header">jquery.ui-contextmenu Test Suite for jQuery UI >= 1.11.0</h1>
17+
<h1 id="qunit-header">jquery.ui-contextmenu Test Suite for older jQuery UI 1.10</h1>
1518
<h2 id="qunit-banner"></h2>
1619
<div id="qunit-testrunner-toolbar"></div>
1720
<h2 id="qunit-userAgent"></h2>
@@ -27,14 +30,14 @@ <h3>Sample Markup</h3>
2730
<!-- This element will be copied as #sampleMenu for every test: -->
2831

2932
<ul id="sampleMenuTemplate" style="display: none;">
30-
<li data-command="cut"><span class="ui-icon ui-icon-scissors"></span>Cut
31-
<li data-command="copy"><span class="ui-icon ui-icon-copy"></span>Copy
32-
<li data-command="paste" class="ui-state-disabled ui-icon-clipboard">Paste
33+
<li data-command="cut"><a href="#"><span class="ui-icon ui-icon-scissors"></span>Cut</a>
34+
<li data-command="copy"><a href="#"><span class="ui-icon ui-icon-copy"></span>Copy</a>
35+
<li data-command="paste" class="ui-state-disabled ui-icon-clipboard"><a href="#">Paste</a>
3336
<li>----
34-
<li>More
37+
<li><a>More</a>
3538
<ul>
36-
<li data-command="sub1">Sub Item 1
37-
<li data-command="sub2">Sub Item 2
39+
<li data-command="sub1"><a href="#">Sub Item 1</a>
40+
<li data-command="sub2"><a href="#">Sub Item 2</a>
3841
</ul>
3942
</ul>
4043
</body>

test/index.html

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1+
12
<!doctype html>
23
<html>
34
<head>
45
<meta charset="utf-8">
56
<title>jquery.ui-contextmenu Test Suite</title>
6-
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.10.0.css">
7-
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
8-
<!--
9-
<script src="jquery-ui.js"></script>
10-
-->
11-
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
12-
<script src="http://code.jquery.com/qunit/qunit-1.10.0.js"></script>
7+
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.15.0.css">
8+
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
9+
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
10+
<script src="http://code.jquery.com/qunit/qunit-1.15.0.js"></script>
1311
<script src="../jquery.ui-contextmenu.js"></script>
1412
<script src="tests.js"></script>
1513
</head>
1614
<body>
17-
<h1 id="qunit-header">jquery.ui-contextmenu Test Suite</h1>
15+
<h1 id="qunit-header">jquery.ui-contextmenu Test Suite for jQuery UI 1.11+</h1>
1816
<h2 id="qunit-banner"></h2>
1917
<div id="qunit-testrunner-toolbar"></div>
2018
<h2 id="qunit-userAgent"></h2>
@@ -30,14 +28,14 @@ <h3>Sample Markup</h3>
3028
<!-- This element will be copied as #sampleMenu for every test: -->
3129

3230
<ul id="sampleMenuTemplate" style="display: none;">
33-
<li data-command="cut"><a href="#"><span class="ui-icon ui-icon-scissors"></span>Cut</a>
34-
<li data-command="copy"><a href="#"><span class="ui-icon ui-icon-copy"></span>Copy</a>
35-
<li data-command="paste" class="ui-state-disabled ui-icon-clipboard"><a href="#">Paste</a>
31+
<li data-command="cut"><span class="ui-icon ui-icon-scissors"></span>Cut
32+
<li data-command="copy"><span class="ui-icon ui-icon-copy"></span>Copy
33+
<li data-command="paste" class="ui-state-disabled ui-icon-clipboard">Paste
3634
<li>----
37-
<li><a>More</a>
35+
<li>More
3836
<ul>
39-
<li data-command="sub1"><a href="#">Sub Item 1</a>
40-
<li data-command="sub2"><a href="#">Sub Item 2</a>
37+
<li data-command="sub1">Sub Item 1
38+
<li data-command="sub2">Sub Item 2
4139
</ul>
4240
</ul>
4341
</body>

test/issue-80-async.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
// or return a promise to delay opening until an async response becomes
110110
// available
111111
ui.result = dfd.promise();
112-
console.log("beforeOpen", event, ui, event.originalEvent.type);
112+
// console.log("beforeOpen", event, ui, event.originalEvent.type);
113113
return;
114114
},
115115
focus: function(event, ui) {

test/tests.js

+116-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ function TestHelpers() {
1616
uiVersion = {
1717
major: parseInt(match[1], 10),
1818
minor: parseInt(match[2], 10)
19+
},
20+
uiVersionBefore11 = ( uiVersion.major < 2 && uiVersion.minor < 11 ),
21+
findEntry = function( menu, indexOrCommand ) {
22+
if ( typeof indexOrCommand === "number" ) {
23+
return menu.children( ":eq(" + indexOrCommand + ")" );
24+
}
25+
return menu.find("li[data-command=" + indexOrCommand + "]");
1926
};
2027

2128
return {
@@ -37,22 +44,32 @@ function TestHelpers() {
3744
},
3845
entryEvent: function( menu, item, type ) {
3946
lastItem = item;
40-
if ( uiVersion.major < 2 && uiVersion.minor < 11 ) {
41-
menu.children( ":eq(" + item + ")" ).find( "a:first" ).trigger( type );
47+
if ( uiVersionBefore11 ) {
48+
findEntry(menu, item).find( "a:first" ).trigger( type );
4249
} else {
43-
menu.children( ":eq(" + item + ")" ).trigger( type );
50+
findEntry(menu, item).trigger( type );
4451
}
4552
},
4653
click: function( menu, item ) {
4754
lastItem = item;
48-
if ( uiVersion.major < 2 && uiVersion.minor < 11 ) {
49-
menu.children( ":eq(" + item + ")" ).find( "a:first" ).trigger( "click" );
55+
if ( uiVersionBefore11 ) {
56+
findEntry(menu, item).find( "a:first" ).trigger( "click" );
5057
} else {
51-
menu.children( ":eq(" + item + ")" ).trigger( "click" );
58+
findEntry(menu, item).trigger( "click" );
5259
}
5360
},
54-
entry: function( menu, item ) {
55-
return menu.children( ":eq(" + item + ")" );
61+
entry: findEntry,
62+
entryTitle: function( menu, item ) {
63+
// return the plain text (without sub-elements)
64+
if ( uiVersionBefore11 ) {
65+
return findEntry(menu, item).find( "a:first" ).contents().filter(function() {
66+
return this.nodeType === 3;
67+
})[0].nodeValue;
68+
} else {
69+
return findEntry(menu, item).contents().filter(function() {
70+
return this.nodeType === 3;
71+
})[0].nodeValue;
72+
}
5673
}
5774
};
5875
}
@@ -72,6 +89,7 @@ var th = new TestHelpers(),
7289
logOutput = th.logOutput,
7390
click = th.click,
7491
entryEvent = th.entryEvent,
92+
entryTitle = th.entryTitle,
7593
entry = th.entry,
7694
lifecycle = {
7795
setup: function() {
@@ -394,6 +412,96 @@ asyncTest("Array menu", function() {
394412
"Event sequence OK.");
395413
start();
396414
}, 500);
415+
});
416+
417+
// ****************************************************************************
418+
419+
module("'beforeOpen' event", lifecycle);
420+
421+
asyncTest("modify on open", function() {
422+
var $ctx, $popup,
423+
menu = [
424+
{ title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors" },
425+
{ title: "Copy", cmd: "copy", uiIcon: "ui-icon-copy" },
426+
{ title: "Paste", cmd: "paste", uiIcon: "ui-icon-clipboard", disabled: true }
427+
];
428+
429+
expect(9);
430+
431+
$("#container").contextmenu({
432+
delegate: ".hasmenu",
433+
menu: menu,
434+
beforeOpen: function(event, ui) {
435+
log("beforeOpen");
436+
$ctx
437+
.contextmenu("setEntry", "cut", "Cut - changed")
438+
.contextmenu("setEntry", "copy", { title: "Copy - changed", cmd: "copy2" })
439+
.contextmenu("setEntry", "paste", {
440+
title: "Paste - changed", cmd: "paste",
441+
children: [
442+
{ title: "Sub 1", cmd: "sub_1" },
443+
{ title: "Sub 2", cmd: "sub_2", disabled: true }
444+
]
445+
} );
446+
},
447+
open: function(event) {
448+
log("open");
449+
equal(entryTitle($popup, "cut"), "Cut - changed",
450+
"setEntry(string)");
451+
equal(entry($popup, "copy").length, 0,
452+
"setEntry(object) change command id");
453+
equal(entryTitle($popup, "copy2"), "Copy - changed",
454+
"setEntry(object) set title");
455+
equal(entryTitle($popup, "paste"), "Paste - changed",
456+
"setEntry(object) set nested title");
457+
equal(entryTitle($popup, "sub_1"), "Sub 1",
458+
"setEntry(object) created nested entry");
459+
ok(entry($popup, "sub_2").hasClass("ui-state-disabled"),
460+
"setEntry(object) created nested disabled entry");
461+
// ok( entry($popup, 2).hasClass("ui-state-disabled"),
462+
// "open: Entry is disabled" );
463+
464+
// ok( $ctx.contextmenu("isOpen"),
465+
// "isOpen() true in open event");
466+
467+
// ok( entry($popup, 0).is(":visible"),
468+
// "beforeOpen: Entry 0 is visible" );
469+
// ok( entry($popup, 0).hasClass("ui-state-disabled"),
470+
// "beforeOpen: Entry 0 is disabled: enableEntry(false) worked" );
471+
472+
// ok( entry($popup, 1).is(":hidden"),
473+
// "beforeOpen: Entry 1 is hidden: showEntry(false) worked" );
474+
// ok( !entry($popup, 1).hasClass("ui-state-disabled"),
475+
// "beforeOpen: Entry 1 is enabled" );
476+
477+
setTimeout(function() {
478+
click($popup, "cut");
479+
}, 10);
480+
},
481+
select: function(event, ui) {
482+
var t = ui.item ? $(ui.item).attr("data-command") : ui.item;
483+
log("select(" + t + ")");
484+
equal( ui.cmd, "cut", "select: ui.cmd is set" );
485+
equal( ui.target.text(), "AAA", "select: ui.target is set" );
486+
},
487+
close: function(event) {
488+
log("close");
489+
}
397490
});
398491

492+
$ctx = $(":moogle-contextmenu");
493+
$popup = $ctx.contextmenu("getMenu");
494+
495+
log("open()");
496+
$ctx.contextmenu("open", $("span.hasmenu:first"));
497+
log("after open()");
498+
499+
setTimeout(function() {
500+
equal(logOutput(), "open(),beforeOpen,after open(),open,select(cut),close",
501+
"Event sequence OK.");
502+
start();
503+
}, 500);
504+
505+
});
506+
399507
});

0 commit comments

Comments
 (0)