add some support for tickets, introspection method, minor fixes
[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('pick', function () {
22 it('picks', function () {
23 const srcObj = {
24 a: 1,
25 b: 2,
26 c: 3,
27 };
28 const result = common.pick(srcObj, ['a', 'c', 'd']);
29 assert.deepStrictEqual(result, {
30 'a': 1,
31 'c': 3,
32 });
33 });
34 }); // pick
35
36 describe('logTruncate', function () {
37 it('returns short string', function () {
38 const str = 'this is a short string';
39 const result = common.logTruncate(str, 100);
40 assert.strictEqual(result, str);
41 });
42 it('truncates long string', function () {
43 const str = 'this is not really a very long string but it is long enough for this test';
44 const result = common.logTruncate(str, 10);
45 assert(result.length < str.length);
46 });
47 }); // logTruncate
48
49 describe('axiosResponseLogData', function () {
50 it('covers', function () {
51 const response = {
52 status: 200,
53 statusText: 'OK',
54 headers: {
55 'Content-Type': 'text/plain',
56 },
57 otherData: 'blah',
58 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.”',
59 };
60 const expected = {
61 status: 200,
62 statusText: 'OK',
63 headers: {
64 'Content-Type': 'text/plain',
65 },
66 data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
67 };
68 const result = common.axiosResponseLogData(response);
69 assert.deepStrictEqual(result, expected);
70 });
71 it('covers no data', function () {
72 const response = {
73 status: 200,
74 statusText: 'OK',
75 headers: {
76 'Content-Type': 'text/plain',
77 },
78 };
79 const expected = {
80 status: 200,
81 statusText: 'OK',
82 headers: {
83 'Content-Type': 'text/plain',
84 },
85 };
86 const result = common.axiosResponseLogData(response);
87 assert.deepStrictEqual(result, expected);
88 });
89 }); // axiosResponseLogData
90
91 describe('setSymmetricDifference', function () {
92 it('covers difference', function () {
93 const setA = new Set([1, 2, 3]);
94 const setB = new Set([2, 3, 4]);
95 const expected = new Set([1, 4]);
96 const result = common.setSymmetricDifference(setA, setB);
97 assert(result.size);
98 assert.deepStrictEqual(result, expected);
99 });
100 it('covers no difference', function () {
101 const setA = new Set([1, 2, 3, 4]);
102 const setB = new Set([1, 2, 3, 4]);
103 const expected = new Set();
104 const result = common.setSymmetricDifference(setA, setB);
105 assert(!result.size);
106 assert.deepStrictEqual(result, expected);
107 });
108 }); // setSymmetricDifference
109
110 describe('properURLComponentName', function () {
111 it('maps proper names', function () {
112 [
113 ['hash', 'fragment'],
114 ['protocol', 'scheme'],
115 ['host', 'host'],
116 ].forEach(([name, expected]) => {
117 const result = common.properURLComponentName(name);
118 assert.strictEqual(result, expected);
119 });
120 });
121 }); // properURLComponentName
122
123 describe('formData', function () {
124 it('covers', function () {
125 const result = common.formData({
126 key: 'value',
127 foo: 'bar',
128 });
129 assert.strictEqual(result, 'key=value&foo=bar');
130 });
131 }); // formData
132
133 }); // common