From 762c57262d4ffe294060f828b4671b0c8540416b Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lachance Date: Tue, 6 Sep 2016 18:29:33 -0400 Subject: [PATCH 1/2] + Add the missing optional callback for the MockJaxSettings.response function --- jquery-mockjax/jquery-mockjax-tests.ts | 48 ++++++++++++++++++++------ jquery-mockjax/jquery-mockjax.d.ts | 2 +- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/jquery-mockjax/jquery-mockjax-tests.ts b/jquery-mockjax/jquery-mockjax-tests.ts index 6fae52eb07..55e77cd2bb 100644 --- a/jquery-mockjax/jquery-mockjax-tests.ts +++ b/jquery-mockjax/jquery-mockjax-tests.ts @@ -9,7 +9,7 @@ class Tests { run(): void { const self = this; - var t = QUnit.test; + let t = QUnit.test; QUnit.begin(() => { @@ -36,7 +36,7 @@ class Tests { responseText: 'Hello Word' }); - var xhr = $.ajax({ + let xhr = $.ajax({ url: '/xmlhttprequest', complete: () => { } }); @@ -67,7 +67,7 @@ class Tests { }); t('Intercept asynchronized proxy calls', (assert) => { - var done = assert.async(); + let done = assert.async(); $.mockjax({ url: '/proxy', proxy: 'test_proxy.json' @@ -85,7 +85,7 @@ class Tests { }); t('Intercept and proxy (sub-ajax request)', (assert) => { - var done = assert.async(); + let done = assert.async(); $.mockjax({ url: '/proxy', @@ -104,7 +104,7 @@ class Tests { }); t('Proxy type specification', (assert) => { - var done = assert.async(); + let done = assert.async(); $.mockjax({ url: '/proxy', @@ -124,7 +124,7 @@ class Tests { }); t('Support 1.5 $.ajax(url, settings) signature.', (assert) => { - var done = assert.async(); + let done = assert.async(); $.mockjax({ url: '/resource', @@ -141,9 +141,9 @@ class Tests { }); t('Dynamic response callback', (assert) => { - var done = assert.async(); + let done = assert.async(); - var settings: MockJaxSettings = { + let settings: MockJaxSettings = { url: '/response-callback', response: (settings) => { settings.responseText = settings.data.response + ' 2'; @@ -165,8 +165,36 @@ class Tests { } }); }); + + t('Asyncronous response callback', (assert) => { + let done = assert.async(); + + let settings: MockJaxSettings = { + url: '/async-response-callback', + response: (settings, callback) => { + setTimeout(() => { + settings.responseText = settings.data.response + ' 3'; + callback(); + }, 10); + } + }; + + $.mockjax(settings); + + $.ajax({ + url: '/async-response-callback', + dataType: 'text', + data: { + response: 'Hello world' + }, + error: self._noErrorCallbackExpected, + complete: (xhr) => { + assert.equal(xhr.responseText, 'Hello world 3', 'Response Text matches'); + done(); + } + }); + }); } } -var tests = new Tests(); -tests.run(); +new Tests().run(); diff --git a/jquery-mockjax/jquery-mockjax.d.ts b/jquery-mockjax/jquery-mockjax.d.ts index 7505a30e6f..8a7c64d815 100644 --- a/jquery-mockjax/jquery-mockjax.d.ts +++ b/jquery-mockjax/jquery-mockjax.d.ts @@ -22,7 +22,7 @@ interface MockJaxSettings { isTimeout?: boolean; dataType?: string; contentType?: string; - response?: (settings: any) => void; + response?: (settings: any, callback?: Function) => void; responseText?: string | Object; responseXml?: string; proxy?: string; From 0bc8dbd7d90b2b7ad2de5bf43bf607cb6e0a9fbe Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lachance Date: Tue, 6 Sep 2016 18:41:35 -0400 Subject: [PATCH 2/2] * Rename the parameter to fit with the doc (https://github.com/jakerella/jquery-mockjax/#callback) --- jquery-mockjax/jquery-mockjax-tests.ts | 4 ++-- jquery-mockjax/jquery-mockjax.d.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/jquery-mockjax/jquery-mockjax-tests.ts b/jquery-mockjax/jquery-mockjax-tests.ts index 55e77cd2bb..36ebd7e64c 100644 --- a/jquery-mockjax/jquery-mockjax-tests.ts +++ b/jquery-mockjax/jquery-mockjax-tests.ts @@ -171,10 +171,10 @@ class Tests { let settings: MockJaxSettings = { url: '/async-response-callback', - response: (settings, callback) => { + response: (settings, completed) => { setTimeout(() => { settings.responseText = settings.data.response + ' 3'; - callback(); + completed(); }, 10); } }; diff --git a/jquery-mockjax/jquery-mockjax.d.ts b/jquery-mockjax/jquery-mockjax.d.ts index 8a7c64d815..c054f33317 100644 --- a/jquery-mockjax/jquery-mockjax.d.ts +++ b/jquery-mockjax/jquery-mockjax.d.ts @@ -22,7 +22,7 @@ interface MockJaxSettings { isTimeout?: boolean; dataType?: string; contentType?: string; - response?: (settings: any, callback?: Function) => void; + response?: (settings: any, done?: Function) => void; responseText?: string | Object; responseXml?: string; proxy?: string;