|
| 1 | +<?php |
| 2 | + |
| 3 | +use Spatie\TypeScriptTransformer\Actions\ResolveModuleImportsAction; |
| 4 | +use Spatie\TypeScriptTransformer\Support\ImportLocation; |
| 5 | +use Spatie\TypeScriptTransformer\Support\ImportName; |
| 6 | +use Spatie\TypeScriptTransformer\Support\Location; |
| 7 | +use Spatie\TypeScriptTransformer\Tests\Factories\TransformedFactory; |
| 8 | +use Spatie\TypeScriptTransformer\TypeScript\TypeReference; |
| 9 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptImport; |
| 10 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptString; |
| 11 | + |
| 12 | +beforeEach(function () { |
| 13 | + $this->action = new ResolveModuleImportsAction(); |
| 14 | +}); |
| 15 | + |
| 16 | +it('wont resolve imports when types are in the same module', function () { |
| 17 | + $location = new Location([], [ |
| 18 | + $reference = TransformedFactory::alias('A', new TypeScriptString())->build(), |
| 19 | + TransformedFactory::alias('B', new TypeReference($reference->reference), references: [ |
| 20 | + $reference, |
| 21 | + ])->build(), |
| 22 | + ]); |
| 23 | + |
| 24 | + expect($this->action->execute($location)->isEmpty())->toBe(true); |
| 25 | +}); |
| 26 | + |
| 27 | +it('will import a type from another module', function () { |
| 28 | + $nestedReference = TransformedFactory::alias('Nested', new TypeScriptString(), location: ['parent', 'level', 'nested'])->build(); |
| 29 | + $parentReference = TransformedFactory::alias('Parent', new TypeScriptString(), location: ['parent'])->build(); |
| 30 | + $deeperParent = TransformedFactory::alias('DeeperParent', new TypeScriptString(), location: ['parent', 'deeper'])->build(); |
| 31 | + $rootReference = TransformedFactory::alias('Root', new TypeScriptString(), location: [])->build(); |
| 32 | + |
| 33 | + $location = new Location(['parent', 'level'], [ |
| 34 | + TransformedFactory::alias('Type', new TypeScriptString(), references: [ |
| 35 | + $nestedReference, |
| 36 | + $parentReference, |
| 37 | + $deeperParent, |
| 38 | + $rootReference, |
| 39 | + ])->build(), |
| 40 | + ]); |
| 41 | + |
| 42 | + $imports = $this->action->execute($location); |
| 43 | + |
| 44 | + expect($imports->toArray()) |
| 45 | + ->toHaveCount(4) |
| 46 | + ->each->toBeInstanceOf(ImportLocation::class); |
| 47 | + |
| 48 | + expect($imports->getTypeScriptNodes())->toEqual([ |
| 49 | + new TypeScriptImport('nested', [new ImportName('Nested', $nestedReference->reference)]), |
| 50 | + new TypeScriptImport('../', [new ImportName('Parent', $parentReference->reference)]), |
| 51 | + new TypeScriptImport('../deeper', [new ImportName('DeeperParent', $deeperParent->reference)]), |
| 52 | + new TypeScriptImport('../../', [new ImportName('Root', $rootReference->reference)]), |
| 53 | + ]); |
| 54 | +}); |
| 55 | + |
| 56 | +it('wont import the same type twice', function () { |
| 57 | + $nestedReference = TransformedFactory::alias('Nested', new TypeScriptString(), location: ['nested'])->build(); |
| 58 | + |
| 59 | + $location = new Location([], [ |
| 60 | + TransformedFactory::alias('TypeA', new TypeScriptString(), references: [ |
| 61 | + $nestedReference, |
| 62 | + ])->build(), |
| 63 | + TransformedFactory::alias('TypeB', new TypeScriptString(), references: [ |
| 64 | + $nestedReference, |
| 65 | + ])->build(), |
| 66 | + ]); |
| 67 | + |
| 68 | + $imports = $this->action->execute($location); |
| 69 | + |
| 70 | + expect($imports->toArray()) |
| 71 | + ->toHaveCount(1) |
| 72 | + ->each->toBeInstanceOf(ImportLocation::class); |
| 73 | + |
| 74 | + expect($imports->getTypeScriptNodes())->toEqual([ |
| 75 | + new TypeScriptImport('nested', [new ImportName('Nested', $nestedReference->reference)]), |
| 76 | + ]); |
| 77 | +}); |
| 78 | + |
| 79 | +it('will alias a reference if it is already in the module', function (){ |
| 80 | + $nestedCollection = TransformedFactory::alias('Collection', new TypeScriptString(), location: ['nested'])->build(); |
| 81 | + |
| 82 | + $location = new Location([], [ |
| 83 | + TransformedFactory::alias('Collection', new TypeScriptString(), references: [ |
| 84 | + $nestedCollection, |
| 85 | + ])->build(), |
| 86 | + ]); |
| 87 | + |
| 88 | + $imports = $this->action->execute($location); |
| 89 | + |
| 90 | + expect($imports->toArray()) |
| 91 | + ->toHaveCount(1) |
| 92 | + ->each->toBeInstanceOf(ImportLocation::class); |
| 93 | + |
| 94 | + expect($imports->getTypeScriptNodes())->toEqual([ |
| 95 | + new TypeScriptImport('nested', [new ImportName('Collection', $nestedCollection->reference, 'CollectionImport')]), |
| 96 | + ]); |
| 97 | +}); |
| 98 | + |
| 99 | +it('will alias a reference if it is already in the module and already aliased by another import', function (){ |
| 100 | + $nestedCollection = TransformedFactory::alias('Collection', new TypeScriptString(), location: ['nested'])->build(); |
| 101 | + $otherNestedCollection = TransformedFactory::alias('Collection', new TypeScriptString(), location: ['otherNested'])->build(); |
| 102 | + |
| 103 | + $location = new Location([], [ |
| 104 | + TransformedFactory::alias('Collection', new TypeScriptString(), references: [ |
| 105 | + $nestedCollection, |
| 106 | + $otherNestedCollection, |
| 107 | + ])->build(), |
| 108 | + ]); |
| 109 | + |
| 110 | + $imports = $this->action->execute($location); |
| 111 | + |
| 112 | + expect($imports->toArray()) |
| 113 | + ->toHaveCount(2) |
| 114 | + ->each->toBeInstanceOf(ImportLocation::class); |
| 115 | + |
| 116 | + expect($imports->getTypeScriptNodes())->toEqual([ |
| 117 | + new TypeScriptImport('nested', [new ImportName('Collection', $nestedCollection->reference, 'CollectionImport')]), |
| 118 | + new TypeScriptImport('otherNested', [new ImportName('Collection', $otherNestedCollection->reference, 'CollectionImport2')]), |
| 119 | + ]); |
| 120 | +}); |
0 commit comments