From dcbb35226245483930a1f2969927a3b348bb2558 Mon Sep 17 00:00:00 2001 From: Matt Zabriskie Date: Mon, 7 Mar 2016 13:21:27 -0700 Subject: [PATCH] Moving test helpers and auto-loading them --- karma.conf.js | 4 +- test/specs/__getAjaxRequest.js | 28 ------ test/specs/__helpers.js | 88 +++++++++++++++++++ test/specs/__setupBasicAuthTest.js | 42 --------- test/specs/__validateInvalidCharacterError.js | 3 - test/specs/api.spec.js | 2 - test/specs/basicAuth.spec.js | 2 - test/specs/basicAuthWithPolyfill.spec.js | 1 - test/specs/defaults.spec.js | 6 +- test/specs/headers.spec.js | 3 - test/specs/helpers/btoa.spec.js | 19 ++-- test/specs/instance.spec.js | 3 - test/specs/interceptors.spec.js | 3 - test/specs/options.spec.js | 3 - test/specs/promise.spec.js | 4 - test/specs/requests.spec.js | 21 ++++- test/specs/transform.spec.js | 3 - test/specs/utils/isX.spec.js | 27 +++++- test/specs/xsrf.spec.js | 3 - 19 files changed, 141 insertions(+), 124 deletions(-) delete mode 100644 test/specs/__getAjaxRequest.js create mode 100644 test/specs/__helpers.js delete mode 100644 test/specs/__setupBasicAuthTest.js delete mode 100644 test/specs/__validateInvalidCharacterError.js diff --git a/karma.conf.js b/karma.conf.js index 7a15530..98f7485 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -111,7 +111,8 @@ module.exports = function(config) { // list of files / patterns to load in the browser files: [ - 'test/specs/**/*.spec.js' + 'test/specs/__helpers.js', + 'test/specs/**/*.spec.js', ], @@ -124,6 +125,7 @@ module.exports = function(config) { // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { + 'test/specs/__helpers.js': ['webpack', 'sourcemap'], 'test/specs/**/*.spec.js': ['webpack', 'sourcemap'] }, diff --git a/test/specs/__getAjaxRequest.js b/test/specs/__getAjaxRequest.js deleted file mode 100644 index f6a5753..0000000 --- a/test/specs/__getAjaxRequest.js +++ /dev/null @@ -1,28 +0,0 @@ -var attempts = 0; -var MAX_ATTEMPTS = 5; -var ATTEMPT_DELAY_FACTOR = 5; - -module.exports = function getAjaxRequest() { - return new Promise(function (resolve, reject) { - attempts = 0; - attemptGettingAjaxRequest(resolve, reject); - }); -} - -function attemptGettingAjaxRequest(resolve, reject) { - var delay = attempts * attempts * ATTEMPT_DELAY_FACTOR; - - if (attempts++ > MAX_ATTEMPTS) { - reject(new Error('No request was found')); - return; - } - - setTimeout(function () { - var request = jasmine.Ajax.requests.mostRecent(); - if (request) { - resolve(request); - } else { - attemptGettingAjaxRequest(resolve, reject); - } - }, delay); -} diff --git a/test/specs/__helpers.js b/test/specs/__helpers.js new file mode 100644 index 0000000..80ecc74 --- /dev/null +++ b/test/specs/__helpers.js @@ -0,0 +1,88 @@ +// Polyfill ES6 Promise +require('es6-promise').polyfill(); + +// Import axios +axios = require('../../index'); + +// Is this an old version of IE that lacks standard objects like DataView, ArrayBuffer, FormData, etc. +isOldIE = /MSIE (8|9)\.0/.test(navigator.userAgent); + +// Get Ajax request using an increasing timeout to retry +getAjaxRequest = (function () { +var attempts = 0; +var MAX_ATTEMPTS = 5; +var ATTEMPT_DELAY_FACTOR = 5; + +function getAjaxRequest() { + return new Promise(function (resolve, reject) { + attempts = 0; + attemptGettingAjaxRequest(resolve, reject); + }); +} + +function attemptGettingAjaxRequest(resolve, reject) { + var delay = attempts * attempts * ATTEMPT_DELAY_FACTOR; + + if (attempts++ > MAX_ATTEMPTS) { + reject(new Error('No request was found')); + return; + } + + setTimeout(function () { + var request = jasmine.Ajax.requests.mostRecent(); + if (request) { + resolve(request); + } else { + attemptGettingAjaxRequest(resolve, reject); + } + }, delay); +} + +return getAjaxRequest; +})(); + +// Validate an invalid character error +validateInvalidCharacterError = function validateInvalidCharacterError(error) { + expect(/character/i.test(error.message)).toEqual(true); +}; + +// Setup basic auth tests +setupBasicAuthTest = function setupBasicAuthTest() { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + }); + + it('should accept HTTP Basic auth with username/password', function (done) { + axios('/foo', { + auth: { + username: 'Aladdin', + password: 'open sesame' + } + }); + + setTimeout(function () { + var request = jasmine.Ajax.requests.mostRecent(); + + expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='); + done(); + }, 0); + }); + + it('should fail to encode HTTP Basic auth credentials with non-Latin1 characters', function (done) { + axios('/foo', { + auth: { + username: 'Aladßç£☃din', + password: 'open sesame' + } + }).then(function(response) { + done(new Error('Should not succeed to make a HTTP Basic auth request with non-latin1 chars in credentials.')); + }).catch(function(error) { + validateInvalidCharacterError(error); + done(); + }); + }); +}; diff --git a/test/specs/__setupBasicAuthTest.js b/test/specs/__setupBasicAuthTest.js deleted file mode 100644 index acc3699..0000000 --- a/test/specs/__setupBasicAuthTest.js +++ /dev/null @@ -1,42 +0,0 @@ -var axios = require('../../index'); -var validateInvalidCharacterError = require('./__validateInvalidCharacterError'); - -module.exports = function setupBasicAuthTest() { - beforeEach(function () { - jasmine.Ajax.install(); - }); - - afterEach(function () { - jasmine.Ajax.uninstall(); - }); - - it('should accept HTTP Basic auth with username/password', function (done) { - axios('/foo', { - auth: { - username: 'Aladdin', - password: 'open sesame' - } - }); - - setTimeout(function () { - var request = jasmine.Ajax.requests.mostRecent(); - - expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='); - done(); - }, 0); - }); - - it('should fail to encode HTTP Basic auth credentials with non-Latin1 characters', function (done) { - axios('/foo', { - auth: { - username: 'Aladßç£☃din', - password: 'open sesame' - } - }).then(function(response) { - done(new Error('Should not succeed to make a HTTP Basic auth request with non-latin1 chars in credentials.')); - }).catch(function(error) { - validateInvalidCharacterError(error); - done(); - }); - }); -}; diff --git a/test/specs/__validateInvalidCharacterError.js b/test/specs/__validateInvalidCharacterError.js deleted file mode 100644 index e34243a..0000000 --- a/test/specs/__validateInvalidCharacterError.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = function validateInvalidCharacterError(error) { - expect(/character/i.test(error.message)).toEqual(true); -}; diff --git a/test/specs/api.spec.js b/test/specs/api.spec.js index 583b2bf..48a77df 100644 --- a/test/specs/api.spec.js +++ b/test/specs/api.spec.js @@ -1,5 +1,3 @@ -var axios = require('../../index'); - describe('static api', function () { it('should have request method helpers', function () { expect(typeof axios.get).toEqual('function'); diff --git a/test/specs/basicAuth.spec.js b/test/specs/basicAuth.spec.js index 742fc25..676dcf6 100644 --- a/test/specs/basicAuth.spec.js +++ b/test/specs/basicAuth.spec.js @@ -1,5 +1,3 @@ -var setupBasicAuthTest = require('./__setupBasicAuthTest'); - describe('basicAuth without btoa polyfill', function () { setupBasicAuthTest(); }); diff --git a/test/specs/basicAuthWithPolyfill.spec.js b/test/specs/basicAuthWithPolyfill.spec.js index 13de4c8..980d539 100644 --- a/test/specs/basicAuthWithPolyfill.spec.js +++ b/test/specs/basicAuthWithPolyfill.spec.js @@ -1,4 +1,3 @@ -var setupBasicAuthTest = require('./__setupBasicAuthTest'); var window_btoa; describe('basicAuth with btoa polyfill', function () { diff --git a/test/specs/defaults.spec.js b/test/specs/defaults.spec.js index 3aa1008..933386c 100644 --- a/test/specs/defaults.spec.js +++ b/test/specs/defaults.spec.js @@ -1,20 +1,16 @@ -var axios = require('../../index'); var defaults = require('../../lib/defaults'); var utils = require('../../lib/utils'); -var getAjaxRequest = require('./__getAjaxRequest'); describe('defaults', function () { - var __defaults; var XSRF_COOKIE_NAME = 'CUSTOM-XSRF-TOKEN'; beforeEach(function () { jasmine.Ajax.install(); - __defaults = axios.defaults; }); afterEach(function () { jasmine.Ajax.uninstall(); - axios.defaults = __defaults; + delete axios.defaults.baseURL; document.cookie = XSRF_COOKIE_NAME + '=;expires=' + new Date(Date.now() - 86400000).toGMTString(); }); diff --git a/test/specs/headers.spec.js b/test/specs/headers.spec.js index 524f22a..5a8b1a8 100644 --- a/test/specs/headers.spec.js +++ b/test/specs/headers.spec.js @@ -1,6 +1,3 @@ -var axios = require('../../index'); -var getAjaxRequest = require('./__getAjaxRequest'); - function testHeaderValue(headers, key, val) { var found = false; diff --git a/test/specs/helpers/btoa.spec.js b/test/specs/helpers/btoa.spec.js index 79b76b2..b55a69a 100644 --- a/test/specs/helpers/btoa.spec.js +++ b/test/specs/helpers/btoa.spec.js @@ -1,29 +1,26 @@ var __btoa = require('../../../lib/helpers/btoa'); -var validateInvalidCharacterError = require('../__validateInvalidCharacterError'); describe('btoa polyfill', function () { it('should behave the same as native window.btoa', function () { + // btoa doesn't exist in IE8/9 + if (isOldIE && typeof Int8Array === 'undefined') { + return; + } + var data = 'Hello, world'; expect(__btoa(data)).toEqual(window.btoa(data)); }); it('should throw an error if char is out of range 0xFF', function () { - var err1, err2; + var err; var data = 'I ♡ Unicode!'; - try { - window.btoa(data); - } catch (e) { - err1 = e; - } - try { __btoa(data); } catch (e) { - err2 = e; + err = e; } - validateInvalidCharacterError(err1); - validateInvalidCharacterError(err2); + validateInvalidCharacterError(err); }); }); diff --git a/test/specs/instance.spec.js b/test/specs/instance.spec.js index 1541227..3885183 100644 --- a/test/specs/instance.spec.js +++ b/test/specs/instance.spec.js @@ -1,6 +1,3 @@ -var axios = require('../../index'); -var getAjaxRequest = require('./__getAjaxRequest'); - describe('instance', function () { beforeEach(function () { jasmine.Ajax.install(); diff --git a/test/specs/interceptors.spec.js b/test/specs/interceptors.spec.js index de0e69b..fdc58bc 100644 --- a/test/specs/interceptors.spec.js +++ b/test/specs/interceptors.spec.js @@ -1,6 +1,3 @@ -var axios = require('../../index'); -var getAjaxRequest = require('./__getAjaxRequest'); - describe('interceptors', function () { beforeEach(function () { jasmine.Ajax.install(); diff --git a/test/specs/options.spec.js b/test/specs/options.spec.js index e4db490..6520b8a 100644 --- a/test/specs/options.spec.js +++ b/test/specs/options.spec.js @@ -1,6 +1,3 @@ -var axios = require('../../index'); -var getAjaxRequest = require('./__getAjaxRequest'); - describe('options', function () { beforeEach(function () { jasmine.Ajax.install(); diff --git a/test/specs/promise.spec.js b/test/specs/promise.spec.js index da12c03..54cedd6 100644 --- a/test/specs/promise.spec.js +++ b/test/specs/promise.spec.js @@ -1,7 +1,3 @@ -require('es6-promise').polyfill(); -var axios = require('../../index'); -var getAjaxRequest = require('./__getAjaxRequest'); - describe('promise', function () { beforeEach(function () { jasmine.Ajax.install(); diff --git a/test/specs/requests.spec.js b/test/specs/requests.spec.js index 6c42363..28da0c6 100644 --- a/test/specs/requests.spec.js +++ b/test/specs/requests.spec.js @@ -1,6 +1,3 @@ -var axios = require('../../index'); -var getAjaxRequest = require('./__getAjaxRequest'); - describe('requests', function () { beforeEach(function () { jasmine.Ajax.install(); @@ -154,6 +151,12 @@ describe('requests', function () { }); it('should support binary data as array buffer', function (done) { + // Int8Array doesn't exist in IE8/9 + if (isOldIE && typeof Int8Array === 'undefined') { + done(); + return; + } + var input = new Int8Array(2); input[0] = 1; input[1] = 2; @@ -170,6 +173,12 @@ describe('requests', function () { }); it('should support binary data as array buffer view', function (done) { + // Int8Array doesn't exist in IE8/9 + if (isOldIE && typeof Int8Array === 'undefined') { + done(); + return; + } + var input = new Int8Array(2); input[0] = 1; input[1] = 2; @@ -186,6 +195,12 @@ describe('requests', function () { }); it('should support array buffer response', function (done) { + // ArrayBuffer doesn't exist in IE8/9 + if (isOldIE && typeof ArrayBuffer === 'undefined') { + done(); + return; + } + var response; function str2ab(str) { diff --git a/test/specs/transform.spec.js b/test/specs/transform.spec.js index 668abaa..eac9e11 100644 --- a/test/specs/transform.spec.js +++ b/test/specs/transform.spec.js @@ -1,6 +1,3 @@ -var axios = require('../../index'); -var getAjaxRequest = require('./__getAjaxRequest'); - describe('transform', function () { beforeEach(function () { jasmine.Ajax.install(); diff --git a/test/specs/utils/isX.spec.js b/test/specs/utils/isX.spec.js index 6ec903d..88a0f2b 100644 --- a/test/specs/utils/isX.spec.js +++ b/test/specs/utils/isX.spec.js @@ -7,22 +7,41 @@ describe('utils::isX', function () { }); it('should validate ArrayBuffer', function () { + // ArrayBuffer doesn't exist in IE8/9 + if (isOldIE && typeof ArrayBuffer === 'undefined') { + return; + } + expect(utils.isArrayBuffer(new ArrayBuffer(2))).toEqual(true); expect(utils.isArrayBuffer({})).toEqual(false); }); it('should validate ArrayBufferView', function () { + // ArrayBuffer doesn't exist in IE8/9 + if (isOldIE && typeof ArrayBuffer === 'undefined') { + return; + } + expect(utils.isArrayBufferView(new DataView(new ArrayBuffer(2)))).toEqual(true); }); it('should validate FormData', function () { + // FormData doesn't exist in IE8/9 + if (isOldIE && typeof FormData === 'undefined') { + return; + } + expect(utils.isFormData(new FormData())).toEqual(true); }); - // TODO Blob is not a constructor in PhantomJS - // it('should validate Blob', function () { - // expect(utils.isBlob(new Blob())).toEqual(true); - // }); + it('should validate Blob', function () { + // Blob doesn't exist in IE8/9 + if (isOldIE && typeof Blob === 'undefined') { + return; + } + + expect(utils.isBlob(new Blob())).toEqual(true); + }); it('should validate String', function () { expect(utils.isString('')).toEqual(true); diff --git a/test/specs/xsrf.spec.js b/test/specs/xsrf.spec.js index 5a5fb2f..71fc35c 100644 --- a/test/specs/xsrf.spec.js +++ b/test/specs/xsrf.spec.js @@ -1,6 +1,3 @@ -var axios = require('../../index'); -var getAjaxRequest = require('./__getAjaxRequest'); - describe('xsrf', function () { beforeEach(function () { jasmine.Ajax.install();