Skip to content

Commit

Permalink
de-type body to allow use of ArrayBuffer, Blob or something else
Browse files Browse the repository at this point in the history
  • Loading branch information
James Newell committed Feb 20, 2018
1 parent 3d9df5c commit a7c6250
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 153 deletions.
4 changes: 4 additions & 0 deletions .prettierrc.js
@@ -0,0 +1,4 @@
module.exports = {
printWidth: 120,
singleQuote: true
};
26 changes: 26 additions & 0 deletions packages/xhr-mock-tests/src/native.test.ts
@@ -0,0 +1,26 @@
import {expect} from 'chai';
import mock from 'xhr-mock';

describe('native', () => {
beforeEach(() => mock.setup());
afterEach(() => mock.teardown());

it('should get a blob', done => {

This comment has been minimized.

Copy link
@jameslnewell

jameslnewell Feb 20, 2018

Owner

this should read arraybuffer instead of blob

mock.get('/myfile.png', {
body: new ArrayBuffer(0)
});

// sourced from https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data
const req = new XMLHttpRequest();
req.open('GET', '/myfile.png', true);
req.responseType = 'arraybuffer';

req.onload = function(oEvent) {
const arrayBuffer = req.response; // Note: not oReq.responseText
expect(arrayBuffer).to.be.an('ArrayBuffer');
done();
};

req.send(null);
});
});
4 changes: 4 additions & 0 deletions packages/xhr-mock/CHANGELOG.md
@@ -1,5 +1,9 @@
# Change log

# 2.2.0

* added "support" for `responseType` of `arraybuffer`, `blob` and `document` by returning whatever object `res.body(body)` is set to

## 2.1.0

* added support for `responseType="json"`
Expand Down
2 changes: 1 addition & 1 deletion packages/xhr-mock/package.json
@@ -1,6 +1,6 @@
{
"name": "xhr-mock",
"version": "2.1.0",
"version": "2.2.0",
"description": "Utility for mocking XMLHttpRequest.",
"keywords": [
"mock",
Expand Down
8 changes: 4 additions & 4 deletions packages/xhr-mock/src/MockResponse.ts
Expand Up @@ -6,7 +6,7 @@ export default class MockResponse {
private _status: number = 200;
private _reason: string = 'OK';
private _headers: MockHeaders = {};
private _body: null | string = null;
private _body: any = null;

status(): number;
status(status: number): MockResponse;
Expand Down Expand Up @@ -69,9 +69,9 @@ export default class MockResponse {
}
}

body(): null | string;
body(body: null | string): MockResponse;
body(body?: null | string): null | string | MockResponse {
body(): any;
body(body: any): MockResponse;
body(body?: any): any | MockResponse {
if (typeof body !== 'undefined') {
this._body = body;
return this;
Expand Down
27 changes: 27 additions & 0 deletions packages/xhr-mock/src/MockXMLHttpRequest.test.ts
Expand Up @@ -100,6 +100,33 @@ describe('MockXMLHttpRequest', () => {
xhr.open('get', '/');
xhr.send();
});

it('should return null when the type is other and the request is not done', () => {
const fakeBuffer = {};
MockXMLHttpRequest.addHandler((req, res) => res.body(fakeBuffer));
const xhr = new MockXMLHttpRequest();
xhr.responseType = 'blob';
xhr.open('get', '/');
expect(xhr.response).toEqual(null);
});

it('should return an object when the type is other and the request is done', done => {
const fakeBuffer = {};
MockXMLHttpRequest.addHandler((req, res) => res.body(fakeBuffer));
const xhr = new MockXMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = () => {
try {
expect(xhr.response).toBe(fakeBuffer);
done();
} catch (error) {
done.fail(error);
}
};
xhr.onerror = failOnEvent(done);
xhr.open('get', '/');
xhr.send();
});
});

describe('.setRequestHeader()', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/xhr-mock/src/MockXMLHttpRequest.ts
Expand Up @@ -156,7 +156,8 @@ export default class MockXMLHttpRequest extends MockXMLHttpRequestEventTarget
}
}

throw notImplementedError;
// rely on the mock to do the right thing with an arraybuffer, blob or document
return body;
}

get responseText(): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/xhr-mock/src/MockXMLHttpRequestEventTarget.ts
Expand Up @@ -6,7 +6,7 @@ export default class MockXMLHttpRequestEventTarget extends MockEventTarget
implements XMLHttpRequestEventTarget {
onabort: (this: XMLHttpRequestEventTarget, ev: ProgressEvent) => any;
// @ts-ignore: https://github.com/jameslnewell/xhr-mock/issues/45
onerror: (this: XMLHttpRequestEventTarget, ev: ProgressEvent) => any;
onerror: (this: XMLHttpRequestEventTarget, ev: ErrorEvent) => any;
onload: (this: XMLHttpRequestEventTarget, ev: ProgressEvent) => any;
onloadend: (this: XMLHttpRequestEventTarget, ev: ProgressEvent) => any;
onloadstart: (this: XMLHttpRequestEventTarget, ev: ProgressEvent) => any;
Expand Down
2 changes: 1 addition & 1 deletion packages/xhr-mock/src/types.ts
Expand Up @@ -10,7 +10,7 @@ export type MockObject = {
status?: number;
reason?: string;
headers?: MockHeaders;
body?: string;
body?: any;
};

export type MockFunction = (
Expand Down

0 comments on commit a7c6250

Please sign in to comment.