|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | +require "active_support/testing/stream" |
| 5 | +require "generators/alchemy/elements/elements_generator" |
| 6 | + |
| 7 | +RSpec.describe Alchemy::Generators::ElementsGenerator do |
| 8 | + include ActiveSupport::Testing::Stream |
| 9 | + |
| 10 | + around do |example| |
| 11 | + Dir.mktmpdir do |dir| |
| 12 | + @destination = Pathname.new(dir) |
| 13 | + Dir.chdir(@destination) do |
| 14 | + example.run |
| 15 | + end |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + let(:destination) { @destination } |
| 20 | + let(:elements_dir) { destination.join("app/views/alchemy/elements") } |
| 21 | + let(:generator_args) { [] } |
| 22 | + |
| 23 | + subject(:run_generator) do |
| 24 | + capture(:stdout) do |
| 25 | + described_class.start(generator_args, destination_root: destination) |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + describe "#create_partials" do |
| 30 | + before do |
| 31 | + allow(Alchemy::ElementDefinition).to receive(:all).and_return(elements) |
| 32 | + end |
| 33 | + |
| 34 | + context "with elements defined" do |
| 35 | + let(:elements) do |
| 36 | + [ |
| 37 | + Alchemy::ElementDefinition.new( |
| 38 | + name: "article", |
| 39 | + ingredients: [{"role" => "headline", "type" => "Headline"}] |
| 40 | + ), |
| 41 | + Alchemy::ElementDefinition.new( |
| 42 | + name: "sidebar", |
| 43 | + ingredients: [] |
| 44 | + ) |
| 45 | + ] |
| 46 | + end |
| 47 | + |
| 48 | + it "creates a partial for each element" do |
| 49 | + run_generator |
| 50 | + |
| 51 | + expect(elements_dir.join("_article.html.erb")).to exist |
| 52 | + expect(elements_dir.join("_sidebar.html.erb")).to exist |
| 53 | + end |
| 54 | + |
| 55 | + it "generates partials with element_view_for and element name as variable" do |
| 56 | + run_generator |
| 57 | + |
| 58 | + content = elements_dir.join("_article.html.erb").read |
| 59 | + expect(content).to include("element_view_for(article)") |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context "with underscored element names" do |
| 64 | + let(:elements) do |
| 65 | + [Alchemy::ElementDefinition.new(name: "featured_article", ingredients: [])] |
| 66 | + end |
| 67 | + |
| 68 | + it "preserves underscores in the partial filename" do |
| 69 | + run_generator |
| 70 | + |
| 71 | + expect(elements_dir.join("_featured_article.html.erb")).to exist |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context "when ElementDefinition.all returns empty array" do |
| 76 | + let(:elements) { [] } |
| 77 | + |
| 78 | + it "does not create any partials" do |
| 79 | + run_generator |
| 80 | + |
| 81 | + expect(Dir.glob(elements_dir.join("_*.html.erb"))).to be_empty |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context "with slim template engine" do |
| 86 | + let(:elements) { [Alchemy::ElementDefinition.new(name: "article", ingredients: [])] } |
| 87 | + let(:generator_args) { ["-e", "slim"] } |
| 88 | + |
| 89 | + it "creates slim partials" do |
| 90 | + run_generator |
| 91 | + |
| 92 | + expect(elements_dir.join("_article.html.slim")).to exist |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context "with haml template engine" do |
| 97 | + let(:elements) { [Alchemy::ElementDefinition.new(name: "article", ingredients: [])] } |
| 98 | + let(:generator_args) { ["-e", "haml"] } |
| 99 | + |
| 100 | + it "creates haml partials" do |
| 101 | + run_generator |
| 102 | + |
| 103 | + expect(elements_dir.join("_article.html.haml")).to exist |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + context "with nestable elements" do |
| 108 | + let(:elements) do |
| 109 | + [Alchemy::ElementDefinition.new( |
| 110 | + name: "container", |
| 111 | + ingredients: [], |
| 112 | + nestable_elements: ["nested_item"] |
| 113 | + )] |
| 114 | + end |
| 115 | + |
| 116 | + it "includes nested elements rendering in the partial" do |
| 117 | + run_generator |
| 118 | + |
| 119 | + content = elements_dir.join("_container.html.erb").read |
| 120 | + expect(content).to include("nested_elements") |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + context "when partial already exists with different template engine" do |
| 125 | + let(:elements) { [Alchemy::ElementDefinition.new(name: "existing", ingredients: [])] } |
| 126 | + let(:generator_args) { ["--force"] } |
| 127 | + |
| 128 | + before do |
| 129 | + FileUtils.mkdir_p(elements_dir) |
| 130 | + elements_dir.join("_existing.html.slim").write("= element_view_for(existing)") |
| 131 | + end |
| 132 | + |
| 133 | + it "uses the existing template engine instead of the default" do |
| 134 | + expect(run_generator).to include("warning", "slim") |
| 135 | + expect(elements_dir.join("_existing.html.erb")).not_to exist |
| 136 | + expect(elements_dir.join("_existing.html.slim")).to exist |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | +end |
0 commit comments