Skip to content

Commit ee44087

Browse files
committed
tests
1 parent f377b33 commit ee44087

File tree

3 files changed

+177
-8
lines changed

3 files changed

+177
-8
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import {
5+
extractTypesAndDescription,
6+
processListItemsIntoProperties,
7+
generateSignatures,
8+
getFullName,
9+
} from '../createSignatureElements.mjs';
10+
11+
describe('createSignatureElements', () => {
12+
describe('extractTypesAndDescription', () => {
13+
it('should extract types and description from content nodes', () => {
14+
const contentNodes = [
15+
{ type: 'link', children: [{ type: 'inlineCode', value: '<string>' }] },
16+
{ type: 'text', value: ' | ' },
17+
{ type: 'link', children: [{ type: 'inlineCode', value: '<number>' }] },
18+
{ type: 'text', value: ' - This is a description' },
19+
];
20+
21+
const result = extractTypesAndDescription(contentNodes);
22+
23+
assert.equal(result.types.length, 3);
24+
assert.equal(result.description.length, 1);
25+
assert.equal(result.description[0].value, ' - This is a description');
26+
});
27+
28+
it('should handle generic types correctly', () => {
29+
const contentNodes = [
30+
{ type: 'link', children: [{ type: 'inlineCode', value: '<T>' }] },
31+
{ type: 'text', value: ' - Description of generic' },
32+
];
33+
34+
const result = extractTypesAndDescription(contentNodes);
35+
36+
assert.equal(result.types.length, 1);
37+
assert.equal(result.types[0].children[0].value, '<T>');
38+
assert.equal(result.description.length, 1);
39+
});
40+
41+
it('should handle content with no types', () => {
42+
const contentNodes = [{ type: 'text', value: 'Only description' }];
43+
44+
const result = extractTypesAndDescription(contentNodes);
45+
46+
assert.equal(result.types.length, 0);
47+
assert.equal(result.description.length, 1);
48+
assert.equal(result.description[0].value, 'Only description');
49+
});
50+
});
51+
52+
describe('processListItemsIntoProperties', () => {
53+
it('should convert list items into property data objects', () => {
54+
const listItems = [
55+
{
56+
children: [
57+
{
58+
type: 'paragraph',
59+
children: [
60+
{ type: 'text', value: 'propName' },
61+
{
62+
type: 'link',
63+
children: [{ type: 'inlineCode', value: '<string>' }],
64+
},
65+
{ type: 'text', value: ' - Description text' },
66+
],
67+
},
68+
],
69+
},
70+
];
71+
72+
const result = processListItemsIntoProperties(listItems);
73+
74+
assert.equal(result.length, 1);
75+
assert.ok(result[0].hasTypes);
76+
assert.ok(result[0].hasDescription);
77+
});
78+
});
79+
80+
describe('generateSignatures', () => {
81+
it('should generate a single signature when no optional parameters', () => {
82+
const functionName = 'testFunction';
83+
const signature = {
84+
params: [
85+
{ name: 'param1', optional: false },
86+
{ name: 'param2', optional: false },
87+
],
88+
return: { type: 'string' },
89+
};
90+
91+
const result = generateSignatures(functionName, signature);
92+
93+
assert.equal(result.length, 1);
94+
assert.equal(result[0], 'testFunction(param1, param2): string');
95+
});
96+
97+
it('should generate multiple signatures for optional parameters', () => {
98+
const functionName = 'testFunction';
99+
const signature = {
100+
params: [
101+
{ name: 'required', optional: false },
102+
{ name: 'optional1', optional: true },
103+
{ name: 'optional2', optional: true },
104+
],
105+
return: { type: 'number' },
106+
};
107+
108+
const result = generateSignatures(functionName, signature);
109+
110+
assert.equal(result.length, 4);
111+
assert.ok(result.includes('testFunction(required): number'));
112+
assert.ok(result.includes('testFunction(required, optional1?): number'));
113+
assert.ok(result.includes('testFunction(required, optional2?): number'));
114+
assert.ok(
115+
result.includes(
116+
'testFunction(required, optional1?, optional2?): number'
117+
)
118+
);
119+
});
120+
121+
it('should handle functions without return type', () => {
122+
const functionName = 'voidFunction';
123+
const signature = {
124+
params: [{ name: 'param', optional: false }],
125+
return: null,
126+
};
127+
128+
const result = generateSignatures(functionName, signature);
129+
130+
assert.equal(result[0], 'voidFunction(param)');
131+
});
132+
});
133+
134+
describe('getFullName', () => {
135+
it('should extract function name from code in text', () => {
136+
const heading = {
137+
name: 'functionName',
138+
text: 'The `obj.functionName` function',
139+
};
140+
141+
const result = getFullName(heading);
142+
143+
assert.equal(result, 'obj.functionName');
144+
});
145+
146+
it('should return just the name when no code is found', () => {
147+
const heading = {
148+
name: 'functionName',
149+
text: 'The functionName function',
150+
};
151+
152+
const result = getFullName(heading);
153+
154+
assert.equal(result, 'functionName');
155+
});
156+
157+
it('should handle complex function names', () => {
158+
const heading = {
159+
name: 'add',
160+
text: 'Using `math.add` to add numbers',
161+
};
162+
163+
const result = getFullName(heading);
164+
165+
assert.equal(result, 'math.add');
166+
});
167+
});
168+
});

