Skip to content

Commit 7210632

Browse files
committed
Adding the generator_spec gem to help with generator spec testing
Adding tests for the generator
1 parent c2af4de commit 7210632

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ group :test do
105105
gem "axe-matchers", ">= 2.5.0", require: false
106106
gem "capybara"
107107
gem "climate_control"
108+
gem "generator_spec"
108109
gem "guard-rspec", require: false
109110
gem "rails-controller-testing"
110111
gem "rspec-json_expectations"

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ GEM
276276
fog-core
277277
nokogiri (>= 1.5.11, < 2.0.0)
278278
formatador (1.1.0)
279+
generator_spec (0.10.0)
280+
activesupport (>= 3.0.0)
281+
railties (>= 3.0.0)
279282
geocoder (1.8.2)
280283
globalid (1.2.1)
281284
activesupport (>= 6.1)
@@ -777,6 +780,7 @@ DEPENDENCIES
777780
faker
778781
faraday
779782
fog-aws
783+
generator_spec
780784
geocoder (>= 1.6.6)
781785
graphlient
782786
graphql

lib/generators/strapi/component_generator.rb

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ module Strapi
22
class ComponentGenerator < Rails::Generators::Base
33
source_root File.expand_path("component_templates", __dir__)
44

5-
class_option :component_name, type: :string
6-
class_option :component_type, type: :string
5+
argument :component_name, type: :string, required: true
6+
argument :component_type, type: :string, required: true
77
class_option :strapi_params, type: :array, default: []
88

99
COMPONENT_TYPES = %w[blocks content_blocks buttons email_content]
1010
PROVIDER = "providers/strapi/"
1111
BASE_PATH = "app/services/cms/"
1212
STRAPI_PATH = "#{BASE_PATH}#{PROVIDER}"
13-
TEST_PATH = "app/sevices/spec/#{PROVIDER}"
13+
TEST_PATH = "spec/services/cms/#{PROVIDER}"
1414

1515
def setup_params
16-
raise StandardError unless COMPONENT_TYPES.include?(options["component_type"])
17-
@component_name_class = options["component_name"].classify
18-
@component_type_class = options["component_type"].camelize
19-
@component_filename = options["component_name"].underscore
20-
@component_type_filename = options["component_type"].underscore
21-
@component_strapi_name = options["component_name"].underscore.tr("_", "-")
16+
raise StandardError unless COMPONENT_TYPES.include?(component_type)
17+
@component_name_class = component_name.classify
18+
@component_type_class = component_type.camelize
19+
@component_filename = component_name.underscore
20+
@component_type_filename = component_type.underscore
21+
@component_strapi_name = component_name.underscore.tr("_", "-")
2222
@strapi_params = options["strapi_params"]
2323
@rails_param_names = options["strapi_params"].map { _1.underscore }
2424
end
@@ -39,8 +39,23 @@ def create_data_file
3939
template("mapping_template.rb.tt", "#{BASE_PATH}dynamic_components/#{@component_type_filename}/#{@component_filename}.rb")
4040
end
4141

42-
def run_other_generators
43-
generate "component Cms::#{@component_name_class} #{@rails_param_names.join(" ")} --test-framework=rspec"
42+
def run_view_component_generator
43+
params = @rails_param_names.join(" ").presence
44+
Rails::Generators.invoke(
45+
"component",
46+
["Cms::#{@component_name_class}", params, "--test-framework=rspec", "--sidecar"].compact,
47+
behaviour: :invoke,
48+
destination_root:
49+
)
50+
rescue
51+
puts <<~HEREDOC
52+
#{"*" * 80}
53+
Unable to create component, please run this command seperatly
54+
55+
rails generate component Cms::#{@component_name_class} #{@rails_param_names.join(" ")} --test-framework=rspec
56+
#{"*" * 80}
57+
58+
HEREDOC
4459
end
4560

4661
def print_method_defintions
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require "rails_helper"
2+
require "generator_spec"
3+
require "generators/strapi/component_generator"
4+
5+
RSpec.describe Strapi::ComponentGenerator, type: :generator do
6+
let(:tmp_dir) { Rails.root.join("tmp/gen_test") }
7+
tests Strapi::ComponentGenerator
8+
destination Rails.root.join("tmp/gen_test")
9+
10+
before do
11+
FileUtils.rm_rf(tmp_dir)
12+
prepare_destination
13+
end
14+
15+
after do
16+
FileUtils.rm_rf(tmp_dir)
17+
end
18+
19+
context "with all arguments" do
20+
before do
21+
run_generator %w[gen_test blocks --strapi-params=title textContent]
22+
end
23+
24+
it "creates the query file" do
25+
expect(File).to exist(file_path("app/services/cms/providers/strapi/queries/components/blocks/gen_test.rb"))
26+
end
27+
28+
it "creates the query test file" do
29+
expect(File).to exist(file_path("spec/services/cms/providers/strapi/queries/components/blocks/gen_test_spec.rb"))
30+
end
31+
32+
it "creates data file" do
33+
expect(File).to exist(file_path("app/services/cms/dynamic_components/blocks/gen_test.rb"))
34+
end
35+
36+
it "creates mock file" do
37+
expect(File).to exist(file_path("app/services/cms/providers/strapi/mocks/dynamic_components/blocks/gen_test.rb"))
38+
end
39+
40+
it "creates component file" do
41+
expect(File).to exist(file_path("app/components/cms/gen_test_component.rb"))
42+
expect(File).to exist(file_path("app/components/cms/gen_test_component/gen_test_component.html.erb"))
43+
expect(File).to exist(file_path("spec/components/cms/gen_test_component_spec.rb"))
44+
end
45+
46+
def file_path(file)
47+
"tmp/gen_test/#{file}"
48+
end
49+
end
50+
51+
context "with invalid type" do
52+
it "creates the query file" do
53+
expect {
54+
run_generator %w[gen_test not-a-type --strapi-params=title textContent]
55+
}.to raise_error
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)