-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathutils.spec.ts
184 lines (157 loc) · 6.71 KB
/
utils.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { DocumentationTag } from '@electron/docs-parser';
import { describe, expect, it } from 'vitest';
import * as utils from '../src/utils.js';
describe('utils', () => {
describe('extendArray', () => {
it('should return an array with all elements added correctly', () => {
expect(utils.extendArray(['foo'], ['bar'])).toEqual(['foo', 'bar']);
});
it('should return an array with all elements added in the correct oreder', () => {
expect(utils.extendArray([1, 2, 3, 4], [2, 3, 4, 5])).toEqual([1, 2, 3, 4, 2, 3, 4, 5]);
});
it('should mutate the original array', () => {
const primary = [1, 5, 9];
const secondary = [2, 6, 10];
utils.extendArray(primary, secondary);
expect(primary).toEqual([1, 5, 9, 2, 6, 10]);
});
});
describe('wrapComment', () => {
it('should return an array', () => {
expect(utils.wrapComment('Foo Bar')).toHaveLength(3);
});
it('should be a correctly formatted JS multi-line comment', () => {
const wrapped = utils.wrapComment('Foo bar');
expect(wrapped[0]).toEqual('/**');
expect(wrapped[wrapped.length - 1]).toEqual(' */');
});
it('should wrap each line to be a max of 80 chars', () => {
const reallyLongString =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pulvinar nibh eu orci fringilla interdum. In mi arcu, accumsan nec justo eget, pharetra egestas mauris. Quisque nisl tellus, sagittis lobortis commodo nec, tincidunt a arcu. Donec congue lacus a lacus euismod, in hendrerit nunc faucibus. Praesent ac libero eros. Nunc lorem turpis, elementum vel pellentesque vitae, aliquet et erat. In tempus, nulla vitae cursus congue, massa dui pretium eros, eget ornare ipsum diam a velit. Aliquam ac iaculis dui. Phasellus mollis augue volutpat turpis posuere scelerisque. Donec a rhoncus nisl, eu viverra massa. Suspendisse rutrum fermentum diam, posuere tempus turpis accumsan in. Pellentesque commodo in leo vitae aliquet. Vestibulum id justo ac odio mollis fringilla ac a odio. Quisque rhoncus pretium risus, tristique convallis urna.';
const wrapped = utils.wrapComment(reallyLongString);
wrapped.forEach((line) => {
// Subtract 3 due to commend prefix " * "
expect(line.length - 3).toBeLessThanOrEqual(80);
});
});
it('should not split words unless it needs to', () => {
const wrapped = utils.wrapComment(
'Thisisalongword Thisisalongword Thisisalongword Thisisalongword Thisisalongword Thisisalongword Thisisalongword Thisisalongword',
);
wrapped.forEach((line, index) => {
if (index === 0 || index === wrapped.length - 1) return;
expect(line.endsWith('Thisisalongword')).toEqual(true);
});
});
it('should handle long urls and not create needless empty lines', () => {
const wrapped = utils.wrapComment(
'Unregisters the app from notifications received from APNS. See: https://developer.apple.com/documentation/appkit/nsapplication/1428747-unregisterforremotenotifications?language=objc',
);
expect(wrapped.length).toEqual(4);
});
it('should create a tag-only comment', () => {
const wrapped = utils.wrapComment('', [DocumentationTag.STABILITY_DEPRECATED]);
expect(wrapped.length).toEqual(3);
expect(wrapped[0]).toEqual('/**');
expect(wrapped[1].endsWith('@deprecated')).toEqual(true);
expect(wrapped[wrapped.length - 1]).toEqual(' */');
});
});
describe('typify', () => {
it('should lower case known types', () => {
expect(utils.typify('String')).toEqual('string');
expect(utils.typify('Number')).toEqual('number');
});
it('should convert specific number types to typescript types', () => {
expect(utils.typify('Integer')).toEqual('number');
expect(utils.typify('Float')).toEqual('number');
expect(utils.typify('Double')).toEqual('number');
expect(utils.typify('Number')).toEqual('number');
});
it('should correctly convert a void function', () => {
expect(utils.typify('VoidFunction')).toEqual('(() => void)');
});
it('should lower case known array types', () => {
expect(utils.typify('String[]')).toEqual('string[]');
expect(utils.typify('Number[]')).toEqual('number[]');
});
it('should map an array of types through typify as well', () => {
expect(utils.typify(['String', 'Float', 'Boolean'])).toEqual(
'(string) | (number) | (boolean)',
);
});
it('should map an array of types through typify as well and remove duplicates', () => {
expect(utils.typify(['String', 'Float', 'Double'])).toEqual('(string) | (number)');
});
it('should map node objects to the correct type', () => {
expect(utils.typify('buffer')).toEqual('Buffer');
});
it('should convert custom types with inner types', () => {
expect(
utils.typify({
collection: false,
innerTypes: [
{
collection: false,
type: 'T',
},
],
type: 'Foo',
}),
).toEqual('Foo<T>');
expect(
utils.typify({
collection: false,
innerTypes: [
{
collection: false,
type: 'A',
},
{
collection: false,
type: 'B',
},
],
type: 'Foo',
}),
).toEqual('Foo<A, B>');
});
});
describe('paramify', () => {
it('should pass through most param names', () => {
expect(utils.paramify('foo')).toEqual('foo');
});
it('should clean reserved words', () => {
expect(utils.paramify('switch')).toEqual('the_switch');
});
});
describe('isEmitter', () => {
it('should return true on most modules', () => {
expect(utils.isEmitter({ name: 'app', type: 'Module', events: [1] } as any)).toEqual(true);
});
it('should return false for specific non-emitter modules', () => {
expect(
utils.isEmitter({
name: 'menuitem',
type: 'Class',
instanceEvents: [],
instanceMethods: [],
} as any),
).toEqual(false);
});
});
describe('isOptional', () => {
it('should return true if param is not required', () => {
expect(utils.isOptional({} as any)).toEqual(true);
});
it('should return false if param is required', () => {
expect(utils.isOptional({ required: true } as any)).toEqual(false);
});
it('should default to true if param is a non-function', () => {
expect(utils.isOptional({ type: 'Foo' } as any)).toEqual(true);
});
it('should default to false if param is a function', () => {
expect(utils.isOptional({ type: 'Function' } as any)).toEqual(false);
});
});
});