Merge pull request #1554 from johnnyreilly/master

jQuery: Added missing tests / JSDoc
This commit is contained in:
John Reilly
2014-01-16 06:41:49 -08:00
2 changed files with 322 additions and 11 deletions
+223
View File
@@ -946,6 +946,17 @@ function test_removeData() {
$("span:eq(3)").text("" + $("div").data("test2"));
}
function test_jQuery_removeData() {
var div = $("div")[0];
$("span:eq(0)").text("" + $("div").data("test1"));
jQuery.data(div, "test1", "VALUE-1");
jQuery.data(div, "test2", "VALUE-2");
$("span:eq(1)").text("" + jQuery.data(div, "test1"));
jQuery.removeData(div, "test1");
$("span:eq(2)").text("" + jQuery.data(div, "test1"));
$("span:eq(3)").text("" + jQuery.data(div, "test2"));
}
function test_dblclick() {
$('#target').dblclick(function () {
alert('Handler for .dblclick() called.');
@@ -1095,6 +1106,68 @@ function test_dequeue() {
});
}
function test_queue() {
$("#show").click(function () {
var n = jQuery.queue($("div")[0], "fx");
$("span").text("Queue length is: " + n.length);
});
function runIt() {
$("div")
.show("slow")
.animate({
left: "+=200"
}, 2000)
.slideToggle(1000)
.slideToggle("fast")
.animate({
left: "-=200"
}, 1500)
.hide("slow")
.show(1200)
.slideUp("normal", runIt);
}
runIt();
$(document.body).click(function () {
var divs = $("div")
.show("slow")
.animate({ left: "+=200" }, 2000);
jQuery.queue(divs[0], "fx", function () {
$(this).addClass("newcolor");
jQuery.dequeue(this);
});
divs.animate({ left: "-=200" }, 500);
jQuery.queue(divs[0], "fx", function () {
$(this).removeClass("newcolor");
jQuery.dequeue(this);
});
divs.slideUp();
});
$("#start").click(function () {
var divs = $("div")
.show("slow")
.animate({ left: "+=200" }, 5000);
jQuery.queue(divs[0], "fx", function () {
$(this).addClass("newcolor");
jQuery.dequeue(this);
});
divs.animate({ left: "-=200" }, 1500);
jQuery.queue(divs[0], "fx", function () {
$(this).removeClass("newcolor");
jQuery.dequeue(this);
});
divs.slideUp();
});
$("#stop").click(function () {
jQuery.queue($("div")[0], "fx", []);
$("div").stop();
});
}
function test_detach() {
$("p").click(function () {
$(this).toggleClass("off");
@@ -1669,6 +1742,104 @@ function test_hasData() {
$p.append(jQuery.hasData(p) + " ");
}
function test_jQuery_proxy() {
function test1() {
var me = {
type: "zombie",
test: function (event?) {
// Without proxy, `this` would refer to the event target
// use event.target to reference that element.
var element = event.target;
$(element).css("background-color", "red");
// With proxy, `this` refers to the me object encapsulating
// this function.
$("#log").append("Hello " + this.type + "<br>");
$("#test").off("click", this.test);
}
};
var you = {
type: "person",
test: function (event?) {
$("#log").append(this.type + " ");
}
};
// Execute you.test() in the context of the `you` object
// no matter where it is called
// i.e. the `this` keyword will refer to `you`
var youClick = $.proxy(you.test, you);
// attach click handlers to #test
$("#test")
// this === "zombie"; handler unbound after first click
.on("click", $.proxy(me.test, me))
// this === "person"
.on("click", youClick)
// this === "zombie"
.on("click", $.proxy(you.test, me))
// this === "<button> element"
.on("click", you.test);
}
function test2() {
var obj = {
name: "John",
test: function () {
$("#log").append(this.name);
$("#test").off("click", obj.test);
}
};
$("#test").on("click", jQuery.proxy(obj, "test"));
}
function test3() {
var me = {
// I'm a dog
type: "dog",
// Note that event comes *after* one and two
test: function (one?, two?, event?) {
$("#log")
// `one` maps to `you`, the 1st additional
// argument in the $.proxy function call
.append("<h3>Hello " + one.type + ":</h3>")
// The `this` keyword refers to `me`
// (the 2nd, context, argument of $.proxy)
.append("I am a " + this.type + ", ")
// `two` maps to `they`, the 2nd additional
// argument in the $.proxy function call
.append("and they are " + two.type + ".<br>")
// The event type is "click"
.append("Thanks for " + event.type + "ing.")
// The clicked element is `event.target`,
// and its type is "button"
.append("the " + event.target.type + ".");
}
};
var you = { type: "cat" };
var they = { type: "fish" };
// Set up handler to execute me.test() in the context
// of `me`, with `you` and `they` as additional arguments
var proxy = $.proxy(me.test, me, you, they);
$("#test")
.on("click", proxy);
}
}
function test_height() {
$(window).height();
$(document).height();
@@ -1876,6 +2047,58 @@ function test_scrollTop() {
$("div.demo").scrollTop(300);
}
function test_param() {
function test1() {
var myObject = {
a: {
one: 1,
two: 2,
three: 3
},
b: [1, 2, 3]
};
var recursiveEncoded = $.param(myObject);
var recursiveDecoded = decodeURIComponent($.param(myObject));
alert(recursiveEncoded);
alert(recursiveDecoded);
}
function test2() {
var myObject = {
a: {
one: 1,
two: 2,
three: 3
},
b: [1, 2, 3]
};
var shallowEncoded = $.param(myObject, true);
var shallowDecoded = decodeURIComponent(shallowEncoded);
alert(shallowEncoded);
alert(shallowDecoded);
}
var params = { width: 1680, height: 1050 };
var str = jQuery.param(params);
$("#results").text(str);
// <=1.3.2:
$.param({ a: [2, 3, 4] }); // "a=2&a=3&a=4"
// >=1.4:
$.param({ a: [2, 3, 4] }); // "a[]=2&a[]=3&a[]=4"
// <=1.3.2:
$.param({ a: { b: 1, c: 2 }, d: [3, 4, { e: 5 }] });
// "a=[object+Object]&d=3&d=4&d=[object+Object]"
// >=1.4:
$.param({ a: { b: 1, c: 2 }, d: [3, 4, { e: 5 }] });
// "a[b]=1&a[c]=2&d[]=3&d[]=4&d[2][e]=5"
}
function test_position() {
var p = $("p:first");
var position = p.position();
+99 -11
View File
@@ -333,7 +333,19 @@ interface JQuerySupport {
}
interface JQueryParam {
/**
* Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.
*
* @param obj An array or object to serialize.
*/
(obj: any): string;
/**
* Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.
*
* @param obj An array or object to serialize.
* @param traditional A Boolean indicating whether to perform a traditional "shallow" serialization.
*/
(obj: any, traditional: boolean): string;
}
@@ -411,9 +423,9 @@ interface JQueryEasing {
swing(p: number): number;
}
/*
Static members of jQuery (those on $ and jQuery themselves)
*/
/**
* Static members of jQuery (those on $ and jQuery themselves)
*/
interface JQueryStatic {
/**
@@ -510,6 +522,9 @@ interface JQueryStatic {
*/
getScript(url: string, success?: (script: string, textStatus: string, jqXHR: JQueryXHR) => any): JQueryXHR;
/**
* Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.
*/
param: JQueryParam;
/**
@@ -644,6 +659,9 @@ interface JQueryStatic {
*/
when<T>(...deferreds: any[]): JQueryPromise<T>;
/**
* Hook directly into jQuery to override how particular CSS properties are retrieved or set, normalize CSS property naming, or create custom properties.
*/
cssHooks: { [key: string]: any; };
cssNumber: any;
@@ -669,24 +687,94 @@ interface JQueryStatic {
*/
data(element: Element): any;
dequeue(element: Element, queueName?: string): any;
/**
* Execute the next function on the queue for the matched element.
*
* @param element A DOM element from which to remove and execute a queued function.
* @param queueName A string containing the name of the queue. Defaults to fx, the standard effects queue.
*/
dequeue(element: Element, queueName?: string): void;
/**
* Determine whether an element has any jQuery data associated with it.
*
* @param element A DOM element to be checked for data.
*/
hasData(element: Element): boolean;
/**
* Show the queue of functions to be executed on the matched element.
*
* @param element A DOM element to inspect for an attached queue.
* @param queueName A string containing the name of the queue. Defaults to fx, the standard effects queue.
*/
queue(element: Element, queueName?: string): any[];
queue(element: Element, queueName: string, newQueueOrCallback: any): JQuery;
/**
* Manipulate the queue of functions to be executed on the matched element.
*
* @param element A DOM element where the array of queued functions is attached.
* @param queueName A string containing the name of the queue. Defaults to fx, the standard effects queue.
* @param newQueue An array of functions to replace the current queue contents.
*/
queue(element: Element, queueName: string, newQueue: Function[]): JQuery;
/**
* Manipulate the queue of functions to be executed on the matched element.
*
* @param element A DOM element on which to add a queued function.
* @param queueName A string containing the name of the queue. Defaults to fx, the standard effects queue.
* @param callback The new function to add to the queue.
*/
queue(element: Element, queueName: string, callback: Function): JQuery;
/**
* Remove a previously-stored piece of data.
*
* @param element A DOM element from which to remove data.
* @param name A string naming the piece of data to remove.
*/
removeData(element: Element, name?: string): JQuery;
// Deferred
/**
* A constructor function that returns a chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.
*
* @param beforeStart A function that is called just before the constructor returns.
*/
Deferred<T>(beforeStart?: (deferred: JQueryDeferred<T>) => any): JQueryDeferred<T>;
// Effects
fx: { tick: () => void; interval: number; stop: () => void; speeds: { slow: number; fast: number; }; off: boolean; step: any; };
/**
* Effects
*/
fx: {
tick: () => void;
/**
* The rate (in milliseconds) at which animations fire.
*/
interval: number;
stop: () => void;
speeds: { slow: number; fast: number; };
/**
* Globally disable all animations.
*/
off: boolean;
step: any;
};
// Events
proxy(fn: (...args: any[]) => any, context: any, ...args: any[]): any;
proxy(context: any, name: string, ...args: any[]): any;
/**
* Takes a function and returns a new one that will always have a particular context.
*
* @param fnction The function whose context will be changed.
* @param context The object to which the context (this) of the function should be set.
* @param additionalArguments Any number of arguments to be passed to the function referenced in the function argument.
*/
proxy(fnction: (...args: any[]) => any, context: Object, ...additionalArguments: any[]): any;
/**
* Takes a function and returns a new one that will always have a particular context.
*
* @param context The object to which the context (this) of the function should be set.
* @param name The name of the function whose context will be changed (should be a property of the context object).
* @param additionalArguments Any number of arguments to be passed to the function named in the name argument.
*/
proxy(context: Object, name: string, ...additionalArguments: any[]): any;
Event: JQueryEventConstructor;