swatchjs-utils
Version:
Utilities for SwatchJS, a framework for easily creating and exposing APIs as methods
133 lines (112 loc) • 4.27 kB
JavaScript
;
var _require = require('chai'),
expect = _require.expect;
var errors = require('../../../lib/errors');
var objectParsers = require('../../../lib/api/parsers/objects');
var error = errors.ERROR_CODE_INVALID_OBJECT;
describe('parsers.objects', function () {
describe('parseObject', function () {
it('should parse and return a serialized JSON document', function () {
function checkTargetObject(obj) {
var result = objectParsers.parseObject(obj);
expect(result).to.deep.equal(obj);
}
// Check that it properly parses objects with expected keys
var t1 = { id: 'test.id', key: 'something' };
checkTargetObject(t1);
var t2 = { id: ' some other item ', key: '-another-' };
checkTargetObject(t2);
var t3 = { id: 'user-id1', other: 100, key: 'extra' };
checkTargetObject(t3);
});
it('should throw an error parsing a non-dict object', function () {
// Check that it rejects false-y input values
expect(function () {
return objectParsers.parseObject(null);
}).to.throw(error);
expect(function () {
return objectParsers.parseObject(undefined);
}).to.throw(error);
// Check that it rejects values that are not objects
expect(function () {
return objectParsers.parseObject('a');
}).to.throw(error);
expect(function () {
return objectParsers.parseObject(100);
}).to.throw(error);
expect(function () {
return objectParsers.parseObject(true);
}).to.throw(error);
expect(function () {
return objectParsers.parseObject(false);
}).to.throw(error);
expect(function () {
return objectParsers.parseObject('false');
}).to.throw(error);
// Check that it rejects complex object-like values
expect(function () {
return objectParsers.parseObject([1, 2, 3]);
}).to.throw(error);
var fn = function fn() {
return 100;
};
expect(function () {
return objectParsers.parseObject(fn);
}).to.throw(error);
var regexp = new RegExp('\\w+');
expect(function () {
return objectParsers.parseObject(regexp);
}).to.throw(error);
});
});
describe('parseOptionalObject', function () {
it('should parse and return a serialized JSON document', function () {
function checkTargetObject(obj) {
var result = objectParsers.parseOptionalObject(obj);
expect(result).to.deep.equal(obj);
}
// Check that it allows null and undefined values
expect(objectParsers.parseOptionalObject(null)).to.equal(undefined);
expect(objectParsers.parseOptionalObject(undefined)).to.equal(undefined);
// Check that it properly parses objects with expected keys
var t1 = { id: 'test.id', key: 'something' };
checkTargetObject(t1);
var t2 = { id: ' some other item ', key: '-another-' };
checkTargetObject(t2);
var t3 = { id: 'user-id1', other: 100, key: 'extra' };
checkTargetObject(t3);
});
it('should throw an error parsing a non-dict object', function () {
// Check that it rejects values that are not objects
expect(function () {
return objectParsers.parseOptionalObject('a');
}).to.throw(error);
expect(function () {
return objectParsers.parseOptionalObject(100);
}).to.throw(error);
expect(function () {
return objectParsers.parseOptionalObject(true);
}).to.throw(error);
expect(function () {
return objectParsers.parseOptionalObject(false);
}).to.throw(error);
expect(function () {
return objectParsers.parseOptionalObject('false');
}).to.throw(error);
// Check that it rejects complex object-like values
expect(function () {
return objectParsers.parseOptionalObject([1, 2, 3]);
}).to.throw(error);
var fn = function fn() {
return 100;
};
expect(function () {
return objectParsers.parseOptionalObject(fn);
}).to.throw(error);
var regexp = new RegExp('\\w+');
expect(function () {
return objectParsers.parseOptionalObject(regexp);
}).to.throw(error);
});
});
});