4 const assert
= require('assert');
5 const sinon
= require('sinon');
6 const StubLogger
= require('../stub-logger');
7 const common
= require('../../src/common');
9 describe('Common', function () {
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
);
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
);
24 it('covers empty input', function () {
26 const expected
= undefined;
27 const result
= common
.camelfy(empty
);
28 assert
.strictEqual(result
, expected
);
30 it('covers un-camelfiable input', function () {
32 const expected
= undefined;
33 const result
= common
.camelfy(bad
);
34 assert
.strictEqual(result
, expected
);
38 describe('freezeDeep', function () {
39 it('freezes things', function () {
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
));
55 describe('axiosResponseLogData', function () {
56 it('covers', function () {
61 'Content-Type': 'text/plain',
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.β',
70 'Content-Type': 'text/plain',
72 data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
74 const result
= common
.axiosResponseLogData(response
);
75 assert
.deepStrictEqual(result
, expected
);
77 it('covers no data', function () {
82 'Content-Type': 'text/plain',
89 'Content-Type': 'text/plain',
92 const result
= common
.axiosResponseLogData(response
);
93 assert
.deepStrictEqual(result
, expected
);
95 }); // axiosResponseLogData
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
);
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
);
110 describe('ensureArray', function () {
111 it('returns empty array for no data', function () {
112 const result
= common
.ensureArray();
113 assert
.deepStrictEqual(result
, []);
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
);
120 it('returns array containing non-array data', function () {
122 const result
= common
.ensureArray(data
);
123 assert
.deepStrictEqual(result
, [data
]);
127 describe('validError', function () {
128 it('covers valid', function () {
129 const result
= common
.validError('error');
130 assert
.strictEqual(result
, true);
132 it('covers invalid', function () {
133 const result
= common
.validError('π');
134 assert
.strictEqual(result
, false);
136 it('covers empty', function () {
137 const result
= common
.validError();
138 assert
.strictEqual(result
, false);
142 describe('validScope', function () {
143 it('covers valid', function () {
144 const result
= common
.validScope('scope');
145 assert
.strictEqual(result
, true);
147 it('covers invalid', function () {
148 const result
= common
.validScope('π');
149 assert
.strictEqual(result
, false);
151 it('covers empty', function () {
152 const result
= common
.validScope();
153 assert
.strictEqual(result
, false);
157 describe('newSecret', function () {
158 it('covers default', async
function () {
159 const result
= await common
.newSecret();
160 assert(result
.length
);
162 it('covers specified', async
function () {
163 const result
= await common
.newSecret(21);
164 assert(result
.length
);
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);
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
);
183 describe('omit', function () {
184 it('covers', function () {
192 const omitted
= ['bar', 'baz'];
196 const result
= common
.omit(obj
, omitted
);
197 assert
.deepStrictEqual(result
, expected
);
201 describe('mysteryBoxLogger', function () {
203 beforeEach(function () {
204 stubLogger
= new StubLogger();
206 mbl
= common
.mysteryBoxLogger(stubLogger
, 'test:scope');
208 afterEach(function () {
211 it('covers', function () {
213 packageName: 'fake-mystery-box',
214 packageVersion: '0.0.0',
218 assert(stubLogger
.debug
.called
);
220 }); // mysteryBoxLogger