-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathhandlers.test.js
More file actions
113 lines (101 loc) · 3.08 KB
/
handlers.test.js
File metadata and controls
113 lines (101 loc) · 3.08 KB
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
import { describe } from '@asdgf/cli';
import * as assert from 'uvu/assert';
import { handleJsDoc } from '../../../../src/features/analyse-phase/creators/handlers.js';
import { getNodesByCriteria } from '../../../../test-helpers/index.js';
/**
* Helper function that finds first jsdoc node of file and runs handleJsDoc to get the result that
* would be put in cem.
* @param {string} file
*/
function getJsDocOutputFromFile(file) {
const foundNodes = getNodesByCriteria(file, [
{ name: 'jsDoc', fn: (node) => Boolean(node.jsDoc) },
]);
const jsDocNode = foundNodes?.jsDoc?.[0];
return handleJsDoc({}, jsDocNode);
}
const createFile = (/** @type {string} */ jsdocLines) => `
class X extends HTMLElement {
/**
${jsdocLines}
*/
callMe() {
return 'thanks';
}
}`;
function getJsDocOutputFromFragment(fragment) {
return getJsDocOutputFromFile(createFile(fragment));
}
describe('handleJsDoc', ({ it }) => {
describe('Parameters', () => {
it("creates a 'parameters' key (of type {name: string, type: {text: string}})", async () => {
const res1 = getJsDocOutputFromFragment('* @param {string} x');
assert.equal(res1.parameters, [
{
name: 'x',
type: {
text: 'string',
},
},
]);
const res2 = getJsDocOutputFromFragment('');
assert.equal(res2.parameters, undefined);
});
it('supports descriptions', async () => {
const result = getJsDocOutputFromFragment(`* @param {string} a text here`);
assert.equal(result.parameters, [
{
description: 'text here',
name: 'a',
type: {
text: 'string',
},
},
]);
});
it('recognizes multiple params', async () => {
const result = getJsDocOutputFromFragment(`
* @param {string} a
* @protected
* @param {number} b`);
assert.equal(result.parameters, [
{
name: 'a',
type: {
text: 'string',
},
},
{
name: 'b',
type: {
text: 'number',
},
},
]);
});
// TODO: implement this in code
it.skip('recognizes object params built from multiple `@param` tags', async () => {
const result = getJsDocOutputFromFragment(`
* @param {object} opts
* @param {string} [opts.currency]`);
assert.equal(result, parameters, [
{
name: 'opts',
type: {
text: '{currency?: string}',
},
},
]);
});
});
describe('Privacy', () => {
it("creates a 'privacy' key (of type 'public'|'protected'|'private')", async () => {
assert.equal(getJsDocOutputFromFragment('* @public').privacy, 'public');
assert.equal(getJsDocOutputFromFragment('* @protected').privacy, 'protected');
assert.equal(getJsDocOutputFromFragment('* @private').privacy, 'private');
});
it("creates no 'privacy' key when not explicitly declared via @public tag", async () => {
assert.equal(getJsDocOutputFromFragment('').privacy, undefined);
});
});
});