Skip to content

Commit 8e7238b

Browse files
Merge pull request #2347 from NCCE/3010-component-generator-fixes
Improving Strapi Component generator
2 parents 9cb7b1e + ddaa99a commit 8e7238b

File tree

4 files changed

+84
-19
lines changed

4 files changed

+84
-19
lines changed

lib/generators/strapi/component_generator.rb

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

5-
argument :component_name, type: :string, required: true
6-
argument :component_type, type: :string, required: true
7-
class_option :strapi_params, type: :array, default: []
5+
desc "Generates a Strapi component and related files"
6+
7+
argument :component_name, type: :string, required: true, desc: "Name of your component, do not include Cms:: namespace, we automatically add this"
8+
argument :component_type, type: :string, required: true, desc: "Name of the type you assigned your component to, e,g blocks"
9+
class_option :strapi_params, type: :array, default: [], desc: "A list of params you want to include from strapi, please provide these matching the case you have used in Strapi"
810

911
COMPONENT_TYPES = %w[blocks content_blocks buttons email_content]
1012
PROVIDER = "providers/strapi/"
1113
BASE_PATH = "app/services/cms/"
12-
STRAPI_PATH = "#{BASE_PATH}#{PROVIDER}"
13-
TEST_PATH = "spec/services/cms/#{PROVIDER}"
14+
STRAPI_PATH = File.join(BASE_PATH, PROVIDER)
15+
TEST_BASE_PATH = "spec/services/cms/"
16+
TEST_PATH = File.join(TEST_BASE_PATH, PROVIDER)
1417

1518
def setup_params
1619
raise StandardError unless COMPONENT_TYPES.include?(component_type)
@@ -24,19 +27,23 @@ def setup_params
2427
end
2528

2629
def create_query_file
27-
template("query_template.rb.tt", "#{STRAPI_PATH}queries/components/#{@component_type_filename}/#{@component_filename}.rb")
30+
template("query_template.rb.tt", File.join(STRAPI_PATH, "queries/components", @component_type_filename, "#{@component_filename}.rb"))
2831
end
2932

3033
def create_query_test
31-
template("query_test_template.rb.tt", "#{TEST_PATH}queries/components/#{@component_type_filename}/#{@component_filename}_spec.rb")
34+
template("query_test_template.rb.tt", File.join(TEST_PATH, "queries/components", @component_type_filename, "#{@component_filename}_spec.rb"))
3235
end
3336

3437
def create_mock_file
35-
template("mock_template.rb.tt", "#{STRAPI_PATH}mocks/dynamic_components/#{@component_type_filename}/#{@component_filename}.rb")
38+
template("mock_template.rb.tt", File.join(STRAPI_PATH, "mocks/dynamic_components", @component_type_filename, "#{@component_filename}.rb"))
3639
end
3740

3841
def create_data_file
39-
template("mapping_template.rb.tt", "#{BASE_PATH}dynamic_components/#{@component_type_filename}/#{@component_filename}.rb")
42+
template("mapping_template.rb.tt", File.join(BASE_PATH, "dynamic_components", @component_type_filename, "#{@component_filename}.rb"))
43+
end
44+
45+
def create_data_text
46+
template("mapping_test_template.rb.tt", File.join(TEST_BASE_PATH, "dynamic_components", @component_type_filename, "#{@component_filename}_spec.rb"))
4047
end
4148

4249
def run_view_component_generator
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Cms::DynamicComponents::<%= @component_type_class %>::<%= @component_name_class %> do
4+
before do
5+
@comp = Cms::Providers::Strapi::Factories::<%= @component_type_class %>Factory.process_component(
6+
Cms::Mocks::DynamicComponents::<%= @component_type_class %>::<%= @component_name_class %>.generate_raw_data
7+
)
8+
end
9+
10+
it "should render as Cms::<%= @component_name_class %>Component" do
11+
expect(@comp.render).to be_a(Cms::<%= @component_name_class %>Component)
12+
end
13+
end

lib/generators/strapi/component_templates/query_template.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Cms
55
module Components
66
module <%= @component_type_class %>
77
class <%= @component_name_class %> < BaseComponentQuery
8-
def self.name = "Component<%= @component_name_class %>"
8+
def self.name = "Component<%= @component_type_class %><%= @component_name_class %>"
99

1010
def self.base_fields
1111
<<~GRAPHQL.freeze

spec/lib/generators/strapi/component_generator_spec.rb

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
RSpec.describe Strapi::ComponentGenerator, type: :generator do
66
let(:tmp_dir) { Rails.root.join("tmp/gen_test") }
7+
let(:query_file_path) { file_path("app/services/cms/providers/strapi/queries/components/blocks/gen_test.rb") }
8+
let(:query_test_path) { file_path("spec/services/cms/providers/strapi/queries/components/blocks/gen_test_spec.rb") }
9+
let(:mapping_file_path) { file_path("app/services/cms/dynamic_components/blocks/gen_test.rb") }
10+
let(:mapping_test_path) { file_path("spec/services/cms/dynamic_components/blocks/gen_test_spec.rb") }
11+
let(:mock_path) { file_path("app/services/cms/providers/strapi/mocks/dynamic_components/blocks/gen_test.rb") }
12+
713
tests Strapi::ComponentGenerator
814
destination Rails.root.join("tmp/gen_test")
915

@@ -21,20 +27,59 @@
2127
run_generator %w[gen_test blocks --strapi-params=title textContent]
2228
end
2329

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"))
30+
context "query file" do
31+
it "is created" do
32+
expect(File).to exist(query_file_path)
33+
end
34+
35+
it "should contain the component name" do
36+
expect(File.read(query_file_path)).to match(/ComponentBlocksGenTest/)
37+
end
38+
39+
it "should include params" do
40+
expect(File.read(query_file_path)).to match(/title/)
41+
expect(File.read(query_file_path)).to match(/textContent/)
42+
end
43+
end
44+
45+
context "query test file" do
46+
it "is created" do
47+
expect(File).to exist(query_test_path)
48+
end
49+
50+
it "should contain the correct example" do
51+
expect(File.read(query_test_path)).to match(/a strapi graphql component/)
52+
end
2653
end
2754

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"))
55+
context "mapping file" do
56+
it "is created" do
57+
expect(File).to exist(mapping_file_path)
58+
end
59+
60+
it "should contain the correct component name" do
61+
expect(File.read(mapping_file_path)).to match(/Cms::GenTestComponent/)
62+
end
3063
end
3164

32-
it "creates data file" do
33-
expect(File).to exist(file_path("app/services/cms/dynamic_components/blocks/gen_test.rb"))
65+
context "mapping test file" do
66+
it "is created" do
67+
expect(File).to exist(mapping_test_path)
68+
end
69+
70+
it "should contain the correct component name" do
71+
expect(File.read(mapping_test_path)).to match(/Cms::GenTestComponent/)
72+
end
3473
end
3574

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"))
75+
context "mock file" do
76+
it "is created" do
77+
expect(File).to exist(mock_path)
78+
end
79+
80+
it "should contain strapi component name" do
81+
expect(File.read(mock_path)).to match(/strapi_component "blocks.gen-test"/)
82+
end
3883
end
3984

4085
it "creates component file" do
@@ -44,7 +89,7 @@
4489
end
4590

4691
def file_path(file)
47-
"tmp/gen_test/#{file}"
92+
File.join(tmp_dir, file)
4893
end
4994
end
5095

0 commit comments

Comments
 (0)