Skip to content

Commit

Permalink
Add MockXhr argument to the onSend hook (#45)
Browse files Browse the repository at this point in the history
* Remove useless Promise resolution value

* Add MockXhr argument to the onSend hook.

Fixes #42
  • Loading branch information
berniegp committed Apr 12, 2023
1 parent a466218 commit ee66117
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
4 changes: 2 additions & 2 deletions src/MockXhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/FactoriesTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
4 changes: 2 additions & 2 deletions test/MockXhrServerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Expand Down Expand Up @@ -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], {
Expand Down
26 changes: 13 additions & 13 deletions test/MockXhrTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
};
});
Expand All @@ -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;
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit ee66117

Please sign in to comment.