|
7 | 7 | generateParamParserOptions, |
8 | 8 | generatePathParamsOptions, |
9 | 9 | generateCustomParamParsersList, |
| 10 | + generateNormalizedParamParsersDeclarations, |
10 | 11 | type ParamParsersMap, |
11 | 12 | } from './generateParamParsers' |
12 | 13 | import { PrefixTree } from '../core/tree' |
@@ -385,10 +386,9 @@ describe('generateParamParserOptions', () => { |
385 | 386 | ]) |
386 | 387 |
|
387 | 388 | const result = generateParamParserOptions(param, importsMap, paramParsers) |
388 | | - expect(result).toBe('_normalizeParamParser(PARAM_PARSER__uuid)') |
389 | | - expect(importsMap.toString()).toContain( |
390 | | - `import { parser as PARAM_PARSER__uuid } from '/path/to/parsers/uuid'` |
391 | | - ) |
| 389 | + expect(result).toBe('_normalized_PARAM_PARSER__uuid') |
| 390 | + // imports are no longer added by generateParamParserOptions for custom parsers |
| 391 | + // they are handled by generateNormalizedParamParsersDeclarations |
392 | 392 | }) |
393 | 393 |
|
394 | 394 | it('generates correct import for native int parser', () => { |
@@ -562,7 +562,7 @@ describe('generatePathParamsOptions', () => { |
562 | 562 | ]) |
563 | 563 |
|
564 | 564 | const result = generatePathParamsOptions(params, importsMap, paramParsers) |
565 | | - expect(result).toContain('id: [_normalizeParamParser(PARAM_PARSER__uuid)]') |
| 565 | + expect(result).toContain('id: [_normalized_PARAM_PARSER__uuid]') |
566 | 566 | expect(result).toContain( |
567 | 567 | 'page: [PARAM_PARSER_INT, /* repeatable: false */, /* optional: */ true]' |
568 | 568 | ) |
@@ -676,16 +676,112 @@ describe('generateParamParserCustomType', () => { |
676 | 676 | parser: 'user-id', // Route uses original kebab-case name |
677 | 677 | } |
678 | 678 | expect(generateParamParserOptions(param, importsMap, paramParsers)).toBe( |
679 | | - '_normalizeParamParser(PARAM_PARSER__userId)' |
| 679 | + '_normalized_PARAM_PARSER__userId' |
680 | 680 | ) // Generated variable is camelCase |
681 | | - |
682 | | - expect(importsMap.toString()).toContain( |
683 | | - `import { parser as PARAM_PARSER__userId } from '/path/to/parsers/user-id'` |
684 | | - ) |
| 681 | + // imports are no longer added by generateParamParserOptions for custom parsers |
685 | 682 |
|
686 | 683 | expect(generateCustomParamParsersList(paramParsers)).toEqual([ |
687 | 684 | "'date-with-dashes'", |
688 | 685 | "'user-id'", |
689 | 686 | ]) |
690 | 687 | }) |
691 | 688 | }) |
| 689 | + |
| 690 | +describe('generateNormalizedParamParsersDeclarations', () => { |
| 691 | + it('returns empty string for empty param parsers map', () => { |
| 692 | + const paramParsers: ParamParsersMap = new Map() |
| 693 | + const importsMap = new ImportsMap() |
| 694 | + const result = generateNormalizedParamParsersDeclarations( |
| 695 | + paramParsers, |
| 696 | + importsMap |
| 697 | + ) |
| 698 | + expect(result).toBe('') |
| 699 | + }) |
| 700 | + |
| 701 | + it('generates a const declaration for a single parser', () => { |
| 702 | + const paramParsers: ParamParsersMap = new Map([ |
| 703 | + [ |
| 704 | + 'uuid', |
| 705 | + { |
| 706 | + name: 'uuid', |
| 707 | + typeName: 'Param_uuid', |
| 708 | + relativePath: 'parsers/uuid', |
| 709 | + absolutePath: '/path/to/parsers/uuid', |
| 710 | + }, |
| 711 | + ], |
| 712 | + ]) |
| 713 | + const importsMap = new ImportsMap() |
| 714 | + const result = generateNormalizedParamParsersDeclarations( |
| 715 | + paramParsers, |
| 716 | + importsMap |
| 717 | + ) |
| 718 | + expect(result).toBe( |
| 719 | + 'const _normalized_PARAM_PARSER__uuid = _normalizeParamParser(PARAM_PARSER__uuid)' |
| 720 | + ) |
| 721 | + expect(importsMap.toString()).toContain( |
| 722 | + `import { _normalizeParamParser } from 'vue-router/experimental'` |
| 723 | + ) |
| 724 | + expect(importsMap.toString()).toContain( |
| 725 | + `import { parser as PARAM_PARSER__uuid } from '/path/to/parsers/uuid'` |
| 726 | + ) |
| 727 | + }) |
| 728 | + |
| 729 | + it('generates declarations for multiple parsers', () => { |
| 730 | + const paramParsers: ParamParsersMap = new Map([ |
| 731 | + [ |
| 732 | + 'uuid', |
| 733 | + { |
| 734 | + name: 'uuid', |
| 735 | + typeName: 'Param_uuid', |
| 736 | + relativePath: 'parsers/uuid', |
| 737 | + absolutePath: '/path/to/parsers/uuid', |
| 738 | + }, |
| 739 | + ], |
| 740 | + [ |
| 741 | + 'slug', |
| 742 | + { |
| 743 | + name: 'slug', |
| 744 | + typeName: 'Param_slug', |
| 745 | + relativePath: 'parsers/slug', |
| 746 | + absolutePath: '/path/to/parsers/slug', |
| 747 | + }, |
| 748 | + ], |
| 749 | + ]) |
| 750 | + const importsMap = new ImportsMap() |
| 751 | + const result = generateNormalizedParamParsersDeclarations( |
| 752 | + paramParsers, |
| 753 | + importsMap |
| 754 | + ) |
| 755 | + expect(result).toContain( |
| 756 | + 'const _normalized_PARAM_PARSER__uuid = _normalizeParamParser(PARAM_PARSER__uuid)' |
| 757 | + ) |
| 758 | + expect(result).toContain( |
| 759 | + 'const _normalized_PARAM_PARSER__slug = _normalizeParamParser(PARAM_PARSER__slug)' |
| 760 | + ) |
| 761 | + }) |
| 762 | + |
| 763 | + it('handles camelCase names from kebab-case filenames', () => { |
| 764 | + const paramParsers: ParamParsersMap = new Map([ |
| 765 | + [ |
| 766 | + 'user-id', |
| 767 | + { |
| 768 | + name: 'userId', |
| 769 | + typeName: 'Param_userId', |
| 770 | + relativePath: 'parsers/user-id', |
| 771 | + absolutePath: '/path/to/parsers/user-id', |
| 772 | + }, |
| 773 | + ], |
| 774 | + ]) |
| 775 | + const importsMap = new ImportsMap() |
| 776 | + const result = generateNormalizedParamParsersDeclarations( |
| 777 | + paramParsers, |
| 778 | + importsMap |
| 779 | + ) |
| 780 | + expect(result).toBe( |
| 781 | + 'const _normalized_PARAM_PARSER__userId = _normalizeParamParser(PARAM_PARSER__userId)' |
| 782 | + ) |
| 783 | + expect(importsMap.toString()).toContain( |
| 784 | + `import { parser as PARAM_PARSER__userId } from '/path/to/parsers/user-id'` |
| 785 | + ) |
| 786 | + }) |
| 787 | +}) |
0 commit comments