src/generators/jsx-ast/utils/createSignatureElements.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import parseSignature from '../../legacy-json/utils/parseSignature.mjs';
88
* Extracts types and description from content nodes
99
* @param {import('@types/mdast').Node[]} contentNodes - Content nodes to process
1010
*/
11-
const extractTypesAndDescription = contentNodes => {
11+
export const extractTypesAndDescription = contentNodes => {
1212
const types = [];
1313
let i = 0;
1414

@@ -51,7 +51,7 @@ const extractTypesAndDescription = contentNodes => {
5151
* @param {import('@types/mdast').ListItem[]} items - List items to process
5252
* @param {string} prefix - Property name prefix for nested properties
5353
*/
54-
const processListItemsIntoProperties = (items, prefix = '') => {
54+
export const processListItemsIntoProperties = (items, prefix = '') => {
5555
const properties = [];
5656

5757
for (const item of items) {
@@ -123,7 +123,7 @@ const processListItemsIntoProperties = (items, prefix = '') => {
123123
* Creates a table from a list of properties
124124
* @param {import('mdast').List} node
125125
*/
126-
const createPropertyTable = node => {
126+
export const createPropertyTable = node => {
127127
// Process all properties
128128
const allProperties = processListItemsIntoProperties(node.children);
129129

@@ -166,7 +166,7 @@ const createPropertyTable = node => {
166166
* @param {string} functionName - Name of the function
167167
* @param {import('../../legacy-json/types.d.ts').MethodSignature} signature - Signature object
168168
*/
169-
const generateSignatures = (functionName, signature) => {
169+
export const generateSignatures = (functionName, signature) => {
170170
const { params, return: returnType } = signature;
171171
const returnStr = returnType ? `: ${returnType.type}` : '';
172172

@@ -219,7 +219,7 @@ const generateSignatures = (functionName, signature) => {
219219
* @param {string} functionName - Name of the function
220220
* @param {import('../../legacy-json/types.d.ts').MethodSignature} signature - Signature object
221221
*/
222-
const createSignatureCodeBlock = (functionName, signature) => {
222+
export const createSignatureCodeBlock = (functionName, signature) => {
223223
const signatures = generateSignatures(functionName, signature);
224224
const codeContent = signatures.join('\n');
225225
return createElement('pre', codeContent);

src/generators/legacy-json/utils/parseSignature.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { PARAM_EXPRESSION } from '../constants.mjs';
55
const OPTIONAL_LEVEL_CHANGES = { '[': 1, ']': -1, ' ': 0 };
66

77
/**
8-
* @param {String} char
98
* @param {Number} depth
9+
* @param {String} char
1010
* @returns {Number}
1111
*/
12-
const updateDepth = (char, depth) =>
13-
depth + (OPTIONAL_LEVEL_CHANGES[char] || 0);
12+
const updateDepth = (depth, char) => {
13+
return depth + (OPTIONAL_LEVEL_CHANGES[char] || 0);
14+
};
1415

1516
/**
1617
* @param {string} parameterName

0 commit comments

Comments
 (0)