|  | 
| 32 | 32 |       let(:ext) { 'ts' } | 
| 33 | 33 | 
 | 
| 34 | 34 |       include_context 'assert framework structure' | 
|  | 35 | +      include_context 'assert application.js entrypoint renaming' | 
|  | 36 | +    end | 
|  | 37 | +  end | 
|  | 38 | + | 
|  | 39 | +  shared_context 'assert application.js entrypoint renaming' do | 
|  | 40 | +    context 'when application.js exists' do | 
|  | 41 | +      before do | 
|  | 42 | +        FileUtils.mkdir_p(File.join(destination_root, 'app/frontend/entrypoints')) | 
|  | 43 | +        File.write(File.join(destination_root, 'app/frontend/entrypoints/application.js'), '// Application JS') | 
|  | 44 | +      end | 
|  | 45 | + | 
|  | 46 | +      it 'renames application.js to application.ts when TypeScript is enabled' do | 
|  | 47 | +        expect { generator }.not_to raise_error | 
|  | 48 | + | 
|  | 49 | +        if ext == 'ts' | 
|  | 50 | +          expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be true | 
|  | 51 | +          expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.js'))).to be false | 
|  | 52 | +        else | 
|  | 53 | +          expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.js'))).to be true | 
|  | 54 | +          expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be false | 
|  | 55 | +        end | 
|  | 56 | +      end | 
|  | 57 | +    end | 
|  | 58 | + | 
|  | 59 | +    context 'when application.js does not exist' do | 
|  | 60 | +      before do | 
|  | 61 | +        FileUtils.rm_f(File.join(destination_root, 'app/frontend/entrypoints/application.js')) | 
|  | 62 | +      end | 
|  | 63 | + | 
|  | 64 | +      it 'does not attempt to rename when TypeScript is enabled' do | 
|  | 65 | +        expect { generator }.not_to raise_error | 
|  | 66 | + | 
|  | 67 | +        expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be false | 
|  | 68 | +      end | 
| 35 | 69 |     end | 
| 36 | 70 |   end | 
| 37 | 71 | 
 | 
|  | 
| 81 | 115 |       let(:ext) { 'ts' } | 
| 82 | 116 | 
 | 
| 83 | 117 |       include_context 'assert framework structure' | 
|  | 118 | +      include_context 'assert application.js entrypoint renaming' | 
| 84 | 119 | 
 | 
| 85 | 120 |       context 'with old Inertia version' do | 
| 86 | 121 |         let(:inertia_version) { '1.2.0' } | 
|  | 
| 101 | 136 |       let(:ext) { 'ts' } | 
| 102 | 137 | 
 | 
| 103 | 138 |       include_context 'assert framework structure' | 
|  | 139 | +      include_context 'assert application.js entrypoint renaming' | 
| 104 | 140 | 
 | 
| 105 | 141 |       context 'with old Inertia version' do | 
| 106 | 142 |         let(:inertia_version) { '1.2.0' } | 
| @@ -192,7 +228,7 @@ def expect_packages_for(framework, ext: 'js') | 
| 192 | 228 |     end) | 
| 193 | 229 |   end | 
| 194 | 230 | 
 | 
| 195 |  | -  def expect_inertia_prepared_for(framework, ext: 'js') | 
|  | 231 | +  def expect_inertia_prepared_for(framework, ext: 'js', application_js_exists: false) | 
| 196 | 232 |     expect(destination_root).to(have_structure do | 
| 197 | 233 |       case framework | 
| 198 | 234 |       when :react | 
| @@ -230,10 +266,15 @@ def expect_inertia_prepared_for(framework, ext: 'js') | 
| 230 | 266 |         end | 
| 231 | 267 |       end | 
| 232 | 268 |       file('app/views/layouts/application.html.erb') do | 
| 233 |  | -        if ext == 'ts' | 
|  | 269 | +        if ext == 'ts' && application_js_exists | 
|  | 270 | +          contains('<%= vite_typescript_tag "inertia" %>') | 
|  | 271 | +          contains("<%= vite_typescript_tag 'application' %>") | 
|  | 272 | +        elsif ext == 'ts' && !application_js_exists | 
| 234 | 273 |           contains('<%= vite_typescript_tag "inertia" %>') | 
|  | 274 | +          contains("<%= vite_javascript_tag 'application' %>") | 
| 235 | 275 |         else | 
| 236 | 276 |           contains('<%= vite_javascript_tag "inertia" %>') | 
|  | 277 | +          contains("<%= vite_javascript_tag 'application' %>") | 
| 237 | 278 |         end | 
| 238 | 279 |         if framework == :react | 
| 239 | 280 |           contains('<%= vite_react_refresh_tag %>') | 
|  | 
0 commit comments