1115f0ab5114c40466147bf821313f796a54e229
4 const assert
= require('assert');
5 const common
= require('../../src/common');
7 describe('Common', function () {
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
);
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
);
22 it('covers empty input', function () {
24 const expected
= undefined;
25 const result
= common
.camelfy(empty
);
26 assert
.strictEqual(result
, expected
);
28 it('covers un-camelfiable input', function () {
30 const expected
= undefined;
31 const result
= common
.camelfy(bad
);
32 assert
.strictEqual(result
, expected
);
36 describe('freezeDeep', function () {
37 it('freezes things', function () {
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
));
53 describe('axiosResponseLogData', function () {
54 it('covers', function () {
59 'Content-Type': 'text/plain',
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.β',
68 'Content-Type': 'text/plain',
70 data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
72 const result
= common
.axiosResponseLogData(response
);
73 assert
.deepStrictEqual(result
, expected
);
75 it('covers no data', function () {
80 'Content-Type': 'text/plain',
87 'Content-Type': 'text/plain',
90 const result
= common
.axiosResponseLogData(response
);
91 assert
.deepStrictEqual(result
, expected
);
93 }); // axiosResponseLogData
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
);
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
);
108 describe('ensureArray', function () {
109 it('returns empty array for no data', function () {
110 const result
= common
.ensureArray();
111 assert
.deepStrictEqual(result
, []);
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
);
118 it('returns array containing non-array data', function () {
120 const result
= common
.ensureArray(data
);
121 assert
.deepStrictEqual(result
, [data
]);
125 describe('validError', function () {
126 it('covers valid', function () {
127 const result
= common
.validError('error');
128 assert
.strictEqual(result
, true);
130 it('covers invalid', function () {
131 const result
= common
.validError('π');
132 assert
.strictEqual(result
, false);
134 it('covers empty', function () {
135 const result
= common
.validError();
136 assert
.strictEqual(result
, false);
140 describe('validScope', function () {
141 it('covers valid', function () {
142 const result
= common
.validScope('scope');
143 assert
.strictEqual(result
, true);
145 it('covers invalid', function () {
146 const result
= common
.validScope('π');
147 assert
.strictEqual(result
, false);
149 it('covers empty', function () {
150 const result
= common
.validScope();
151 assert
.strictEqual(result
, false);
155 describe('newSecret', function () {
156 it('covers default', async
function () {
157 const result
= await common
.newSecret();
158 assert(result
.length
);
160 it('covers specified', async
function () {
161 const result
= await common
.newSecret(21);
162 assert(result
.length
);
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);
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
);