|
8 | 8 | *
|
9 | 9 | */
|
10 | 10 |
|
11 |
| -"use strict"; |
| 11 | +/*global jest, describe, beforeEach, it, expect*/ |
12 | 12 |
|
13 | 13 | jest.autoMockOff();
|
14 | 14 | jest.mock('../../Documentation');
|
15 | 15 |
|
16 | 16 | describe('componentDocblockHandler', () => {
|
17 |
| - var utils; |
| 17 | + var parse; |
18 | 18 | var documentation;
|
19 | 19 | var componentDocblockHandler;
|
20 | 20 |
|
21 |
| - function parse(src) { |
22 |
| - var programPath = utils.parse(src); |
| 21 | + function lastStatement(src) { |
| 22 | + var programPath = parse(src); |
23 | 23 | return programPath.get(
|
24 | 24 | 'body',
|
25 |
| - programPath.node.body.length - 1, |
26 |
| - 'declarations', |
27 |
| - 0, |
28 |
| - 'init', |
29 |
| - 'arguments', |
30 |
| - 0 |
| 25 | + programPath.node.body.length - 1 |
31 | 26 | );
|
32 | 27 | }
|
33 | 28 |
|
34 | 29 | beforeEach(() => {
|
35 |
| - utils = require('../../../tests/utils'); |
| 30 | + ({parse} = require('../../../tests/utils')); |
36 | 31 | documentation = new (require('../../Documentation'));
|
37 | 32 | componentDocblockHandler = require('../componentDocblockHandler');
|
38 | 33 | });
|
39 | 34 |
|
40 |
| - it('finds docblocks for component definitions', () => { |
41 |
| - var definition = parse([ |
42 |
| - '/**', |
43 |
| - ' * Component description', |
44 |
| - ' */', |
45 |
| - 'var Component = React.createClass({});', |
46 |
| - ].join('\n')); |
| 35 | + function test(definitionSrc, parse) { // eslint-disable-line no-shadow |
| 36 | + it('finds docblocks for component definitions', () => { |
| 37 | + var definition = parse(` |
| 38 | + /** |
| 39 | + * Component description |
| 40 | + */ |
| 41 | + ${definitionSrc} |
| 42 | + `); |
47 | 43 |
|
48 |
| - componentDocblockHandler(documentation, definition); |
49 |
| - expect(documentation.description).toBe('Component description'); |
50 |
| - }); |
| 44 | + componentDocblockHandler(documentation, definition); |
| 45 | + expect(documentation.description).toBe('Component description'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('ignores other types of comments', () => { |
| 49 | + var definition = parse(` |
| 50 | + /* |
| 51 | + * This is not a docblock', |
| 52 | + */ |
| 53 | + ${definitionSrc} |
| 54 | + `); |
| 55 | + |
| 56 | + componentDocblockHandler(documentation, definition); |
| 57 | + expect(documentation.description).toBe(''); |
| 58 | + |
| 59 | + definition = parse(` |
| 60 | + // Inline comment' |
| 61 | + ${definitionSrc} |
| 62 | + `); |
| 63 | + |
| 64 | + componentDocblockHandler(documentation, definition); |
| 65 | + expect(documentation.description).toBe(''); |
| 66 | + }); |
51 | 67 |
|
52 |
| - it('ignores other types of comments', () => { |
53 |
| - var definition = parse([ |
54 |
| - '/*', |
55 |
| - ' * This is not a docblock', |
56 |
| - ' */', |
57 |
| - 'var Component = React.createClass({});', |
58 |
| - ].join('\n')); |
| 68 | + it('only considers the docblock directly above the definition', () => { |
| 69 | + var definition = parse(` |
| 70 | + /** |
| 71 | + * This is the wrong docblock |
| 72 | + */ |
| 73 | + var something_else = "foo"; |
| 74 | + ${definitionSrc} |
| 75 | + `); |
59 | 76 |
|
60 |
| - componentDocblockHandler(documentation, definition); |
61 |
| - expect(documentation.description).toBe(''); |
| 77 | + componentDocblockHandler(documentation, definition); |
| 78 | + expect(documentation.description).toBe(''); |
| 79 | + }); |
| 80 | + } |
62 | 81 |
|
63 |
| - definition = parse([ |
64 |
| - '// Inline comment', |
65 |
| - 'var Component = React.createClass({});', |
66 |
| - ].join('\n')); |
| 82 | + describe('React.createClass', () => { |
| 83 | + test( |
| 84 | + 'var Component = React.createClass({})', |
| 85 | + src => lastStatement(src).get('declarations', 0, 'init', 'arguments', 0) |
| 86 | + ); |
| 87 | + }); |
67 | 88 |
|
68 |
| - componentDocblockHandler(documentation, definition); |
69 |
| - expect(documentation.description).toBe(''); |
| 89 | + describe('ClassDeclaration', () => { |
| 90 | + test( |
| 91 | + 'class Component {}', |
| 92 | + src => lastStatement(src) |
| 93 | + ); |
70 | 94 | });
|
71 | 95 |
|
72 |
| - it('only considers the docblock directly above the definition', () => { |
73 |
| - var definition = parse([ |
74 |
| - '/**', |
75 |
| - ' * This is the wrong docblock', |
76 |
| - ' */', |
77 |
| - 'var something_else = "foo";', |
78 |
| - 'var Component = React.createClass({});', |
79 |
| - ].join('\n')); |
80 |
| - |
81 |
| - componentDocblockHandler(documentation, definition); |
82 |
| - expect(documentation.description).toBe(''); |
| 96 | + describe('ClassExpression', () => { |
| 97 | + test( |
| 98 | + 'var Compoent = class {};', |
| 99 | + src => lastStatement(src).get('declarations', 0, 'init') |
| 100 | + ); |
83 | 101 | });
|
84 | 102 | });
|
0 commit comments