initial commit
[squeep-indie-auther] / test / src / common.js
1 /* eslint-env mocha */
2 'use strict';
3
4 const assert = require('assert');
5 const common = require('../../src/common');
6
7 describe('Common', function () {
8
9 describe('camelfy', function () {
10 it('covers', function () {
11 const snake = 'snake_case';
12 const expected = 'snakeCase';
13 const result = common.camelfy(snake);
14 assert.strictEqual(result, expected);
15 });
16 it('covers edge-cases', function () {
17 const kebab = '-kebab-case-';
18 const expected = 'KebabCase';
19 const result = common.camelfy(kebab, '-');
20 assert.strictEqual(result, expected);
21 });
22 it('covers empty input', function () {
23 const empty = '';
24 const expected = undefined;
25 const result = common.camelfy(empty);
26 assert.strictEqual(result, expected);
27 });
28 it('covers un-camelfiable input', function () {
29 const bad = {};
30 const expected = undefined;
31 const result = common.camelfy(bad);
32 assert.strictEqual(result, expected);
33 });
34 }); // camelfy
35
36 describe('freezeDeep', function () {
37 it('freezes things', function () {
38 const obj = {
39 sub1: {
40 sub2: {
41 foo: 'blah',
42 },
43 },
44 };
45 const result = common.freezeDeep(obj);
46 assert(Object.isFrozen(result));
47 assert(Object.isFrozen(result.sub1));
48 assert(Object.isFrozen(result.sub1.sub2));
49 assert(Object.isFrozen(result.sub1.sub2.foo));
50 });
51 }); // freezeDeep
52
53 describe('axiosResponseLogData', function () {
54 it('covers', function () {
55 const response = {
56 status: 200,
57 statusText: 'OK',
58 headers: {
59 'Content-Type': 'text/plain',
60 },
61 otherData: 'blah',
62 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.”',
63 };
64 const expected = {
65 status: 200,
66 statusText: 'OK',
67 headers: {
68 'Content-Type': 'text/plain',
69 },
70 data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
71 };
72 const result = common.axiosResponseLogData(response);
73 assert.deepStrictEqual(result, expected);
74 });
75 it('covers no data', function () {
76 const response = {
77 status: 200,
78 statusText: 'OK',
79 headers: {
80 'Content-Type': 'text/plain',
81 },
82 };
83 const expected = {
84 status: 200,
85 statusText: 'OK',
86 headers: {
87 'Content-Type': 'text/plain',
88 },
89 };
90 const result = common.axiosResponseLogData(response);
91 assert.deepStrictEqual(result, expected);
92 });
93 }); // axiosResponseLogData
94
95 describe('logTruncate', function () {
96 it('returns short string', function () {
97 const str = 'this is a short string';
98 const result = common.logTruncate(str, 100);
99 assert.strictEqual(result, str);
100 });
101 it('truncates long string', function () {
102 const str = 'this is not really a very long string but it is long enough for this test';
103 const result = common.logTruncate(str, 10);
104 assert(result.length < str.length);
105 });
106 }); // logTruncate
107
108 describe('ensureArray', function () {
109 it('returns empty array for no data', function () {
110 const result = common.ensureArray();
111 assert.deepStrictEqual(result, []);
112 });
113 it('returns same array passed in', function () {
114 const expected = [1, 2, 3, 'foo'];
115 const result = common.ensureArray(expected);
116 assert.deepStrictEqual(result, expected);
117 });
118 it('returns array containing non-array data', function () {
119 const data = 'bar';
120 const result = common.ensureArray(data);
121 assert.deepStrictEqual(result, [data]);
122 });
123 }); // ensureArray
124
125 describe('validError', function () {
126 it('covers valid', function () {
127 const result = common.validError('error');
128 assert.strictEqual(result, true);
129 });
130 it('covers invalid', function () {
131 const result = common.validError('πŸ”');
132 assert.strictEqual(result, false);
133 });
134 it('covers empty', function () {
135 const result = common.validError();
136 assert.strictEqual(result, false);
137 });
138 }); // validError
139
140 describe('validScope', function () {
141 it('covers valid', function () {
142 const result = common.validScope('scope');
143 assert.strictEqual(result, true);
144 });
145 it('covers invalid', function () {
146 const result = common.validScope('πŸ”');
147 assert.strictEqual(result, false);
148 });
149 it('covers empty', function () {
150 const result = common.validScope();
151 assert.strictEqual(result, false);
152 });
153 }); // validScope
154
155 describe('newSecret', function () {
156 it('covers default', async function () {
157 const result = await common.newSecret();
158 assert(result.length);
159 });
160 it('covers specified', async function () {
161 const result = await common.newSecret(21);
162 assert(result.length);
163 });
164 }); // newSecret
165
166 describe('dateToEpoch', function () {
167 it('covers no supplied date', function () {
168 const nowMs = Date.now() / 1000;
169 const result = common.dateToEpoch();
170 const drift = Math.abs(result - nowMs);
171 assert(drift < 2000);
172 });
173 it('covers supplied date', function () {
174 const now = new Date();
175 const nowEpoch = Math.ceil(now / 1000);
176 const result = common.dateToEpoch(now);
177 assert.strictEqual(result, nowEpoch);
178 });
179 }); // dateToEpoch
180
181 }); // Common