@incdevco/framework
Version:
node.js lambda framework
330 lines (207 loc) • 7.54 kB
JavaScript
var AWS = require('aws-sdk');
var Expect = require('chai').expect;
var Mock = require('../mock');
var Utilities = require('./index');
describe('utilities', function () {
'use strict';
var expected, mock;
beforeEach(function () {
expected = 'expected';
mock = new Mock();
});
describe('copy', function() {
it('should deep copy an object and the objects should not be tied together', function () {
var copy, original = {
key: 'value'
};
copy = Utilities.copy(original);
Expect(copy).to.deep.equal(original, 'copy deep equals');
original.key = 'value1';
Expect(copy.key).to.equal('value', 'not tied together');
});
it('should return original string', function() {
var str = 'my_string';
var result = Utilities.copy(str);
Expect(result).to.equal(str, 'result');
});
it('should return original undefined', function() {
var str = undefined;
var result = Utilities.copy(str);
Expect(result).to.equal(str, 'result');
});
});
describe('createAwsApiGatewayStageEndpoint', function() {
it('should', function () {
var restApiId = '95cn7dh8gnl2h72m50';
var stage = 'prod';
var region = 'us-west-2';
var expected = 'https://' + restApiId
+ '.execute-api.' + region
+ '.amazonaws.com/' + stage;
var result = Utilities.createAwsApiGatewayStageEndpoint(restApiId, stage, region);
Expect(result).to.equal(expected, 'result');
});
it('should use AWS.config.region is region is not submitted', function () {
var region = 'us-west-2';
AWS.config.update({region: region});
var restApiId = '95cn7dh8gnl2h72m50';
var stage = 'prod';
var expected = 'https://' + restApiId
+ '.execute-api.' + region
+ '.amazonaws.com/' + stage;
var result = Utilities.createAwsApiGatewayStageEndpoint(restApiId, stage);
Expect(result).to.equal(expected, 'result');
});
});
describe('createId', function() {
it('should create a random string at specified length', function () {
var actual, length = 5;
actual = Utilities.createId(length);
Expect(typeof actual).to.equal('string', 'string');
Expect(actual.length).to.equal(length, 'length');
});
});
describe('ddbEmptyStringsToNull', function() {
it('should set all empty string to null', function () {
var original = {
"id": "G664024LD",
"name": "dev-reception",
"is_group": true,
"created": 1499625178,
"creator": "U5TMM8PT7",
"is_archived": false,
"name_normalized": "dev-reception",
"is_mpim": false,
"members": [
"U5TMM8PT7"
],
"topic": {
"value": "",
"creator": "",
"last_set": 0
},
"purpose": {
"value": "",
"creator": "",
"last_set": 0
}
};
Utilities.ddbEmptyStringsToNull(original);
Expect(original.topic.value).to.equal(null, 'topic.value');
Expect(original.topic.creator).to.equal(null, 'topic.creator');
Expect(original.purpose.value).to.equal(null, 'topic.value');
Expect(original.purpose.creator).to.equal(null, 'topic.creator');
});
it('should set all empty string to null even in an array', function () {
var original = {
"id": "G664024LD",
"name": "dev-reception",
"is_group": true,
"created": 1499625178,
"creator": "U5TMM8PT7",
"is_archived": false,
"name_normalized": "dev-reception",
"is_mpim": false,
"members": [
"U5TMM8PT7"
],
"topic": {
"value": "",
"creator": "",
"last_set": 0
},
"purpose": {
"value": "",
"creator": "",
"last_set": 0
},
"array": [
{
"id": "1",
"empty": ""
}
]
};
Utilities.ddbEmptyStringsToNull(original);
Expect(original.topic.value).to.equal(null, 'topic.value');
Expect(original.topic.creator).to.equal(null, 'topic.creator');
Expect(original.purpose.value).to.equal(null, 'topic.value');
Expect(original.purpose.creator).to.equal(null, 'topic.creator');
Expect(original.array[0].empty).to.equal(null, 'topic.creator');
});
});
describe('getCurrentTimestamp', function() {
it('should return current timestamp', function () {
var actual;
actual = Utilities.getCurrentTimestamp();
expected = 'number';
Expect(typeof actual).to.equal(expected, 'typeof');
});
});
describe('merge', function() {
it('should combine the submitted objects into one object', function () {
var actual, object1, object2;
object1 = {
key1: 'value1'
};
object2 = {
key2: 'value2'
};
expected = {
key1: 'value1',
key2: 'value2'
};
actual = Utilities.merge(object1, object2);
Expect(actual).to.deep.equal(expected, 'actual');
});
it('should combine and overwite same keys with later object\'s value', function() {
var actual, object1, object2;
object1 = {
key1: 'value1'
};
object2 = {
key1: 'value2'
};
expected = {
key1: 'value2'
};
actual = Utilities.merge(object1, object2);
Expect(actual).to.deep.equal(expected, 'actual');
});
it('should combine but ignore a null or undefined submitted value', function() {
var actual, object1, object2, object3;
object1 = {
key1: 'value1'
};
object3 = {
key1: 'value2'
};
expected = {
key1: 'value2'
};
actual = Utilities.merge(object1, object2, object3);
Expect(actual).to.deep.equal(expected, 'actual');
object2 = null;
actual = Utilities.merge(object1, object2, object3);
Expect(actual).to.deep.equal(expected, 'actual');
});
});
describe('randNumber', function() {
it('should', function () {
var length = 4;
var result = Utilities.randNumber(length);
Expect(result.toString().length).to.equal(length, 'length');
length = 10;
result = Utilities.randNumber(length);
Expect(result.toString().length).to.equal(length, 'length');
});
});
describe('ucfirst', function() {
it('should capitalize the first letter of the string submitted', function () {
var actual;
actual = Utilities.ucfirst('word');
expected = 'Word';
Expect(actual).to.equal(expected, 'actual');
});
});
});