|
| 1 | +import { readFileSync } from 'fs'; |
| 2 | + |
1 | 3 | import { ClassDeclaration } from '../src/declarations/ClassDeclaration'; |
2 | 4 | import { DeclarationVisibility } from '../src/declarations/DeclarationVisibility'; |
3 | 5 | import { DefaultDeclaration } from '../src/declarations/DefaultDeclaration'; |
@@ -560,6 +562,164 @@ describe('TypescriptParser', () => { |
560 | 562 | expect(usages).toContain('complexComp'); |
561 | 563 | expect(usages).toContain('SingleComp'); |
562 | 564 | }); |
| 565 | + |
| 566 | + it('should parse functions inside {}', () => { |
| 567 | + const usages = parsed.usages; |
| 568 | + |
| 569 | + expect(usages).toContain('myFunc'); |
| 570 | + }); |
| 571 | + |
| 572 | + it('should parse component functions inside {}', () => { |
| 573 | + const usages = parsed.usages; |
| 574 | + |
| 575 | + expect(usages).toContain('MyFunc'); |
| 576 | + }); |
| 577 | + |
| 578 | + it('should parse a component inside a map', () => { |
| 579 | + const usages = parsed.usages; |
| 580 | + |
| 581 | + expect(usages).toContain('AnotherComp'); |
| 582 | + expect(usages).toContain('foobarVariable'); |
| 583 | + }); |
| 584 | + |
| 585 | + it('should parse a function inside a map', () => { |
| 586 | + const usages = parsed.usages; |
| 587 | + |
| 588 | + expect(usages).toContain('valFunc'); |
| 589 | + }); |
| 590 | + |
| 591 | + it('should parseSource correctly', async () => { |
| 592 | + const parsedSource = await parser.parseSource(readFileSync(file).toString()); |
| 593 | + |
| 594 | + expect(parsedSource.usages).toMatchSnapshot(); |
| 595 | + }); |
| 596 | + }); |
| 597 | + |
| 598 | + describe('TSX: Specific cases', () => { |
| 599 | + |
| 600 | + const testFiles = [ |
| 601 | + { |
| 602 | + filename: '1.tsx', |
| 603 | + requiredUsages: [ |
| 604 | + 'sortBy', |
| 605 | + 'Divider', |
| 606 | + 'Checkbox', |
| 607 | + ], |
| 608 | + }, |
| 609 | + { |
| 610 | + filename: '2.tsx', |
| 611 | + requiredUsages: [ |
| 612 | + 'ActionDelete', |
| 613 | + 'Divider', |
| 614 | + 'cloneDeep', |
| 615 | + ], |
| 616 | + }, |
| 617 | + { |
| 618 | + filename: '3.tsx', |
| 619 | + requiredUsages: [ |
| 620 | + 'ImageEdit', |
| 621 | + 'IconButton', |
| 622 | + 'TableHeaderColumn', |
| 623 | + ], |
| 624 | + }, |
| 625 | + ]; |
| 626 | + |
| 627 | + for (const testFile of testFiles) { |
| 628 | + |
| 629 | + it('should parse the correct usages with "parseFile"', async () => { |
| 630 | + const file = getWorkspaceFile(`typescript-parser/specific-cases/${testFile.filename}`); |
| 631 | + const parsed = await parser.parseFile(file, rootPath); |
| 632 | + |
| 633 | + for (const usage of testFile.requiredUsages) { |
| 634 | + expect(parsed.usages).toContain(usage); |
| 635 | + } |
| 636 | + |
| 637 | + expect(parsed.usages).toMatchSnapshot(); |
| 638 | + }); |
| 639 | + |
| 640 | + it('should parse the correct usages with "parseFiles"', async () => { |
| 641 | + const file = getWorkspaceFile(`typescript-parser/specific-cases/${testFile.filename}`); |
| 642 | + const parsed = (await parser.parseFiles([file], rootPath))[0]; |
| 643 | + |
| 644 | + for (const usage of testFile.requiredUsages) { |
| 645 | + expect(parsed.usages).toContain(usage); |
| 646 | + } |
| 647 | + |
| 648 | + expect(parsed.usages).toMatchSnapshot(); |
| 649 | + }); |
| 650 | + |
| 651 | + it('should parse the correct usages with "parseSource"', async () => { |
| 652 | + const file = getWorkspaceFile(`typescript-parser/specific-cases/${testFile.filename}`); |
| 653 | + const fileSource = readFileSync(file).toString(); |
| 654 | + const parsed = await parser.parseSource(fileSource); |
| 655 | + |
| 656 | + for (const usage of testFile.requiredUsages) { |
| 657 | + expect(parsed.usages).toContain(usage); |
| 658 | + } |
| 659 | + |
| 660 | + expect(parsed.usages).toMatchSnapshot(); |
| 661 | + }); |
| 662 | + |
| 663 | + } |
| 664 | + |
| 665 | + }); |
| 666 | + |
| 667 | + describe('JavaScript parsing', () => { |
| 668 | + |
| 669 | + const file = getWorkspaceFile('typescript-parser/javascript.js'); |
| 670 | + |
| 671 | + it('should parse a simple javascript file correctly with "parseFile"', async () => { |
| 672 | + const parsed = await parser.parseFile(file, rootPath); |
| 673 | + delete parsed.filePath; |
| 674 | + delete (parsed as any).rootPath; |
| 675 | + |
| 676 | + expect(parsed).toMatchSnapshot(); |
| 677 | + }); |
| 678 | + |
| 679 | + it('should parse a simple javascript file correctly with "parseFiles"', async () => { |
| 680 | + const parsed = await parser.parseFiles([file], rootPath); |
| 681 | + delete parsed[0].filePath; |
| 682 | + delete (parsed[0] as any).rootPath; |
| 683 | + |
| 684 | + expect(parsed).toMatchSnapshot(); |
| 685 | + }); |
| 686 | + |
| 687 | + it('should parse a simple javascript file correctly with "parseSource"', async () => { |
| 688 | + const content = readFileSync(file).toString(); |
| 689 | + const parsed = await parser.parseSource(content); |
| 690 | + |
| 691 | + expect(parsed).toMatchSnapshot(); |
| 692 | + }); |
| 693 | + |
| 694 | + }); |
| 695 | + |
| 696 | + describe('JSX parsing', () => { |
| 697 | + |
| 698 | + const file = getWorkspaceFile('typescript-parser/jsx.jsx'); |
| 699 | + |
| 700 | + it('should parse a simple javascript react file correctly with "parseFile"', async () => { |
| 701 | + const parsed = await parser.parseFile(file, rootPath); |
| 702 | + delete parsed.filePath; |
| 703 | + delete (parsed as any).rootPath; |
| 704 | + |
| 705 | + expect(parsed).toMatchSnapshot(); |
| 706 | + }); |
| 707 | + |
| 708 | + it('should parse a simple javascript react file correctly with "parseFiles"', async () => { |
| 709 | + const parsed = await parser.parseFiles([file], rootPath); |
| 710 | + delete parsed[0].filePath; |
| 711 | + delete (parsed[0] as any).rootPath; |
| 712 | + |
| 713 | + expect(parsed).toMatchSnapshot(); |
| 714 | + }); |
| 715 | + |
| 716 | + it('should parse a simple javascript react file correctly with "parseSource"', async () => { |
| 717 | + const content = readFileSync(file).toString(); |
| 718 | + const parsed = await parser.parseSource(content); |
| 719 | + |
| 720 | + expect(parsed).toMatchSnapshot(); |
| 721 | + }); |
| 722 | + |
563 | 723 | }); |
564 | 724 |
|
565 | 725 | }); |
0 commit comments