|
| 1 | +import type { NodePath } from '@babel/traverse'; |
| 2 | +import { parse, noopImporter } from '../../../tests/utils'; |
| 3 | +import FindAnnotatedDefinitionsResolver from '../FindAnnotatedDefinitionsResolver.js'; |
| 4 | +import { describe, expect, test } from 'vitest'; |
| 5 | + |
| 6 | +describe('FindAnnotatedDefinitionsResolver', () => { |
| 7 | + const resolver = new FindAnnotatedDefinitionsResolver(); |
| 8 | + |
| 9 | + function findComponentsInSource( |
| 10 | + source: string, |
| 11 | + importer = noopImporter, |
| 12 | + ): NodePath[] { |
| 13 | + return resolver.resolve(parse(source, {}, importer, true)); |
| 14 | + } |
| 15 | + |
| 16 | + describe('class definitions', () => { |
| 17 | + test('finds ClassDeclaration with line comment', () => { |
| 18 | + const source = ` |
| 19 | + // @component |
| 20 | + class Component {} |
| 21 | + `; |
| 22 | + |
| 23 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 24 | + }); |
| 25 | + |
| 26 | + test('finds ClassDeclaration with line comment and empty line', () => { |
| 27 | + const source = ` |
| 28 | + // @component |
| 29 | +
|
| 30 | + class Component {} |
| 31 | + `; |
| 32 | + |
| 33 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 34 | + }); |
| 35 | + |
| 36 | + test('finds ClassDeclaration with block comment', () => { |
| 37 | + const source = ` |
| 38 | + /* @component */ |
| 39 | + class Component {} |
| 40 | + `; |
| 41 | + |
| 42 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 43 | + }); |
| 44 | + |
| 45 | + test('finds ClassDeclaration with block comment and empty line', () => { |
| 46 | + const source = ` |
| 47 | + /* @component */ |
| 48 | +
|
| 49 | + class Component {} |
| 50 | + `; |
| 51 | + |
| 52 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 53 | + }); |
| 54 | + |
| 55 | + test('finds ClassExpression', () => { |
| 56 | + const source = ` |
| 57 | + // @component |
| 58 | + const Component = class {} |
| 59 | + `; |
| 60 | + |
| 61 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 62 | + }); |
| 63 | + |
| 64 | + test('finds ClassExpression with assignment', () => { |
| 65 | + const source = ` |
| 66 | + let Component; |
| 67 | + // @component |
| 68 | + Component = class {} |
| 69 | + `; |
| 70 | + |
| 71 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 72 | + }); |
| 73 | + |
| 74 | + test('finds nothing when not annotated', () => { |
| 75 | + const source = ` |
| 76 | + class ComponentA {} |
| 77 | + const ComponentB = class {} |
| 78 | + `; |
| 79 | + |
| 80 | + const result = findComponentsInSource(source); |
| 81 | + |
| 82 | + expect(Array.isArray(result)).toBe(true); |
| 83 | + expect(result.length).toBe(0); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('stateless components', () => { |
| 88 | + test('finds ArrowFunctionExpression', () => { |
| 89 | + const source = ` |
| 90 | + // @component |
| 91 | + const Component = () => {}; |
| 92 | + `; |
| 93 | + |
| 94 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 95 | + }); |
| 96 | + |
| 97 | + test('finds FunctionDeclaration', () => { |
| 98 | + const source = ` |
| 99 | + // @component |
| 100 | + function Component() {} |
| 101 | + `; |
| 102 | + |
| 103 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 104 | + }); |
| 105 | + |
| 106 | + test('finds FunctionExpression', () => { |
| 107 | + const source = ` |
| 108 | + // @component |
| 109 | + const Component = function() {}; |
| 110 | + `; |
| 111 | + |
| 112 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 113 | + }); |
| 114 | + |
| 115 | + test('finds ObjectMethod', () => { |
| 116 | + const source = ` |
| 117 | + const obj = { |
| 118 | + // @component |
| 119 | + component() {} |
| 120 | + }; |
| 121 | + `; |
| 122 | + |
| 123 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 124 | + }); |
| 125 | + |
| 126 | + test('finds ObjectProperty', () => { |
| 127 | + const source = ` |
| 128 | + const obj = { |
| 129 | + // @component |
| 130 | + component: function() {} |
| 131 | + }; |
| 132 | + `; |
| 133 | + |
| 134 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 135 | + }); |
| 136 | + |
| 137 | + test('finds nothing when not annotated', () => { |
| 138 | + const source = ` |
| 139 | + const ComponentA = () => {}; |
| 140 | + function ComponentB() {} |
| 141 | + const ComponentC = function() {}; |
| 142 | + const obj = { component() {} }; |
| 143 | + const obj2 = { component: function() {} }; |
| 144 | + `; |
| 145 | + |
| 146 | + const result = findComponentsInSource(source); |
| 147 | + |
| 148 | + expect(Array.isArray(result)).toBe(true); |
| 149 | + expect(result.length).toBe(0); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + test('finds component wrapped in HOC', () => { |
| 154 | + const source = ` |
| 155 | + // @component |
| 156 | + const Component = React.memo(() => {}); |
| 157 | + `; |
| 158 | + |
| 159 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 160 | + }); |
| 161 | + |
| 162 | + test('finds component wrapped in two HOCs', () => { |
| 163 | + const source = ` |
| 164 | + // @component |
| 165 | + const Component = React.memo(otherHOC(() => {})); |
| 166 | + `; |
| 167 | + |
| 168 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 169 | + }); |
| 170 | + |
| 171 | + test('finds component wrapped in function', () => { |
| 172 | + const source = ` |
| 173 | + function X () { |
| 174 | + // @component |
| 175 | + const subcomponent = class {} |
| 176 | + } |
| 177 | + `; |
| 178 | + |
| 179 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 180 | + }); |
| 181 | + |
| 182 | + test('does not traverse up ArrayExpressions', () => { |
| 183 | + const source = ` |
| 184 | + // @component |
| 185 | + const arr = [ |
| 186 | + function() {}, |
| 187 | + function() {} |
| 188 | + ] |
| 189 | + `; |
| 190 | + |
| 191 | + const result = findComponentsInSource(source); |
| 192 | + |
| 193 | + expect(Array.isArray(result)).toBe(true); |
| 194 | + expect(result.length).toBe(0); |
| 195 | + }); |
| 196 | + |
| 197 | + test('does not traverse up function parameters', () => { |
| 198 | + const source = ` |
| 199 | + // @component |
| 200 | + function x(component = () => {}) {} |
| 201 | + `; |
| 202 | + |
| 203 | + const result = findComponentsInSource(source); |
| 204 | + |
| 205 | + expect(Array.isArray(result)).toBe(true); |
| 206 | + expect(result.length).toBe(1); |
| 207 | + expect(result[0].type).not.toBe('ArrowFunctionExpression'); |
| 208 | + }); |
| 209 | + |
| 210 | + test('finds function parameter with annotation', () => { |
| 211 | + const source = ` |
| 212 | + function x(component = /* @component */() => {}) {} |
| 213 | + `; |
| 214 | + |
| 215 | + expect(findComponentsInSource(source)).toMatchSnapshot(); |
| 216 | + }); |
| 217 | +}); |
0 commit comments