|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'thor' |
| 4 | +require_relative '../../lib/inertia_rails/generators/helper' |
| 5 | + |
| 6 | +RSpec.describe InertiaRails::Generators::Helper, type: :helper do |
| 7 | + describe '#guess_the_default_framework' do |
| 8 | + let(:package_json_path) { Pathname.new(File.expand_path("spec/fixtures/package_json_files/#{fixture_file_name}", Dir.pwd)) } |
| 9 | + |
| 10 | + shared_examples 'framework detection' do |file_name, expected_framework| |
| 11 | + let(:fixture_file_name) { file_name } |
| 12 | + |
| 13 | + it "returns #{expected_framework.inspect} when inspect \"#{file_name}\"" do |
| 14 | + expect(described_class.guess_the_default_framework(package_json_path)).to eq(expected_framework) |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + it_behaves_like 'framework detection', 'react_package.json', 'react' |
| 19 | + it_behaves_like 'framework detection', 'svelte5_caret_package.json', 'svelte' |
| 20 | + it_behaves_like 'framework detection', 'svelte5_exact_package.json', 'svelte' |
| 21 | + it_behaves_like 'framework detection', 'svelte5_tilde_package.json', 'svelte' |
| 22 | + it_behaves_like 'framework detection', 'svelte4_package.json', 'svelte4' |
| 23 | + it_behaves_like 'framework detection', 'vue_package.json', 'vue' |
| 24 | + |
| 25 | + # Handle exception |
| 26 | + context 'when framework cannot be determined' do |
| 27 | + let(:fixture_file_name) { 'empty_package.json' } |
| 28 | + |
| 29 | + it 'raises an error' do |
| 30 | + allow(described_class).to receive(:exit) # Prevent `exit` from terminating the test |
| 31 | + expect(Thor::Shell::Basic).to receive_message_chain(:new, :say_error) |
| 32 | + .with('Could not determine the Inertia.js framework you are using.') |
| 33 | + described_class.guess_the_default_framework(package_json_path) |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | +end |
0 commit comments