|
| 1 | +import { Module, TypeScriptRenderer } from '../src'; |
| 2 | + |
| 3 | +test('duplicate selective imports from same source should merge', () => { |
| 4 | + const source = new Module('source'); |
| 5 | + const target = new Module('target'); |
| 6 | + |
| 7 | + source.importSelective(target, ['foo']); |
| 8 | + source.importSelective(target, ['bar']); |
| 9 | + |
| 10 | + const ts = new TypeScriptRenderer(); |
| 11 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 12 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 13 | + import { bar, foo } from "source";" |
| 14 | + `); |
| 15 | +}); |
| 16 | + |
| 17 | +test('selective imports with aliases from same source should merge', () => { |
| 18 | + const source = new Module('source'); |
| 19 | + const target = new Module('target'); |
| 20 | + |
| 21 | + source.importSelective(target, [['foo', 'f']]); |
| 22 | + source.importSelective(target, [['bar', 'b']]); |
| 23 | + |
| 24 | + const ts = new TypeScriptRenderer(); |
| 25 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 26 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 27 | + import { bar as b, foo as f } from "source";" |
| 28 | + `); |
| 29 | +}); |
| 30 | + |
| 31 | +test('mixed regular and aliased selective imports from same source should merge', () => { |
| 32 | + const source = new Module('source'); |
| 33 | + const target = new Module('target'); |
| 34 | + |
| 35 | + source.importSelective(target, ['foo']); |
| 36 | + source.importSelective(target, [['bar', 'b']]); |
| 37 | + source.importSelective(target, ['baz']); |
| 38 | + |
| 39 | + const ts = new TypeScriptRenderer(); |
| 40 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 41 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 42 | + import { bar as b, baz, foo } from "source";" |
| 43 | + `); |
| 44 | +}); |
| 45 | + |
| 46 | +test('imports from different sources should remain separate', () => { |
| 47 | + const source1 = new Module('source1'); |
| 48 | + const source2 = new Module('source2'); |
| 49 | + const target = new Module('target'); |
| 50 | + |
| 51 | + source1.importSelective(target, ['foo']); |
| 52 | + source2.importSelective(target, ['bar']); |
| 53 | + |
| 54 | + const ts = new TypeScriptRenderer(); |
| 55 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 56 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 57 | + import { foo } from "source1"; |
| 58 | + import { bar } from "source2";" |
| 59 | + `); |
| 60 | +}); |
| 61 | + |
| 62 | +test('aliased module imports should not merge with selective imports', () => { |
| 63 | + const source = new Module('source'); |
| 64 | + const target = new Module('target'); |
| 65 | + |
| 66 | + source.import(target, 'S'); |
| 67 | + source.importSelective(target, ['foo']); |
| 68 | + |
| 69 | + const ts = new TypeScriptRenderer(); |
| 70 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 71 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 72 | + import * as S from "source"; |
| 73 | + import { foo } from "source";" |
| 74 | + `); |
| 75 | +}); |
| 76 | + |
| 77 | +test('multiple aliased module imports from same source should remain separate', () => { |
| 78 | + const source = new Module('source'); |
| 79 | + const target = new Module('target'); |
| 80 | + |
| 81 | + source.import(target, 'S1'); |
| 82 | + source.import(target, 'S2'); |
| 83 | + |
| 84 | + const ts = new TypeScriptRenderer(); |
| 85 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 86 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 87 | + import * as S1 from "source"; |
| 88 | + import * as S2 from "source";" |
| 89 | + `); |
| 90 | +}); |
| 91 | + |
| 92 | +test('deduplication with custom fromLocation', () => { |
| 93 | + const source = new Module('source'); |
| 94 | + const target = new Module('target'); |
| 95 | + |
| 96 | + source.importSelective(target, ['foo'], { fromLocation: './custom' }); |
| 97 | + source.importSelective(target, ['bar'], { fromLocation: './custom' }); |
| 98 | + |
| 99 | + const ts = new TypeScriptRenderer(); |
| 100 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 101 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 102 | + import { bar, foo } from "./custom";" |
| 103 | + `); |
| 104 | +}); |
| 105 | + |
| 106 | +test('different fromLocations should not merge', () => { |
| 107 | + const source = new Module('source'); |
| 108 | + const target = new Module('target'); |
| 109 | + |
| 110 | + source.importSelective(target, ['foo'], { fromLocation: './path1' }); |
| 111 | + source.importSelective(target, ['bar'], { fromLocation: './path2' }); |
| 112 | + |
| 113 | + const ts = new TypeScriptRenderer(); |
| 114 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 115 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 116 | + import { foo } from "./path1"; |
| 117 | + import { bar } from "./path2";" |
| 118 | + `); |
| 119 | +}); |
| 120 | + |
| 121 | +test('complex scenario with multiple sources and mixed imports', () => { |
| 122 | + const source1 = new Module('source1'); |
| 123 | + const source2 = new Module('source2'); |
| 124 | + const target = new Module('target'); |
| 125 | + |
| 126 | + source1.importSelective(target, ['a']); |
| 127 | + source2.import(target, 'S2'); |
| 128 | + source1.importSelective(target, [['b', 'B']]); |
| 129 | + source2.importSelective(target, ['c']); |
| 130 | + source1.importSelective(target, ['d']); |
| 131 | + |
| 132 | + const ts = new TypeScriptRenderer(); |
| 133 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 134 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 135 | + import * as S2 from "source2"; |
| 136 | + import { a, b as B, d } from "source1"; |
| 137 | + import { c } from "source2";" |
| 138 | + `); |
| 139 | +}); |
| 140 | + |
| 141 | +test('same import added twice should deduplicate', () => { |
| 142 | + const source = new Module('source'); |
| 143 | + const target = new Module('target'); |
| 144 | + |
| 145 | + source.importSelective(target, ['foo']); |
| 146 | + source.importSelective(target, ['foo']); |
| 147 | + |
| 148 | + const ts = new TypeScriptRenderer(); |
| 149 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 150 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 151 | + import { foo } from "source";" |
| 152 | + `); |
| 153 | +}); |
| 154 | + |
| 155 | +test('same import with different aliases should keep both', () => { |
| 156 | + const source = new Module('source'); |
| 157 | + const target = new Module('target'); |
| 158 | + |
| 159 | + source.importSelective(target, [['foo', 'f1']]); |
| 160 | + source.importSelective(target, [['foo', 'f2']]); |
| 161 | + |
| 162 | + const ts = new TypeScriptRenderer(); |
| 163 | + expect(ts.render(target)).toMatchInlineSnapshot(` |
| 164 | + "/* eslint-disable prettier/prettier, @stylistic/max-len */ |
| 165 | + import { foo as f1, foo as f2 } from "source";" |
| 166 | + `); |
| 167 | +}); |
0 commit comments