|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | +require "rails/generators" |
| 5 | +require "generators/alchemy/ingredient/ingredient_generator" |
| 6 | + |
| 7 | +RSpec.describe Alchemy::Generators::IngredientGenerator do |
| 8 | + let(:destination) { Rails.root.join("tmp/generators") } |
| 9 | + |
| 10 | + before do |
| 11 | + FileUtils.rm_rf(destination) |
| 12 | + FileUtils.mkdir_p(destination) |
| 13 | + end |
| 14 | + |
| 15 | + after do |
| 16 | + FileUtils.rm_rf(destination) |
| 17 | + end |
| 18 | + |
| 19 | + def run_generator(args = ["Color"]) |
| 20 | + described_class.start(args, destination_root: destination, quiet: true) |
| 21 | + end |
| 22 | + |
| 23 | + describe "generating a custom ingredient" do |
| 24 | + before { run_generator(["Foo"]) } |
| 25 | + |
| 26 | + it "creates the model" do |
| 27 | + model_path = destination.join("app/models/alchemy/ingredients/foo.rb") |
| 28 | + expect(File.exist?(model_path)).to be true |
| 29 | + content = File.read(model_path) |
| 30 | + expect(content).to include("class Foo < Alchemy::Ingredient") |
| 31 | + end |
| 32 | + |
| 33 | + it "creates the view component" do |
| 34 | + view_path = destination.join("app/components/alchemy/ingredients/foo_view.rb") |
| 35 | + expect(File.exist?(view_path)).to be true |
| 36 | + content = File.read(view_path) |
| 37 | + expect(content).to include("class FooView < BaseView") |
| 38 | + end |
| 39 | + |
| 40 | + it "creates the editor component" do |
| 41 | + editor_path = destination.join("app/components/alchemy/ingredients/foo_editor.rb") |
| 42 | + expect(File.exist?(editor_path)).to be true |
| 43 | + content = File.read(editor_path) |
| 44 | + expect(content).to include("class FooEditor < BaseEditor") |
| 45 | + end |
| 46 | + |
| 47 | + it "does not create an editor partial" do |
| 48 | + partial_path = destination.join("app/views/alchemy/ingredients/_foo_editor.html.erb") |
| 49 | + expect(File.exist?(partial_path)).to be false |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + describe "with underscored class name" do |
| 54 | + before { run_generator(["foo_bar"]) } |
| 55 | + |
| 56 | + it "creates properly named files" do |
| 57 | + expect(File.exist?(destination.join("app/models/alchemy/ingredients/foo_bar.rb"))).to be true |
| 58 | + expect(File.exist?(destination.join("app/components/alchemy/ingredients/foo_bar_view.rb"))).to be true |
| 59 | + expect(File.exist?(destination.join("app/components/alchemy/ingredients/foo_bar_editor.rb"))).to be true |
| 60 | + end |
| 61 | + |
| 62 | + it "uses classified name in class definitions" do |
| 63 | + model_content = File.read(destination.join("app/models/alchemy/ingredients/foo_bar.rb")) |
| 64 | + expect(model_content).to include("class FooBar < Alchemy::Ingredient") |
| 65 | + end |
| 66 | + end |
| 67 | +end |
0 commit comments