Skip to content

Commit

Permalink
fix #67 (#69)
Browse files Browse the repository at this point in the history
* fix #67
  • Loading branch information
jameslnewell committed Jun 14, 2018
1 parent 656c3a7 commit 72c021c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
12 changes: 4 additions & 8 deletions .travis.yml
@@ -1,17 +1,13 @@
language: node_js
node_js:
- "6"
- "8"
- "10"

# https://docs.travis-ci.com/user/gui-and-headless-browsers/#Using-the-Firefox-addon-in-headless-mode
addons:
apt:
packages:
- xvfb
chrome: stable
# firefox: latest

# so chrome works
# sudo: false

# https://docs.travis-ci.com/user/gui-and-headless-browsers/
before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
Expand Down
4 changes: 4 additions & 0 deletions packages/xhr-mock/CHANGELOG.md
@@ -1,5 +1,9 @@
# Change log

# 2.4.1

* fix: check for DOM specific classes before checking for an instance of them so that environments without them e.g. `mocha` without `jsdom` mostly works

# 2.4.0

* added `once` and `delay` utility functions.
Expand Down
2 changes: 1 addition & 1 deletion packages/xhr-mock/package.json
@@ -1,6 +1,6 @@
{
"name": "xhr-mock",
"version": "2.4.0",
"version": "2.4.1-preview.1",
"description": "Utility for mocking XMLHttpRequest.",
"keywords": [
"mock",
Expand Down
19 changes: 14 additions & 5 deletions packages/xhr-mock/src/MockXMLHttpRequest.ts
Expand Up @@ -17,7 +17,6 @@ const notImplementedError = new MockError(
// implemented according to https://xhr.spec.whatwg.org/

const FORBIDDEN_METHODS = ['CONNECT', 'TRACE', 'TRACK'];
const USE_URL_SEARCH_PARAMS = typeof URLSearchParams !== 'undefined';

export enum ReadyState {
UNSENT = 0,
Expand Down Expand Up @@ -642,7 +641,11 @@ export default class MockXMLHttpRequest extends MockXMLHttpRequestEventTarget
let encoding;
let mimeType;
if (body !== null && body !== undefined) {
if (body instanceof Document) {
if (
typeof Document !== 'undefined' &&
typeof XMLDocument !== 'undefined' &&
body instanceof Document
) {
// Set encoding to `UTF-8`.
// Set mimeType to `text/html` if body is an HTML document, and to `application/xml` otherwise. Then append `;charset=UTF-8` to mimeType.
// Set request body to body, serialized, converted to Unicode, and utf-8 encoded.
Expand All @@ -654,11 +657,17 @@ export default class MockXMLHttpRequest extends MockXMLHttpRequestEventTarget
// Set request body and mimeType to the result of extracting body.
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract

if (body instanceof Blob) {
if (typeof Blob !== 'undefined' && body instanceof Blob) {
mimeType = body.type;
} else if (body instanceof FormData) {
} else if (
typeof FormData !== 'undefined' &&
body instanceof FormData
) {
mimeType = 'multipart/form-data; boundary=----XHRMockFormBoundary';
} else if (USE_URL_SEARCH_PARAMS && body instanceof URLSearchParams) {
} else if (
typeof URLSearchParams !== 'undefined' &&
body instanceof URLSearchParams
) {
encoding = 'UTF-8';
mimeType = 'application/x-www-form-urlencoded';
} else if (typeof body === 'string') {
Expand Down
8 changes: 7 additions & 1 deletion packages/xhr-mock/test/acceptance.test.ts
Expand Up @@ -50,8 +50,11 @@ describe('xhr-mock', () => {
it('when the request errored', () => {
expect.assertions(1);

mock.error(() => {
/* do nothing */
});
mock.get('/', () => {
throw new Error();
throw new Error('😵');
});

const xhr = new XMLHttpRequest();
Expand Down Expand Up @@ -274,6 +277,9 @@ describe('xhr-mock', () => {
});

it('when the request errored', () => {
mock.error(() => {
/* do nothing */
});
mock.get('/', () => Promise.reject(new Error('😵')));

const xhr = new XMLHttpRequest();
Expand Down
5 changes: 4 additions & 1 deletion packages/xhr-mock/test/howto.test.ts
Expand Up @@ -44,7 +44,10 @@ describe('how to', () => {
});

it('should simulate an error', async () => {
mock.get('/', () => Promise.reject(new Error()));
mock.error(() => {
/* do nothing */
});
mock.get('/', () => Promise.reject(new Error('😵')));

const xhr = new XMLHttpRequest();
xhr.onerror = event => console.log('error');
Expand Down

0 comments on commit 72c021c

Please sign in to comment.