|
| 1 | +import type { |
| 2 | + ArrowFunctionExpression, |
| 3 | + ClassDeclaration, |
| 4 | + VariableDeclaration, |
| 5 | +} from '@babel/types'; |
| 6 | +import { parse, parseTypescript } from '../../../tests/utils'; |
| 7 | +import getTypeFromReactComponent from '../getTypeFromReactComponent.js'; |
| 8 | +import { describe, expect, test } from 'vitest'; |
| 9 | +import type { NodePath } from '@babel/traverse'; |
| 10 | + |
| 11 | +describe('getTypeFromReactComponent', () => { |
| 12 | + test('handles no stateless props', () => { |
| 13 | + const path = parseTypescript |
| 14 | + .statementLast<VariableDeclaration>(`const x = () => {}`) |
| 15 | + .get('declarations')[0] |
| 16 | + .get('init') as NodePath<ArrowFunctionExpression>; |
| 17 | + |
| 18 | + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); |
| 19 | + }); |
| 20 | + |
| 21 | + test('handles no class props', () => { |
| 22 | + const path = parseTypescript.statementLast<ClassDeclaration>( |
| 23 | + `import React from 'react'; |
| 24 | + class X extends React.Component { |
| 25 | + render() {} |
| 26 | + }`, |
| 27 | + ); |
| 28 | + |
| 29 | + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('TypeScript', () => { |
| 33 | + describe('stateless', () => { |
| 34 | + test('finds param type annotation', () => { |
| 35 | + const path = parseTypescript |
| 36 | + .statementLast<VariableDeclaration>(`const x = (props: Props) => {}`) |
| 37 | + .get('declarations')[0] |
| 38 | + .get('init') as NodePath<ArrowFunctionExpression>; |
| 39 | + |
| 40 | + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('classes', () => { |
| 45 | + test('finds props type in params', () => { |
| 46 | + const path = parseTypescript.statementLast<ClassDeclaration>( |
| 47 | + `import React from 'react'; |
| 48 | + class X extends React.Component<Props, State> { |
| 49 | + render() {} |
| 50 | + }`, |
| 51 | + ); |
| 52 | + |
| 53 | + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); |
| 54 | + }); |
| 55 | + |
| 56 | + test('finds props type in properties', () => { |
| 57 | + const path = parseTypescript.statementLast<ClassDeclaration>( |
| 58 | + `import React from 'react'; |
| 59 | + class X extends React.Component { |
| 60 | + props: Props; |
| 61 | + render() {} |
| 62 | + }`, |
| 63 | + ); |
| 64 | + |
| 65 | + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('Flow', () => { |
| 71 | + describe('stateless', () => { |
| 72 | + test('finds param type annotation', () => { |
| 73 | + const path = parse |
| 74 | + .statementLast<VariableDeclaration>(`const x = (props: Props) => {}`) |
| 75 | + .get('declarations')[0] |
| 76 | + .get('init') as NodePath<ArrowFunctionExpression>; |
| 77 | + |
| 78 | + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('classes', () => { |
| 83 | + test('finds props type in new params', () => { |
| 84 | + const path = parse.statementLast<ClassDeclaration>( |
| 85 | + `import React from 'react'; |
| 86 | + class X extends React.Component<Props, State> { |
| 87 | + render() {} |
| 88 | + }`, |
| 89 | + ); |
| 90 | + |
| 91 | + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); |
| 92 | + }); |
| 93 | + |
| 94 | + test('finds props type in old params', () => { |
| 95 | + const path = parse.statementLast<ClassDeclaration>( |
| 96 | + `import React from 'react'; |
| 97 | + class X extends React.Component<DefaultProps, Props, State> { |
| 98 | + render() {} |
| 99 | + }`, |
| 100 | + ); |
| 101 | + |
| 102 | + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); |
| 103 | + }); |
| 104 | + |
| 105 | + test('finds props type in properties', () => { |
| 106 | + const path = parse.statementLast<ClassDeclaration>( |
| 107 | + `import React from 'react'; |
| 108 | + class X extends React.Component { |
| 109 | + props: Props; |
| 110 | + render() {} |
| 111 | + }`, |
| 112 | + ); |
| 113 | + |
| 114 | + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments