initial commit
[squeep-indieauth-helper] / test / lib / common.js
1 /* eslint-env mocha */
2 'use strict';
3
4 const assert = require('assert');
5 const common = require('../../lib/common');
6
7 describe('common', function () {
8 describe('fileScope', function () {
9 it('names a file path', function () {
10 const filename = 'lib/foo/bar.js';
11 const result = common.fileScope(filename)('baz');
12 assert.strictEqual(result, 'bar:baz');
13 });
14 it('names an index path', function () {
15 const filename = 'lib/foo/index.js';
16 const result = common.fileScope(filename)('baz');
17 assert.strictEqual(result, 'foo:baz');
18 });
19 }); // fileScope
20
21 describe('base64ToBase64URL', function () {
22 it('works', function () {
23 const b64 = 'ab/cd+e=';
24 const expected = 'ab_cd-e';
25 const result = common.base64ToBase64URL(b64);
26 assert.strictEqual(result, expected);
27 });
28 }); // base64ToBase64URL
29
30 describe('pick', function () {
31 it('picks', function () {
32 const srcObj = {
33 a: 1,
34 b: 2,
35 c: 3,
36 };
37 const result = common.pick(srcObj, ['a', 'c', 'd']);
38 assert.deepStrictEqual(result, {
39 'a': 1,
40 'c': 3,
41 });
42 });
43 }); // pick
44
45 describe('logTruncate', function () {
46 it('returns short string', function () {
47 const str = 'this is a short string';
48 const result = common.logTruncate(str, 100);
49 assert.strictEqual(result, str);
50 });
51 it('truncates long string', function () {
52 const str = 'this is not really a very long string but it is long enough for this test';
53 const result = common.logTruncate(str, 10);
54 assert(result.length < str.length);
55 });
56 }); // logTruncate
57
58 describe('axiosResponseLogData', function () {
59 it('covers', function () {
60 const response = {
61 status: 200,
62 statusText: 'OK',
63 headers: {
64 'Content-Type': 'text/plain',
65 },
66 otherData: 'blah',
67 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 };
69 const expected = {
70 status: 200,
71 statusText: 'OK',
72 headers: {
73 'Content-Type': 'text/plain',
74 },
75 data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
76 };
77 const result = common.axiosResponseLogData(response);
78 assert.deepStrictEqual(result, expected);
79 });
80 it('covers no data', function () {
81 const response = {
82 status: 200,
83 statusText: 'OK',
84 headers: {
85 'Content-Type': 'text/plain',
86 },
87 };
88 const expected = {
89 status: 200,
90 statusText: 'OK',
91 headers: {
92 'Content-Type': 'text/plain',
93 },
94 };
95 const result = common.axiosResponseLogData(response);
96 assert.deepStrictEqual(result, expected);
97 });
98 }); // axiosResponseLogData
99
100 }); // common