X-Git-Url: http://git.squeep.com/?p=squeep-indie-auther;a=blobdiff_plain;f=test%2Fsrc%2Fcommon.js;fp=test%2Fsrc%2Fcommon.js;h=1115f0ab5114c40466147bf821313f796a54e229;hp=0000000000000000000000000000000000000000;hb=b0103b0d496262c438b40bc20304081dbfe41e73;hpb=8ed81748bce7cea7904cac7225b20a60cafdfc16 diff --git a/test/src/common.js b/test/src/common.js new file mode 100644 index 0000000..1115f0a --- /dev/null +++ b/test/src/common.js @@ -0,0 +1,181 @@ +/* eslint-env mocha */ +'use strict'; + +const assert = require('assert'); +const common = require('../../src/common'); + +describe('Common', function () { + + describe('camelfy', function () { + it('covers', function () { + const snake = 'snake_case'; + const expected = 'snakeCase'; + const result = common.camelfy(snake); + assert.strictEqual(result, expected); + }); + it('covers edge-cases', function () { + const kebab = '-kebab-case-'; + const expected = 'KebabCase'; + const result = common.camelfy(kebab, '-'); + assert.strictEqual(result, expected); + }); + it('covers empty input', function () { + const empty = ''; + const expected = undefined; + const result = common.camelfy(empty); + assert.strictEqual(result, expected); + }); + it('covers un-camelfiable input', function () { + const bad = {}; + const expected = undefined; + const result = common.camelfy(bad); + assert.strictEqual(result, expected); + }); + }); // camelfy + + describe('freezeDeep', function () { + it('freezes things', function () { + const obj = { + sub1: { + sub2: { + foo: 'blah', + }, + }, + }; + const result = common.freezeDeep(obj); + assert(Object.isFrozen(result)); + assert(Object.isFrozen(result.sub1)); + assert(Object.isFrozen(result.sub1.sub2)); + assert(Object.isFrozen(result.sub1.sub2.foo)); + }); + }); // freezeDeep + + describe('axiosResponseLogData', function () { + it('covers', function () { + const response = { + status: 200, + statusText: 'OK', + headers: { + 'Content-Type': 'text/plain', + }, + otherData: 'blah', + data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green Meadows," said Old Mother West Wind, “and there I saw the Best Thing in the World.”', + }; + const expected = { + status: 200, + statusText: 'OK', + headers: { + 'Content-Type': 'text/plain', + }, + data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)', + }; + const result = common.axiosResponseLogData(response); + assert.deepStrictEqual(result, expected); + }); + it('covers no data', function () { + const response = { + status: 200, + statusText: 'OK', + headers: { + 'Content-Type': 'text/plain', + }, + }; + const expected = { + status: 200, + statusText: 'OK', + headers: { + 'Content-Type': 'text/plain', + }, + }; + const result = common.axiosResponseLogData(response); + assert.deepStrictEqual(result, expected); + }); + }); // axiosResponseLogData + + describe('logTruncate', function () { + it('returns short string', function () { + const str = 'this is a short string'; + const result = common.logTruncate(str, 100); + assert.strictEqual(result, str); + }); + it('truncates long string', function () { + const str = 'this is not really a very long string but it is long enough for this test'; + const result = common.logTruncate(str, 10); + assert(result.length < str.length); + }); + }); // logTruncate + + describe('ensureArray', function () { + it('returns empty array for no data', function () { + const result = common.ensureArray(); + assert.deepStrictEqual(result, []); + }); + it('returns same array passed in', function () { + const expected = [1, 2, 3, 'foo']; + const result = common.ensureArray(expected); + assert.deepStrictEqual(result, expected); + }); + it('returns array containing non-array data', function () { + const data = 'bar'; + const result = common.ensureArray(data); + assert.deepStrictEqual(result, [data]); + }); + }); // ensureArray + + describe('validError', function () { + it('covers valid', function () { + const result = common.validError('error'); + assert.strictEqual(result, true); + }); + it('covers invalid', function () { + const result = common.validError('🐔'); + assert.strictEqual(result, false); + }); + it('covers empty', function () { + const result = common.validError(); + assert.strictEqual(result, false); + }); + }); // validError + + describe('validScope', function () { + it('covers valid', function () { + const result = common.validScope('scope'); + assert.strictEqual(result, true); + }); + it('covers invalid', function () { + const result = common.validScope('🐔'); + assert.strictEqual(result, false); + }); + it('covers empty', function () { + const result = common.validScope(); + assert.strictEqual(result, false); + }); + }); // validScope + + describe('newSecret', function () { + it('covers default', async function () { + const result = await common.newSecret(); + assert(result.length); + }); + it('covers specified', async function () { + const result = await common.newSecret(21); + assert(result.length); + }); + }); // newSecret + + describe('dateToEpoch', function () { + it('covers no supplied date', function () { + const nowMs = Date.now() / 1000; + const result = common.dateToEpoch(); + const drift = Math.abs(result - nowMs); + assert(drift < 2000); + }); + it('covers supplied date', function () { + const now = new Date(); + const nowEpoch = Math.ceil(now / 1000); + const result = common.dateToEpoch(now); + assert.strictEqual(result, nowEpoch); + }); + }); // dateToEpoch + +}); // Common