Skip to content

Commit 8a07de7

Browse files
BenLorantfypvasek
authored andcommitted
add tests
1 parent 3ddb3b3 commit 8a07de7

File tree

4 files changed

+116
-13
lines changed

4 files changed

+116
-13
lines changed

src/__tests__/data/ColumnWithMethods.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,28 @@ export class Column extends React.Component<IColumnProps, {}> {
2626

2727
/**
2828
* My super cool method
29-
* @param myNum Documentation for parameter 1
29+
* @param myParam Documentation for parameter 1
3030
* @public
31-
* @returns The answer to the universe times myNum
31+
* @returns The answer to the universe
3232
*/
33-
static calculateAnswerr(myNum: number): number {
34-
return 42 * myNum;
33+
myCoolMethod(myParam: number, mySecondParam: string): number {
34+
return 42;
3535
}
3636

3737
/**
38-
* hello how are you
3938
* @public
4039
*/
41-
myNormalMethod() {
42-
return 78;
40+
myBasicMethod() {
41+
return null;
42+
}
43+
44+
/**
45+
* @public
46+
*/
47+
myArrowFunction = () => 23;
48+
49+
myPrivateFunction() {
50+
return 99;
4351
}
4452

4553
public render() {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as React from 'react';
2+
3+
/**
4+
* Column properties.
5+
*/
6+
export interface IColumnProps {
7+
/** prop1 description */
8+
prop1?: string;
9+
/** prop2 description */
10+
prop2: number;
11+
/**
12+
* prop3 description
13+
*/
14+
prop3: () => void;
15+
/** prop4 description */
16+
prop4: 'option1' | 'option2' | 'option3';
17+
}
18+
19+
/**
20+
* Column description
21+
*/
22+
export class Column extends React.Component<IColumnProps, {}> {
23+
public static defaultProps: Partial<IColumnProps> = {
24+
prop1: 'prop1'
25+
};
26+
27+
/**
28+
* My super cool static method
29+
* @param myParam Documentation for parameter 1
30+
* @public
31+
* @returns The answer to the universe
32+
*/
33+
static myStaticMethod(myParam: number, mySecondParam: string): number {
34+
return 42;
35+
}
36+
37+
public render() {
38+
const { prop1 } = this.props;
39+
return <div>{prop1}</div>;
40+
}
41+
}
42+
43+
export default Column;

src/__tests__/parser.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,4 +842,57 @@ describe('parser', () => {
842842
assert.equal(parsed.displayName, 'StatelessDisplayNameStyledComponent');
843843
});
844844
});
845+
846+
describe('methods', () => {
847+
it('should properly parse methods', () => {
848+
const [parsed] = parse(fixturePath('ColumnWithMethods'));
849+
const methods = parsed.methods;
850+
const myCoolMethod = methods[0];
851+
852+
assert.equal(myCoolMethod.description, 'My super cool method');
853+
assert.equal(myCoolMethod.docblock, 'My super cool method\n@param myParam Documentation for parameter 1\n@public\n@returns The answer to the universe'); // tslint:disable-line max-line-length
854+
assert.deepEqual(myCoolMethod.modifiers, []);
855+
assert.equal(myCoolMethod.name, 'myCoolMethod');
856+
assert.deepEqual(myCoolMethod.params, [
857+
{
858+
description: 'Documentation for parameter 1',
859+
name: 'myParam'
860+
},
861+
{
862+
description: null,
863+
name: 'mySecondParam'
864+
}
865+
]);
866+
assert.deepEqual(myCoolMethod.returns, {
867+
description: 'The answer to the universe',
868+
type: 'number'
869+
});
870+
});
871+
872+
it('should properly parse static methods', () => {
873+
const [parsed] = parse(fixturePath('ColumnWithStaticMethods'));
874+
const methods = parsed.methods;
875+
876+
assert.equal(methods[0].name, 'myStaticMethod');
877+
assert.deepEqual(methods[0].modifiers, ['static']);
878+
});
879+
880+
it('should handle method with no information', () => {
881+
const [parsed] = parse(fixturePath('ColumnWithMethods'));
882+
const methods = parsed.methods;
883+
assert.equal(methods[1].name, 'myBasicMethod');
884+
});
885+
886+
it('should handle arrow function', () => {
887+
const [parsed] = parse(fixturePath('ColumnWithMethods'));
888+
const methods = parsed.methods;
889+
assert.equal(methods[2].name, 'myArrowFunction');
890+
});
891+
892+
it('should not parse functions not marked with @public', () => {
893+
const [parsed] = parse(fixturePath('ColumnWithMethods'));
894+
const methods = parsed.methods;
895+
assert.equal(Boolean(methods.find((method => method.name === 'myPrivateFunction'))), false);
896+
});
897+
});
845898
});

src/parser.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export interface Method {
3535
name: string;
3636
docblock: string;
3737
modifiers: string[];
38-
params: Array<{ name: string, description?: string }>;
38+
params: Array<{ name: string, description?: string|null }>;
3939
returns?: {
4040
description?: string|null;
4141
type?: string;
@@ -232,8 +232,8 @@ export class Parser {
232232
const displayName =
233233
resolvedComponentName || computeComponentName(exp, source);
234234
const description = this.findDocComment(commentSource).fullComment;
235+
const methods = this.getMethodsInfo(type);
235236

236-
// here
237237
if (propsType) {
238238
const defaultProps = this.extractDefaultPropsFromComponent(exp, source);
239239
const props = this.getPropsInfo(propsType, defaultProps);
@@ -246,7 +246,6 @@ export class Parser {
246246
}
247247
}
248248

249-
const methods = this.getMethodsInfo(type);
250249
return {
251250
description,
252251
displayName,
@@ -257,7 +256,7 @@ export class Parser {
257256
return {
258257
description,
259258
displayName,
260-
methods: [],
259+
methods,
261260
props: {}
262261
};
263262
}
@@ -327,7 +326,7 @@ export class Parser {
327326
}
328327
});
329328

330-
if (type.symbol.members) {
329+
if (type.symbol && type.symbol.members) {
331330
type.symbol.members.forEach((member) => {
332331
methodSymbols.push(member);
333332
});
@@ -384,7 +383,7 @@ export class Parser {
384383
public getParameterInfo(callSignature: ts.Signature) {
385384
return callSignature.parameters.map((param) => {
386385
return {
387-
description: ts.displayPartsToString(param.getDocumentationComment(this.checker)),
386+
description: ts.displayPartsToString(param.getDocumentationComment(this.checker)) || null,
388387
name: param.getName()
389388
};
390389
});

0 commit comments

Comments
 (0)