mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Moving many nodeunit tests to jasmine
This commit is contained in:
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
var spread = require('../../../lib/helpers/spread');
|
||||
|
||||
describe('helpers::spread', function () {
|
||||
it('should spread array to arguments', function () {
|
||||
var value = 0;
|
||||
spread(function (a, b) {
|
||||
value = a * b;
|
||||
})([5, 10]);
|
||||
|
||||
expect(value).toEqual(50);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
var transformData = require('../../../lib/helpers/transformData');
|
||||
|
||||
describe('helpers::transformData', function () {
|
||||
it('should support a single transformer', function () {
|
||||
var data;
|
||||
data = transformData(data, null, function (data) {
|
||||
data = 'foo';
|
||||
return data;
|
||||
});
|
||||
|
||||
expect(data).toEqual('foo');
|
||||
});
|
||||
|
||||
it('should support an array of transformers', function () {
|
||||
var data = '';
|
||||
data = transformData(data, null, [function (data) {
|
||||
data += 'f';
|
||||
return data;
|
||||
}, function (data) {
|
||||
data += 'o';
|
||||
return data;
|
||||
}, function (data) {
|
||||
data += 'o';
|
||||
return data;
|
||||
}]);
|
||||
|
||||
expect(data).toEqual('foo');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,64 @@
|
||||
var forEach = require('../../../lib/utils').forEach;
|
||||
|
||||
describe('utils::forEach', function () {
|
||||
it('should loop over an array', function () {
|
||||
var sum = 0;
|
||||
|
||||
forEach([1, 2, 3, 4, 5], function (val) {
|
||||
sum += val;
|
||||
});
|
||||
|
||||
expect(sum).toEqual(15);
|
||||
});
|
||||
|
||||
it('should loop over arguments', function () {
|
||||
var sum = 0;
|
||||
|
||||
(function () {
|
||||
forEach(arguments, function (val) {
|
||||
sum += val;
|
||||
});
|
||||
})(1, 2, 3, 4, 5);
|
||||
|
||||
expect(sum).toEqual(15);
|
||||
});
|
||||
|
||||
it('should loop over object keys', function () {
|
||||
var keys = '';
|
||||
var vals = 0;
|
||||
var obj = {
|
||||
b: 1,
|
||||
a: 2,
|
||||
r: 3
|
||||
};
|
||||
|
||||
forEach(obj, function (v, k) {
|
||||
keys += k;
|
||||
vals += v;
|
||||
});
|
||||
|
||||
expect(keys).toEqual('bar');
|
||||
expect(vals).toEqual(6);
|
||||
});
|
||||
|
||||
it('should handle undefined gracefully', function () {
|
||||
var count = 0;
|
||||
|
||||
forEach(undefined, function () {
|
||||
count++;
|
||||
});
|
||||
|
||||
expect(count).toEqual(0);
|
||||
});
|
||||
|
||||
it('should make an array out of non-array argument', function () {
|
||||
var count = 0;
|
||||
|
||||
forEach(function () {}, function () {
|
||||
count++;
|
||||
});
|
||||
|
||||
expect(count).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user