From fc12b933f729f236b2149872c42961a8626dafb7 Mon Sep 17 00:00:00 2001 From: mzabriskie Date: Wed, 18 Mar 2015 17:12:51 -0600 Subject: [PATCH] Moving many nodeunit tests to jasmine --- karma.conf.js | 2 +- test/specs/defaults.spec.js | 23 +++++++ test/specs/helpers/buildUrl.spec.js | 48 +++++++++++++ test/specs/helpers/cookies.spec.js | 36 ++++++++++ test/specs/helpers/parseHeaders.spec.js | 19 ++++++ .../helpers/spread.spec.js} | 12 ++-- .../helpers/transformData.spec.js} | 19 +++--- test/specs/helpers/urlIsSameOrigin.spec.js | 11 +++ .../utils/forEach.spec.js} | 44 ++++++------ test/specs/utils/isX.spec.js | 52 ++++++++++++++ test/specs/utils/merge.spec.js | 27 ++++++++ test/specs/utils/trim.spec.js | 12 ++++ test/unit/{axios => adapters}/http.js | 0 test/unit/helpers/buildUrl.js | 67 ------------------- test/unit/helpers/defaults.js | 32 --------- test/unit/helpers/parseHeaders.js | 19 ------ test/unit/utils/isX.js | 60 ----------------- test/unit/utils/merge.js | 28 -------- test/unit/utils/trim.js | 13 ---- 19 files changed, 264 insertions(+), 260 deletions(-) create mode 100644 test/specs/defaults.spec.js create mode 100644 test/specs/helpers/buildUrl.spec.js create mode 100644 test/specs/helpers/cookies.spec.js create mode 100644 test/specs/helpers/parseHeaders.spec.js rename test/{unit/helpers/spread.js => specs/helpers/spread.spec.js} (50%) rename test/{unit/helpers/transformData.js => specs/helpers/transformData.spec.js} (63%) create mode 100644 test/specs/helpers/urlIsSameOrigin.spec.js rename test/{unit/utils/forEach.js => specs/utils/forEach.spec.js} (53%) create mode 100644 test/specs/utils/isX.spec.js create mode 100644 test/specs/utils/merge.spec.js create mode 100644 test/specs/utils/trim.spec.js rename test/unit/{axios => adapters}/http.js (100%) delete mode 100644 test/unit/helpers/buildUrl.js delete mode 100644 test/unit/helpers/defaults.js delete mode 100644 test/unit/helpers/parseHeaders.js delete mode 100644 test/unit/utils/isX.js delete mode 100644 test/unit/utils/merge.js delete mode 100644 test/unit/utils/trim.js diff --git a/karma.conf.js b/karma.conf.js index c628821..3582032 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -40,7 +40,7 @@ module.exports = function(config) { // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter - reporters: ['progress', 'coverage'], + reporters: ['dots', 'coverage'], coverageReporter: { diff --git a/test/specs/defaults.spec.js b/test/specs/defaults.spec.js new file mode 100644 index 0000000..9a97653 --- /dev/null +++ b/test/specs/defaults.spec.js @@ -0,0 +1,23 @@ +var defaults = require('../../lib/defaults'); + +describe('defaults', function () { + it('should transform request json', function () { + expect(defaults.transformRequest[0]({foo: 'bar'})).toEqual('{"foo":"bar"}'); + }); + + it('should do nothing to request string', function () { + expect(defaults.transformRequest[0]('foo=bar')).toEqual('foo=bar'); + }); + + it('should transform response json', function () { + var data = defaults.transformResponse[0]('{"foo":"bar"}'); + + expect(typeof data).toEqual('object'); + expect(data.foo).toEqual('bar'); + }); + + it('should do nothing to response string', function () { + expect(defaults.transformResponse[0]('foo=bar')).toEqual('foo=bar'); + }); +}); + diff --git a/test/specs/helpers/buildUrl.spec.js b/test/specs/helpers/buildUrl.spec.js new file mode 100644 index 0000000..4b4bd01 --- /dev/null +++ b/test/specs/helpers/buildUrl.spec.js @@ -0,0 +1,48 @@ +var buildUrl = require('../../../lib/helpers/buildUrl'); + +describe('helpers::buildUrl', function () { + it('should support null params', function () { + expect(buildUrl('/foo')).toEqual('/foo'); + }); + + it('should support params', function () { + expect(buildUrl('/foo', { + foo: 'bar' + })).toEqual('/foo?foo=bar'); + }); + + it('should support object params', function () { + expect(buildUrl('/foo', { + foo: { + bar: 'baz' + } + })).toEqual('/foo?foo=' + encodeURI('{"bar":"baz"}')); + }); + + it('should support date params', function () { + var date = new Date(); + + expect(buildUrl('/foo', { + date: date + })).toEqual('/foo?date=' + date.toISOString()); + }); + + it('should support array params', function () { + expect(buildUrl('/foo', { + foo: ['bar', 'baz'] + })).toEqual('/foo?foo=bar&foo=baz'); + }); + + it('should support special char params', function () { + expect(buildUrl('/foo', { + foo: '@:$, ' + })).toEqual('/foo?foo=@:$,+'); + }); + + it('should support existing params', function () { + expect(buildUrl('/foo?foo=bar', { + bar: 'baz' + })).toEqual('/foo?foo=bar&bar=baz'); + }); +}); + diff --git a/test/specs/helpers/cookies.spec.js b/test/specs/helpers/cookies.spec.js new file mode 100644 index 0000000..9c3880d --- /dev/null +++ b/test/specs/helpers/cookies.spec.js @@ -0,0 +1,36 @@ +var cookies = require('../../../lib/helpers/cookies'); + +describe('helpers::cookies', function () { + afterEach(function () { + // Remove all the cookies + var expires = Date.now() - (60 * 60 * 24 * 7); + document.cookie.split(';').map(function (cookie) { + return cookie.split('=')[0]; + }).forEach(function (name) { + document.cookie = name + '=; expires=' + new Date(expires).toGMTString(); + }); + }); + + it('should write cookies', function () { + cookies.write('foo', 'baz'); + expect(document.cookie).toEqual('foo=baz'); + }); + + it('should read cookies', function () { + cookies.write('foo', 'abc'); + cookies.write('bar', 'def'); + expect(cookies.read('foo')).toEqual('abc'); + expect(cookies.read('bar')).toEqual('def'); + }); + + it('should remove cookies', function () { + cookies.write('foo', 'bar'); + cookies.remove('foo'); + expect(cookies.read('foo')).toEqual(null); + }); + + it('should uri encode values', function () { + cookies.write('foo', 'bar baz%'); + expect(document.cookie).toEqual('foo=bar%20baz%25'); + }); +}); diff --git a/test/specs/helpers/parseHeaders.spec.js b/test/specs/helpers/parseHeaders.spec.js new file mode 100644 index 0000000..9347964 --- /dev/null +++ b/test/specs/helpers/parseHeaders.spec.js @@ -0,0 +1,19 @@ +var parseHeaders = require('../../../lib/helpers/parseHeaders'); + +describe('helpers::parseHeaders', function () { + it('should parse headers', function () { + var date = new Date(); + var parsed = parseHeaders( + 'Date: ' + date.toISOString() + '\n' + + 'Content-Type: application/json\n' + + 'Connection: keep-alive\n' + + 'Transfer-Encoding: chunked' + ); + + expect(parsed['date']).toEqual(date.toISOString()); + expect(parsed['content-type']).toEqual('application/json'); + expect(parsed['connection']).toEqual('keep-alive'); + expect(parsed['transfer-encoding']).toEqual('chunked'); + }); +}); + diff --git a/test/unit/helpers/spread.js b/test/specs/helpers/spread.spec.js similarity index 50% rename from test/unit/helpers/spread.js rename to test/specs/helpers/spread.spec.js index ae93742..8f8818d 100644 --- a/test/unit/helpers/spread.js +++ b/test/specs/helpers/spread.spec.js @@ -1,13 +1,13 @@ var spread = require('../../../lib/helpers/spread'); -module.exports = { - testSpread: function (test) { +describe('helpers::spread', function () { + it('should spread array to arguments', function () { var value = 0; spread(function (a, b) { value = a * b; })([5, 10]); - test.equals(value, 50); - test.done(); - } -}; \ No newline at end of file + expect(value).toEqual(50); + }); +}); + diff --git a/test/unit/helpers/transformData.js b/test/specs/helpers/transformData.spec.js similarity index 63% rename from test/unit/helpers/transformData.js rename to test/specs/helpers/transformData.spec.js index d6febf2..9eccb7e 100644 --- a/test/unit/helpers/transformData.js +++ b/test/specs/helpers/transformData.spec.js @@ -1,18 +1,17 @@ var transformData = require('../../../lib/helpers/transformData'); -module.exports = { - testSingleFunction: function (test) { +describe('helpers::transformData', function () { + it('should support a single transformer', function () { var data; data = transformData(data, null, function (data) { data = 'foo'; return data; }); - test.equals(data, 'foo'); - test.done(); - }, + expect(data).toEqual('foo'); + }); - testFunctionArray: function (test) { + it('should support an array of transformers', function () { var data = ''; data = transformData(data, null, [function (data) { data += 'f'; @@ -25,7 +24,7 @@ module.exports = { return data; }]); - test.equals(data, 'foo'); - test.done(); - } -}; \ No newline at end of file + expect(data).toEqual('foo'); + }); +}); + diff --git a/test/specs/helpers/urlIsSameOrigin.spec.js b/test/specs/helpers/urlIsSameOrigin.spec.js new file mode 100644 index 0000000..12be4e2 --- /dev/null +++ b/test/specs/helpers/urlIsSameOrigin.spec.js @@ -0,0 +1,11 @@ +var urlIsSameOrigin = require('../../../lib/helpers/urlIsSameOrigin'); + +describe('helpers::urlIsSameOrigin', function () { + it('should detect same origin', function () { + expect(urlIsSameOrigin(window.location.href)).toEqual(true); + }); + + it('should detect different origin', function () { + expect(urlIsSameOrigin('https://github.com/mzabriskie/axios')).toEqual(false); + }); +}); diff --git a/test/unit/utils/forEach.js b/test/specs/utils/forEach.spec.js similarity index 53% rename from test/unit/utils/forEach.js rename to test/specs/utils/forEach.spec.js index cf97e44..f3bc19d 100644 --- a/test/unit/utils/forEach.js +++ b/test/specs/utils/forEach.spec.js @@ -1,18 +1,17 @@ var forEach = require('../../../lib/utils').forEach; -module.exports = { - testArray: function (test) { +describe('utils::forEach', function () { + it('should loop over an array', function () { var sum = 0; forEach([1, 2, 3, 4, 5], function (val) { sum += val; }); - test.equal(sum, 15); - test.done(); - }, + expect(sum).toEqual(15); + }); - testArguments: function (test) { + it('should loop over arguments', function () { var sum = 0; (function () { @@ -21,11 +20,10 @@ module.exports = { }); })(1, 2, 3, 4, 5); - test.equal(sum, 15); - test.done(); - }, + expect(sum).toEqual(15); + }); - testObject: function (test) { + it('should loop over object keys', function () { var keys = ''; var vals = 0; var obj = { @@ -39,30 +37,28 @@ module.exports = { vals += v; }); - test.equal(keys, 'bar'); - test.equal(vals, 6); - test.done(); - }, + expect(keys).toEqual('bar'); + expect(vals).toEqual(6); + }); - testUndefined: function (test) { + it('should handle undefined gracefully', function () { var count = 0; forEach(undefined, function () { count++; }); - test.equals(count, 0); - test.done(); - }, + expect(count).toEqual(0); + }); - testFunction: function (test) { + it('should make an array out of non-array argument', function () { var count = 0; forEach(function () {}, function () { count++; - }) + }); + + expect(count).toEqual(1); + }); +}); - test.equals(count, 1); - test.done(); - } -}; diff --git a/test/specs/utils/isX.spec.js b/test/specs/utils/isX.spec.js new file mode 100644 index 0000000..6ec903d --- /dev/null +++ b/test/specs/utils/isX.spec.js @@ -0,0 +1,52 @@ +var utils = require('../../../lib/utils'); + +describe('utils::isX', function () { + it('should validate Array', function () { + expect(utils.isArray([])).toEqual(true); + expect(utils.isArray({length: 5})).toEqual(false); + }); + + it('should validate ArrayBuffer', function () { + expect(utils.isArrayBuffer(new ArrayBuffer(2))).toEqual(true); + expect(utils.isArrayBuffer({})).toEqual(false); + }); + + it('should validate ArrayBufferView', function () { + expect(utils.isArrayBufferView(new DataView(new ArrayBuffer(2)))).toEqual(true); + }); + + it('should validate FormData', function () { + 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 String', function () { + expect(utils.isString('')).toEqual(true); + expect(utils.isString({toString: function () { return ''; }})).toEqual(false); + }); + + it('should validate Number', function () { + expect(utils.isNumber(123)).toEqual(true); + expect(utils.isNumber('123')).toEqual(false); + }); + + it('should validate Undefined', function () { + expect(utils.isUndefined()).toEqual(true); + expect(utils.isUndefined(null)).toEqual(false); + }); + + it('should validate Object', function () { + expect(utils.isObject({})).toEqual(true); + expect(utils.isObject(null)).toEqual(false); + }); + + it('should validate Date', function () { + expect(utils.isDate(new Date())).toEqual(true); + expect(utils.isDate(Date.now())).toEqual(false); + }); +}); + diff --git a/test/specs/utils/merge.spec.js b/test/specs/utils/merge.spec.js new file mode 100644 index 0000000..5e826e7 --- /dev/null +++ b/test/specs/utils/merge.spec.js @@ -0,0 +1,27 @@ +var merge = require('../../../lib/utils').merge; + +describe('utils::merge', function () { + it('should be immutable', function () { + var a = {}; + var b = {foo: 123}; + var c = {bar: 456}; + + merge(a, b, c); + + expect(typeof a.foo).toEqual('undefined'); + expect(typeof a.bar).toEqual('undefined'); + expect(typeof b.bar).toEqual('undefined'); + expect(typeof c.foo).toEqual('undefined'); + }); + + it('should merge properties', function () { + var a = {foo: 123}; + var b = {bar: 456}; + var c = {foo: 789}; + var d = merge(a, b, c); + + expect(d.foo).toEqual(789); + expect(d.bar).toEqual(456); + }); +}); + diff --git a/test/specs/utils/trim.spec.js b/test/specs/utils/trim.spec.js new file mode 100644 index 0000000..1b45e95 --- /dev/null +++ b/test/specs/utils/trim.spec.js @@ -0,0 +1,12 @@ +var trim = require('../../../lib/utils').trim; + +describe('utils::trim', function () { + it('should trim spaces', function () { + expect(trim(' foo ')).toEqual('foo'); + }); + + it('should trim tabs', function () { + expect(trim('\tfoo\t')).toEqual('foo'); + }); +}); + diff --git a/test/unit/axios/http.js b/test/unit/adapters/http.js similarity index 100% rename from test/unit/axios/http.js rename to test/unit/adapters/http.js diff --git a/test/unit/helpers/buildUrl.js b/test/unit/helpers/buildUrl.js deleted file mode 100644 index a6f53f9..0000000 --- a/test/unit/helpers/buildUrl.js +++ /dev/null @@ -1,67 +0,0 @@ -var buildUrl = require('../../../lib/helpers/buildUrl'); - -module.exports = { - testNullParams: function (test) { - var url = buildUrl('/foo'); - - test.equals(url, '/foo'); - test.done(); - }, - - testParams: function (test) { - var url = buildUrl('/foo', { - foo: 'bar' - }); - - test.equals(url, '/foo?foo=bar'); - test.done(); - }, - - testObjectParam: function (test) { - var url = buildUrl('/foo', { - foo: { - bar: 'baz' - } - }); - - test.equals(url, '/foo?foo=' + encodeURI('{"bar":"baz"}')); - test.done(); - }, - - testDateParam: function (test) { - var date = new Date(); - var url = buildUrl('/foo', { - date: date - }); - - test.equals(url, '/foo?date=' + date.toISOString()); - test.done(); - }, - - testArrayParam: function (test) { - var url = buildUrl('/foo', { - foo: ['bar', 'baz'] - }); - - test.equals(url, '/foo?foo=bar&foo=baz'); - test.done(); - }, - - testSpecialChars: function (test) { - var url = buildUrl('/foo', { - foo: '@:$, ' - }); - - test.equals(url, '/foo?foo=@:$,+'); - test.done(); - }, - - testQuestionMark: function (test) { - var url = buildUrl('/foo?foo=bar', { - bar: 'baz' - }); - - test.equals(url, '/foo?foo=bar&bar=baz'); - test.done(); - } -}; \ No newline at end of file diff --git a/test/unit/helpers/defaults.js b/test/unit/helpers/defaults.js deleted file mode 100644 index df1ea84..0000000 --- a/test/unit/helpers/defaults.js +++ /dev/null @@ -1,32 +0,0 @@ -var defaults = require('../../../lib/defaults'); - -module.exports = { - testTransformRequestJson: function (test) { - var data = defaults.transformRequest[0]({foo: 'bar'}); - - test.equals(data, '{"foo":"bar"}'); - test.done(); - }, - - testTransformRequestString: function (test) { - var data = defaults.transformRequest[0]('foo=bar'); - - test.equals(data, 'foo=bar'); - test.done(); - }, - - testTransformResponseJson: function (test) { - var data = defaults.transformResponse[0]('{"foo":"bar"}'); - - test.equals(typeof data, 'object'); - test.equals(data.foo, 'bar'); - test.done(); - }, - - testTransformResponseString: function (test) { - var data = defaults.transformResponse[0]('foo=bar'); - - test.equals(data, 'foo=bar'); - test.done(); - } -}; \ No newline at end of file diff --git a/test/unit/helpers/parseHeaders.js b/test/unit/helpers/parseHeaders.js deleted file mode 100644 index 0926a35..0000000 --- a/test/unit/helpers/parseHeaders.js +++ /dev/null @@ -1,19 +0,0 @@ -var parseHeaders = require('../../../lib/helpers/parseHeaders'); - -module.exports = { - testParse: function (test) { - var date = new Date(); - var parsed = parseHeaders( - 'Date: ' + date.toISOString() + '\n' + - 'Content-Type: application/json\n' + - 'Connection: keep-alive\n' + - 'Transfer-Encoding: chunked' - ); - - test.equals(parsed['date'], date.toISOString()); - test.equals(parsed['content-type'], 'application/json'); - test.equals(parsed['connection'], 'keep-alive'); - test.equals(parsed['transfer-encoding'], 'chunked'); - test.done(); - } -}; \ No newline at end of file diff --git a/test/unit/utils/isX.js b/test/unit/utils/isX.js deleted file mode 100644 index 0d14021..0000000 --- a/test/unit/utils/isX.js +++ /dev/null @@ -1,60 +0,0 @@ -var utils = require('../../../lib/utils'); - -module.exports = { - testIsArray: function (test) { - test.equals(utils.isArray([]), true); - test.equals(utils.isArray({length: 5}), false); - test.done(); - }, - - testIsArrayBuffer: function (test) { - test.equals(utils.isArrayBuffer(new ArrayBuffer(2)), true); - test.done(); - }, - - testIsArrayBufferView: function (test) { - test.equals(utils.isArrayBufferView(new DataView(new ArrayBuffer(2))), true); - test.done(); - }, - - // TODO These tests need a browser to run - // testIsFormData: function (test) { - // test.equals(utils.isFormData(new FormData()), true); - // test.done(); - // }, - // - // testIsBlob: function (test) { - // test.equals(utils.isBlob(new Blob(['

Foo

'], {type: 'text/html'})), true); - // test.done(); - // }, - - testIsString: function (test) { - test.equals(utils.isString(''), true); - test.equals(utils.isString({toString: function () { return ''; }}), false); - test.done(); - }, - - testIsNumber: function (test) { - test.equals(utils.isNumber(123), true); - test.equals(utils.isNumber('123'), false); - test.done(); - }, - - testIsUndefined: function (test) { - test.equals(utils.isUndefined(), true); - test.equals(utils.isUndefined(null), false); - test.done(); - }, - - testIsObject: function (test) { - test.equals(utils.isObject({}), true); - test.equals(utils.isObject(null), false); - test.done(); - }, - - testIsDate: function (test) { - test.equals(utils.isDate(new Date()), true); - test.equals(utils.isDate(Date.now()), false); - test.done(); - } -}; diff --git a/test/unit/utils/merge.js b/test/unit/utils/merge.js deleted file mode 100644 index b2f1100..0000000 --- a/test/unit/utils/merge.js +++ /dev/null @@ -1,28 +0,0 @@ -var merge = require('../../../lib/utils').merge; - -module.exports = { - testImmutability: function (test) { - var a = {}; - var b = {foo: 123}; - var c = {bar: 456}; - - merge(a, b, c); - - test.equals(typeof a.foo, 'undefined'); - test.equals(typeof a.bar, 'undefined'); - test.equals(typeof b.bar, 'undefined'); - test.equals(typeof c.foo, 'undefined'); - test.done(); - }, - - testMerge: function (test) { - var a = {foo: 123}; - var b = {bar: 456}; - var c = {foo: 789}; - var d = merge(a, b, c); - - test.equals(d.foo, 789); - test.equals(d.bar, 456); - test.done(); - } -}; \ No newline at end of file diff --git a/test/unit/utils/trim.js b/test/unit/utils/trim.js deleted file mode 100644 index e134a39..0000000 --- a/test/unit/utils/trim.js +++ /dev/null @@ -1,13 +0,0 @@ -var trim = require('../../../lib/utils').trim; - -module.exports = { - testTrim: function (test) { - test.equals(trim(' foo '), 'foo'); - test.done(); - }, - - testTrimTab: function (test) { - test.equals(trim('\tfoo'), 'foo'); - test.done(); - } -}; \ No newline at end of file -- GitLab