|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +/*global jest, describe, beforeEach, it, expect*/ |
| 12 | + |
| 13 | +jest.autoMockOff(); |
| 14 | +jest.mock('../../Documentation'); |
| 15 | + |
| 16 | +describe('componentMethodsHandler', () => { |
| 17 | + let documentation; |
| 18 | + let componentMethodsHandler; |
| 19 | + let parse; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + ({parse} = require('../../../tests/utils')); |
| 23 | + documentation = new (require('../../Documentation')); |
| 24 | + componentMethodsHandler = require('../componentMethodsHandler'); |
| 25 | + }); |
| 26 | + |
| 27 | + function test(definition) { |
| 28 | + componentMethodsHandler(documentation, definition); |
| 29 | + expect(documentation.methods).toEqual([{ |
| 30 | + name: 'foo', |
| 31 | + description: 'The foo method', |
| 32 | + visibility: 'protected', |
| 33 | + modifiers: [], |
| 34 | + return: { |
| 35 | + description: 'The number', |
| 36 | + type: {name: 'number'}, |
| 37 | + }, |
| 38 | + params: [{ |
| 39 | + name: 'bar', |
| 40 | + description: 'The bar param', |
| 41 | + type: {name: 'number'}, |
| 42 | + }], |
| 43 | + }, { |
| 44 | + name: 'bar', |
| 45 | + description: 'Static function', |
| 46 | + visibility: 'public', |
| 47 | + modifiers: ['static'], |
| 48 | + return: null, |
| 49 | + params: [], |
| 50 | + }]); |
| 51 | + } |
| 52 | + |
| 53 | + it('extracts the documentation for an ObjectExpression', () => { |
| 54 | + const src = ` |
| 55 | + ({ |
| 56 | + /** |
| 57 | + * The foo method |
| 58 | + * @protected |
| 59 | + * @param bar The bar param |
| 60 | + * @returns The number |
| 61 | + */ |
| 62 | + foo(bar: number): number { |
| 63 | + return bar; |
| 64 | + }, |
| 65 | + statics: { |
| 66 | + /** |
| 67 | + * Static function |
| 68 | + */ |
| 69 | + bar() {} |
| 70 | + }, |
| 71 | + state: { |
| 72 | + foo: 'foo', |
| 73 | + }, |
| 74 | + componentDidMount() {}, |
| 75 | + render() { |
| 76 | + return null; |
| 77 | + }, |
| 78 | + }) |
| 79 | + `; |
| 80 | + |
| 81 | + test(parse(src).get('body', 0, 'expression')); |
| 82 | + }); |
| 83 | + |
| 84 | + it('extracts the documentation for a ClassDeclaration', () => { |
| 85 | + const src = ` |
| 86 | + class Test { |
| 87 | + /** |
| 88 | + * The foo method |
| 89 | + * @protected |
| 90 | + * @param bar The bar param |
| 91 | + * @returns The number |
| 92 | + */ |
| 93 | + foo(bar: number): number { |
| 94 | + return bar; |
| 95 | + } |
| 96 | +
|
| 97 | + /** |
| 98 | + * Static function |
| 99 | + */ |
| 100 | + static bar() {} |
| 101 | +
|
| 102 | + state = { |
| 103 | + foo: 'foo', |
| 104 | + }; |
| 105 | +
|
| 106 | + componentDidMount() {} |
| 107 | +
|
| 108 | + render() { |
| 109 | + return null; |
| 110 | + } |
| 111 | + } |
| 112 | + `; |
| 113 | + |
| 114 | + test(parse(src).get('body', 0)); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should not find methods for stateless components', () => { |
| 118 | + const src = ` |
| 119 | + (props) => {} |
| 120 | + `; |
| 121 | + |
| 122 | + const definition = parse(src).get('body', 0, 'expression'); |
| 123 | + componentMethodsHandler(documentation, definition); |
| 124 | + expect(documentation.methods).toEqual([]); |
| 125 | + }); |
| 126 | +}); |
0 commit comments