|
| 1 | +import * as t from '@babel/types'; |
| 2 | + |
| 3 | +import { parseDirective } from '../../src/utils/parseDirective'; |
| 4 | + |
| 5 | +describe('parseDirective', () => { |
| 6 | + const makePath = (node: any, parent: any = {}) => ({ node, parent }); |
| 7 | + const makeDirective = (value: string) => |
| 8 | + ({ value: { value } }) as t.Directive; |
| 9 | + |
| 10 | + it('parses use trace dev', () => { |
| 11 | + const path = makePath({ id: { name: 'MyComponent' } }); |
| 12 | + const directive = makeDirective('use trace dev'); |
| 13 | + const result = parseDirective(path as any, directive, 'loc'); |
| 14 | + expect(result).toEqual({ |
| 15 | + componentName: 'MyComponent', |
| 16 | + isFunctionComponent: true, |
| 17 | + apiToUse: 'dev', |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('parses use trace otel', () => { |
| 22 | + const path = makePath({ id: { name: 'MyComponent' } }); |
| 23 | + const directive = makeDirective('use trace otel'); |
| 24 | + const result = parseDirective(path as any, directive, 'loc'); |
| 25 | + expect(result.apiToUse).toBe('otel'); |
| 26 | + expect(result.componentName).toBe('MyComponent'); |
| 27 | + expect(result.isFunctionComponent).toBe(true); |
| 28 | + }); |
| 29 | + |
| 30 | + it('parses use trace dev MyComponent', () => { |
| 31 | + const path = makePath({ id: { name: 'OtherComponent' } }); |
| 32 | + const directive = makeDirective('use trace dev MyComponent'); |
| 33 | + const result = parseDirective(path as any, directive, 'loc'); |
| 34 | + expect(result.apiToUse).toBe('dev'); |
| 35 | + expect(result.componentName).toBe('MyComponent'); |
| 36 | + expect(result.isFunctionComponent).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it('parses use trace otel MyComponent', () => { |
| 40 | + const path = makePath({ id: { name: 'OtherComponent' } }); |
| 41 | + const directive = makeDirective('use trace otel MyComponent'); |
| 42 | + const result = parseDirective(path as any, directive, 'loc'); |
| 43 | + expect(result.apiToUse).toBe('otel'); |
| 44 | + expect(result.componentName).toBe('MyComponent'); |
| 45 | + expect(result.isFunctionComponent).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('parses use trace MyComponent', () => { |
| 49 | + const path = makePath({ id: { name: 'OtherComponent' } }); |
| 50 | + const directive = makeDirective('use trace MyComponent'); |
| 51 | + const result = parseDirective(path as any, directive, 'loc'); |
| 52 | + expect(result.apiToUse).toBe('dev'); |
| 53 | + expect(result.componentName).toBe('MyComponent'); |
| 54 | + expect(result.isFunctionComponent).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('throws on invalid API', () => { |
| 58 | + const path = makePath({ id: { name: 'OtherComponent' } }); |
| 59 | + const directive = makeDirective('use trace invalidAPI MyComponent'); |
| 60 | + expect(() => parseDirective(path as any, directive, 'loc')).toThrow( |
| 61 | + /Invalid tracing API specified/ |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('throws on anonymous export default', () => { |
| 66 | + const path = makePath({}, { type: 'ExportDefaultDeclaration' }); |
| 67 | + const directive = makeDirective('use trace dev'); |
| 68 | + expect(() => parseDirective(path as any, directive, 'loc')).toThrow( |
| 69 | + /anonymous default export/ |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it('infers name from variable assignment', () => { |
| 74 | + const path = makePath( |
| 75 | + {}, |
| 76 | + { |
| 77 | + type: 'VariableDeclarator', |
| 78 | + id: { type: 'Identifier', name: 'VarComponent' }, |
| 79 | + } |
| 80 | + ); |
| 81 | + const directive = makeDirective('use trace dev'); |
| 82 | + const result = parseDirective(path as any, directive, 'loc'); |
| 83 | + expect(result.componentName).toBe('VarComponent'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('infers name from object property', () => { |
| 87 | + const path = makePath( |
| 88 | + {}, |
| 89 | + { |
| 90 | + type: 'ObjectProperty', |
| 91 | + key: { type: 'Identifier', name: 'ObjComponent' }, |
| 92 | + } |
| 93 | + ); |
| 94 | + const directive = makeDirective('use trace dev'); |
| 95 | + const result = parseDirective(path as any, directive, 'loc'); |
| 96 | + expect(result.componentName).toBe('ObjComponent'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('infers name from class method and sets isFunctionComponent false', () => { |
| 100 | + const path = makePath( |
| 101 | + {}, |
| 102 | + { |
| 103 | + type: 'ClassMethod', |
| 104 | + key: { type: 'Identifier', name: 'MethodComponent' }, |
| 105 | + } |
| 106 | + ); |
| 107 | + const directive = makeDirective('use trace dev'); |
| 108 | + const result = parseDirective(path as any, directive, 'loc'); |
| 109 | + expect(result.componentName).toBe('MethodComponent'); |
| 110 | + expect(result.isFunctionComponent).toBe(false); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns unknown if no name found', () => { |
| 114 | + const path = makePath({}); |
| 115 | + const directive = makeDirective('use trace dev'); |
| 116 | + const result = parseDirective(path as any, directive, 'loc'); |
| 117 | + expect(result.componentName).toBe('unknown'); |
| 118 | + }); |
| 119 | +}); |
0 commit comments