From a3ef3077dd0861734b18f1dc08f9feeae43b267a Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sun, 18 Jun 2017 16:26:05 -0400 Subject: [PATCH] [jquery] Fixed signatures on Ajax-related methods on JQuery. Added tests for Ajax-related methods on JQuery. --- types/jquery/index.d.ts | 12 +-- types/jquery/jquery-tests.ts | 138 ++++++++++++++++++++++++++++++++++- 2 files changed, 143 insertions(+), 7 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index c1e7a6bac7..2d5543cb3a 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -129,7 +129,7 @@ interface JQuery { * @see {@link https://api.jquery.com/ajaxComplete/} * @since 1.0 */ - ajaxComplete(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings) => void | false): this; + ajaxComplete(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings) => void | false): this; /** * Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event. * @@ -137,7 +137,7 @@ interface JQuery { * @see {@link https://api.jquery.com/ajaxError/} * @since 1.0 */ - ajaxError(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxSettings: JQuery.AjaxSettings, thrownError: string) => void | false): this; + ajaxError(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxSettings: JQuery.AjaxSettings, thrownError: string) => void | false): this; /** * Attach a function to be executed before an Ajax request is sent. This is an Ajax Event. * @@ -145,7 +145,7 @@ interface JQuery { * @see {@link https://api.jquery.com/ajaxSend/} * @since 1.0 */ - ajaxSend(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings) => void | false): this; + ajaxSend(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings) => void | false): this; /** * Register a handler to be called when the first Ajax request begins. This is an Ajax Event. * @@ -169,7 +169,7 @@ interface JQuery { * @see {@link https://api.jquery.com/ajaxSuccess/} * @since 1.0 */ - ajaxSuccess(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings, data: JQuery.PlainObject) => void | false): this; + ajaxSuccess(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings, data: JQuery.PlainObject) => void | false): this; /** * Perform a custom animation of a set of CSS properties. * @@ -1147,7 +1147,7 @@ interface JQuery { */ load(url: string, data: string | JQuery.PlainObject, - complete: (this: TElement, responseText: string, textStatus: JQuery.Ajax.SuccessTextStatus, jqXHR: JQuery.jqXHR) => void): this; + complete: (this: TElement, responseText: string, textStatus: JQuery.Ajax.TextStatus, jqXHR: JQuery.jqXHR) => void): this; /** * Load data from the server and place the returned HTML into the matched element. * @@ -1158,7 +1158,7 @@ interface JQuery { * @since 1.0 */ load(url: string, - complete_data?: ((this: TElement, responseText: string, textStatus: JQuery.Ajax.SuccessTextStatus, jqXHR: JQuery.jqXHR) => void) | string | JQuery.PlainObject): this; + complete_data?: ((this: TElement, responseText: string, textStatus: JQuery.Ajax.TextStatus, jqXHR: JQuery.jqXHR) => void) | string | JQuery.PlainObject): this; /** * Pass each element in the current matched set through a function, producing a new jQuery object * containing the return values. diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 09468a1c55..c76d26ad8e 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -9,6 +9,140 @@ function JQuery() { $('div')[0] === new HTMLElement(); } + function ajax() { + function ajaxComplete() { + // $ExpectType JQuery + $(document).ajaxComplete(function(event, jqXHR, ajaxOptions) { + // $ExpectType Document + this; + // $ExpectType Event + event; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettings + ajaxOptions; + + return false; + }); + } + + function ajaxError() { + // $ExpectType JQuery + $(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) { + // $ExpectType Document + this; + // $ExpectType Event + event; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettings + ajaxSettings; + // $ExpectType string + thrownError; + + return false; + }); + } + + function ajaxSend() { + // $ExpectType JQuery + $(document).ajaxSend(function(event, jqXHR, ajaxOptions) { + // $ExpectType Document + this; + // $ExpectType Event + event; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettings + ajaxOptions; + + return false; + }); + } + + function ajaxStart() { + // $ExpectType JQuery + $(document).ajaxStart(function() { + // $ExpectType Document + this; + + return false; + }); + } + + function ajaxStop() { + // $ExpectType JQuery + $(document).ajaxStop(function() { + // $ExpectType Document + this; + + return false; + }); + } + + function ajaxSuccess() { + // $ExpectType JQuery + $(document).ajaxSuccess(function(event, jqXHR, ajaxOptions, data) { + // $ExpectType Document + this; + // $ExpectType Event + event; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettings + ajaxOptions; + // $ExpectType PlainObject + data; + + return false; + }); + } + + function load() { + // $ExpectType JQuery + $('#result').load('/echo/html/', 'data', function(responseText, textStatus, jqXHR) { + // $ExpectType HTMLElement + this; + // $ExpectType string + responseText; + // $ExpectType TextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType JQuery + $('#result').load('/echo/html/', { data: 'data' }, function(responseText, textStatus, jqXHR) { + // $ExpectType HTMLElement + this; + // $ExpectType string + responseText; + // $ExpectType TextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType JQuery + $('#result').load('/echo/html/', function(responseText, textStatus, jqXHR) { + // $ExpectType HTMLElement + this; + // $ExpectType string + responseText; + // $ExpectType TextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType JQuery + $('#result').load('/echo/html/', 'data'); + + // $ExpectType JQuery + $('#result').load('/echo/html/', { data: 'data' }); + } + } + function on() { function false_handler_shorthand() { $().on('events', false); @@ -184,7 +318,9 @@ function jqXHR() { $.ajax('/echo').fail((jqXHR, textStatus, errorThrown) => { // $ExpectType jqXHR jqXHR; - // $ExpectType "abort" | "timeout" | "error" | "parsererror" | null + // This test is flaky + // Should be 'ErrorTextStatus | null' and should be able to handle it out of order + // $ExpectType "timeout" | "error" | "abort" | "parsererror" | null textStatus; // $ExpectType string errorThrown;