|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | + * Copyright 2016-Present Datadog, Inc. |
| 4 | + */ |
| 5 | + |
| 6 | +import * as Babel from '@babel/core'; |
| 7 | + |
| 8 | +import { getNodeName } from '../src/utils/nodeProcessing'; |
| 9 | + |
| 10 | +const t = Babel.types; |
| 11 | + |
| 12 | +describe('getNodeName', () => { |
| 13 | + it('should return the string as-is when given a string', () => { |
| 14 | + expect(getNodeName(t, 'View')).toBe('View'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should return the name of a JSXIdentifier', () => { |
| 18 | + const node = t.jsxIdentifier('Text'); |
| 19 | + expect(getNodeName(t, node)).toBe('Text'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return dotted name for a JSXMemberExpression', () => { |
| 23 | + const node = t.jsxMemberExpression( |
| 24 | + t.jsxIdentifier('Card'), |
| 25 | + t.jsxIdentifier('Title') |
| 26 | + ); |
| 27 | + expect(getNodeName(t, node)).toBe('Card.Title'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return dotted name for a deeply nested JSXMemberExpression', () => { |
| 31 | + const node = t.jsxMemberExpression( |
| 32 | + t.jsxMemberExpression(t.jsxIdentifier('A'), t.jsxIdentifier('B')), |
| 33 | + t.jsxIdentifier('C') |
| 34 | + ); |
| 35 | + expect(getNodeName(t, node)).toBe('A.B.C'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return null for a node without a name property', () => { |
| 39 | + const node = t.numericLiteral(42); |
| 40 | + expect(getNodeName(t, node)).toBeNull(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return null for an empty statement node', () => { |
| 44 | + const node = t.emptyStatement(); |
| 45 | + expect(getNodeName(t, node)).toBeNull(); |
| 46 | + }); |
| 47 | +}); |
0 commit comments