|
1 | | -import { CodeConnectReactConfig, getRemoteFileUrl, mapImportPath } from '../project' |
| 1 | +import { |
| 2 | + CodeConnectReactConfig, |
| 3 | + getRemoteFileUrl, |
| 4 | + mapImportPath, |
| 5 | + mapImportSpecifier, |
| 6 | +} from '../project' |
2 | 7 |
|
3 | 8 | describe('Project helper functions', () => { |
4 | 9 | function getConfig(importPaths: {}): CodeConnectReactConfig { |
@@ -72,6 +77,95 @@ describe('Project helper functions', () => { |
72 | 77 | ) |
73 | 78 | expect(mapped).toEqual('@ui/icons') |
74 | 79 | }) |
| 80 | + |
| 81 | + it('Uses filename for index files (use mapImportSpecifier for better results)', () => { |
| 82 | + // Note: mapImportPath uses the resolved file path, so index.ts files return 'index'. |
| 83 | + // For better handling of path aliases, use mapImportSpecifier with the original specifier. |
| 84 | + const mapped = mapImportPath( |
| 85 | + '/Users/test/app/src/AlertTitle/index.ts', |
| 86 | + getConfig({ importPaths: { 'src/*': '@acme/package/*' } }), |
| 87 | + ) |
| 88 | + expect(mapped).toEqual('@acme/package/index') |
| 89 | + }) |
| 90 | + |
| 91 | + it('Uses filename for nested index files', () => { |
| 92 | + const mapped = mapImportPath( |
| 93 | + '/Users/test/app/src/components/Button/index.tsx', |
| 94 | + getConfig({ importPaths: { 'src/*': '@ui/*' } }), |
| 95 | + ) |
| 96 | + expect(mapped).toEqual('@ui/index') |
| 97 | + }) |
| 98 | + |
| 99 | + it('Uses filename when file is not an index file', () => { |
| 100 | + const mapped = mapImportPath( |
| 101 | + '/Users/test/app/src/AlertTitle/AlertTitle.tsx', |
| 102 | + getConfig({ importPaths: { 'src/*': '@acme/package/*' } }), |
| 103 | + ) |
| 104 | + expect(mapped).toEqual('@acme/package/AlertTitle') |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe('importSpecifier mappings', () => { |
| 109 | + it('Transforms path alias with wildcard to package path', () => { |
| 110 | + const mapped = mapImportSpecifier( |
| 111 | + '@/AlertTitle', |
| 112 | + getConfig({ importPaths: { '@/*': '@acme/package/*' } }), |
| 113 | + ) |
| 114 | + expect(mapped).toEqual('@acme/package/AlertTitle') |
| 115 | + }) |
| 116 | + |
| 117 | + it('Transforms nested path alias to package path', () => { |
| 118 | + const mapped = mapImportSpecifier( |
| 119 | + '@/components/Button', |
| 120 | + getConfig({ importPaths: { '@/*': '@ui/*' } }), |
| 121 | + ) |
| 122 | + expect(mapped).toEqual('@ui/components/Button') |
| 123 | + }) |
| 124 | + |
| 125 | + it('Handles exact match without wildcard', () => { |
| 126 | + const mapped = mapImportSpecifier( |
| 127 | + '@/Button', |
| 128 | + getConfig({ importPaths: { '@/Button': '@acme/Button' } }), |
| 129 | + ) |
| 130 | + expect(mapped).toEqual('@acme/Button') |
| 131 | + }) |
| 132 | + |
| 133 | + it('Handles wildcard replacement without output wildcard', () => { |
| 134 | + const mapped = mapImportSpecifier( |
| 135 | + '@/components/Button', |
| 136 | + getConfig({ importPaths: { '@/*': '@ui' } }), |
| 137 | + ) |
| 138 | + expect(mapped).toEqual('@ui') |
| 139 | + }) |
| 140 | + |
| 141 | + it('Returns null for non-matching specifiers', () => { |
| 142 | + const mapped = mapImportSpecifier( |
| 143 | + './Button', |
| 144 | + getConfig({ importPaths: { '@/*': '@acme/package/*' } }), |
| 145 | + ) |
| 146 | + expect(mapped).toBeNull() |
| 147 | + }) |
| 148 | + |
| 149 | + it('Matches first pattern when multiple patterns could match', () => { |
| 150 | + const mapped = mapImportSpecifier( |
| 151 | + '@/icons/icon', |
| 152 | + getConfig({ importPaths: { '@/icons/*': '@ui/icons/*', '@/*': '@ui/*' } }), |
| 153 | + ) |
| 154 | + expect(mapped).toEqual('@ui/icons/icon') |
| 155 | + }) |
| 156 | + |
| 157 | + it('Returns null when no importPaths configured', () => { |
| 158 | + const mapped = mapImportSpecifier('@/Button', getConfig({})) |
| 159 | + expect(mapped).toBeNull() |
| 160 | + }) |
| 161 | + |
| 162 | + it('Handles special regex characters in pattern', () => { |
| 163 | + const mapped = mapImportSpecifier( |
| 164 | + '@scope/package/Button', |
| 165 | + getConfig({ importPaths: { '@scope/package/*': '@acme/*' } }), |
| 166 | + ) |
| 167 | + expect(mapped).toEqual('@acme/Button') |
| 168 | + }) |
75 | 169 | }) |
76 | 170 |
|
77 | 171 | describe('getRemoteFileUrl', () => { |
|
0 commit comments