提交 fc12b933 编写于 作者: M mzabriskie

Moving many nodeunit tests to jasmine

上级 b745600a
......@@ -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: {
......
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');
});
});
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');
});
});
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');
});
});
var parseHeaders = require('../../../lib/helpers/parseHeaders');
module.exports = {
testParse: function (test) {
describe('helpers::parseHeaders', function () {
it('should parse headers', function () {
var date = new Date();
var parsed = parseHeaders(
'Date: ' + date.toISOString() + '\n' +
......@@ -10,10 +10,10 @@ module.exports = {
'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
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');
});
});
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);
});
});
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');
});
});
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);
});
});
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();
}
};
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);
});
});
var merge = require('../../../lib/utils').merge;
module.exports = {
testImmutability: function (test) {
describe('utils::merge', function () {
it('should be immutable', function () {
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) {
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);
test.equals(d.foo, 789);
test.equals(d.bar, 456);
test.done();
}
};
\ No newline at end of file
expect(d.foo).toEqual(789);
expect(d.bar).toEqual(456);
});
});
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');
});
});
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
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
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(['<h1>Foo</h1>'], {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();
}
};
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册