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