From ee6611789b2048bdc940f2ed51ca6a72556c694d Mon Sep 17 00:00:00 2001 From: Bertrand Guay-Paquet Date: Wed, 12 Apr 2023 23:54:54 +0200 Subject: [PATCH] Add MockXhr argument to the onSend hook (#45) * Remove useless Promise resolution value * Add MockXhr argument to the onSend hook. Fixes #42 --- README.md | 1 + src/MockXhr.ts | 4 ++-- test/FactoriesTest.ts | 2 +- test/MockXhrServerTest.ts | 4 ++-- test/MockXhrTest.ts | 26 +++++++++++++------------- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9209c44..47236c2 100644 --- a/README.md +++ b/README.md @@ -384,6 +384,7 @@ MockXhrSubclass.onCreate = (xhr) => { /*...*/ }; ##### `onSend` Callback method that receives these arguments: - `request`: [`MockXhrRequest`](#mockxhrrequest-class) for the request. +- `xhr`: The `MockXhr` instance. Use this lifecycle hook to respond to a request with the [mock response methods](#mock-response-methods). diff --git a/src/MockXhr.ts b/src/MockXhr.ts index 071d08c..95e4d5c 100644 --- a/src/MockXhr.ts +++ b/src/MockXhr.ts @@ -19,7 +19,7 @@ interface MockXhrResponse { export type OnCreateCallback = (xhr: MockXhr) => void; -export type OnSendCallback = (this: MockXhrRequest, request: MockXhrRequest) => void; +export type OnSendCallback = (this: MockXhrRequest, request: MockXhrRequest, xhr: MockXhr) => void; const RESPONSE_TYPES = ['', 'arraybuffer', 'blob', 'document', 'json', 'text']; @@ -856,7 +856,7 @@ export default class MockXhr // Saves the callback and request data in case they change before then() executes if (onSend) { const request = this._currentRequest as MockXhrRequest; - Promise.resolve(true).then(() => onSend.call(request, request)); + Promise.resolve().then(() => onSend.call(request, request, this)); } } diff --git a/test/FactoriesTest.ts b/test/FactoriesTest.ts index f67d1c6..40b3c29 100644 --- a/test/FactoriesTest.ts +++ b/test/FactoriesTest.ts @@ -51,7 +51,7 @@ describe('Factories', () => { xhr.open('GET', '/url'); xhr.send(); - return Promise.resolve(true).then(() => { + return Promise.resolve().then(() => { assert.strictEqual(onSend1Count, 0, 'onSend() from first mock not called'); assert.strictEqual(onSend2Count, 1, 'onSend() from second mock called'); }); diff --git a/test/MockXhrServerTest.ts b/test/MockXhrServerTest.ts index 6dde6c3..60fe514 100644 --- a/test/MockXhrServerTest.ts +++ b/test/MockXhrServerTest.ts @@ -335,7 +335,7 @@ describe('MockXhrServer', () => { const xhr = doRequest('method', '/path'); - return Promise.resolve(true).then(() => { + return Promise.resolve().then(() => { assert.strictEqual(xhr.readyState, MockXhr.OPENED, 'final state UNSENT'); }); }); @@ -713,7 +713,7 @@ describe('MockXhrServer', () => { doRequest('GET', '/path2'); doRequest('POST', '/post', { header: '123' }, 12345); - return Promise.resolve(true).then(() => { + return Promise.resolve().then(() => { const log = server.getRequestLog(); assert.strictEqual(log.length, 3, 'handler called'); assert.deepEqual(log[0], { diff --git a/test/MockXhrTest.ts b/test/MockXhrTest.ts index bb01b62..1b57d22 100644 --- a/test/MockXhrTest.ts +++ b/test/MockXhrTest.ts @@ -350,7 +350,7 @@ describe('MockXhr', () => { xhr.open('GET', '/url'); xhr.send(); xhr.timeout = 1; - Promise.resolve(true).then(() => { xhr.timeout = 0; }); + Promise.resolve().then(() => { xhr.timeout = 0; }); // Wait to make sure the timeout has no effect setTimeout(() => { @@ -591,7 +591,7 @@ describe('MockXhr', () => { }); xhr.send(); - return Promise.resolve(true).then(() => { + return Promise.resolve().then(() => { assert.strictEqual(xhr.readyState, MockXhr.OPENED, 'final state OPENED'); assert.strictEqual(onSendCount, 0, 'onSend() should not be called'); assert.strictEqual(onSendXhrCount, 0, 'onSend() should not be called'); @@ -729,7 +729,7 @@ describe('MockXhr', () => { }); xhr.send(); - return Promise.resolve(true).then(() => { + return Promise.resolve().then(() => { assert.isFalse(onSendCalled, 'onSend() should not be called'); assert.isFalse(onSendXhrCalled, 'onSend() should not be called'); assert.strictEqual(xhr.readyState, MockXhr.UNSENT, 'final state UNSENT'); @@ -1073,26 +1073,26 @@ describe('MockXhr', () => { class LocalMockXhr extends MockXhr {} const xhr = new LocalMockXhr(); const calls: string[] = []; - const thisValues: MockXhrRequest[] = []; - const args: MockXhrRequest[] = []; + const thisValues: any[] = []; + const argValues: any[] = []; const done = new Promise((resolve) => { - MockXhr.onSend = function onSend(arg) { + MockXhr.onSend = function onSend(...args) { calls.push('global'); thisValues.push(this); - args.push(arg); + argValues.push(args); }; - LocalMockXhr.onSend = function onSendLocal(arg) { + LocalMockXhr.onSend = function onSendLocal(...args) { calls.push('subclass'); thisValues.push(this); - args.push(arg); + argValues.push(args); }; - xhr.onSend = function onSendXhr(arg) { + xhr.onSend = function onSendXhr(...args) { calls.push('xhr'); thisValues.push(this); - args.push(arg); + argValues.push(args); resolve(true); }; }); @@ -1104,7 +1104,7 @@ describe('MockXhr', () => { assert.instanceOf(req, MockXhrRequest); assert.deepEqual(calls, ['global', 'subclass', 'xhr'], 'hooks called in the right order'); assert.deepEqual(thisValues, [req, req, req], 'correct contexts for callbacks'); - assert.deepEqual(args, [req, req, req], 'correct parameters for callbacks'); + assert.deepEqual(argValues, [[req, xhr], [req, xhr], [req, xhr]], 'correct parameters for callbacks'); }); } finally { delete MockXhr.onSend; @@ -1179,7 +1179,7 @@ describe('MockXhr', () => { xhr.setRequestHeader('header2', 'val2'); xhr.send({ body: 1 }); - return Promise.resolve(true).then(() => { + return Promise.resolve().then(() => { assertSameRequest(requests[0], new RequestData( new HeadersContainer().addHeader('header1', 'val1'), 'GET',