diff --git a/spec/dummy/spec/packs_generator_spec.rb b/spec/dummy/spec/packs_generator_spec.rb index 6f75742437..52b1af0eb3 100644 --- a/spec/dummy/spec/packs_generator_spec.rb +++ b/spec/dummy/spec/packs_generator_spec.rb @@ -2,9 +2,10 @@ require_relative "rails_helper" -# rubocop:disable Metrics/ModuleLength +# rubocop:disable Metrics/ModuleLength Metrics/BlockLength module ReactOnRails - # rubocop:disable Metrics/BlockLength + GENERATED_PACKS_OUTPUT_REGEX = /Generated Packs:/.freeze + describe PacksGenerator do let(:webpacker_source_path) { File.expand_path("fixtures/automated_packs_generation", __dir__) } let(:webpacker_source_entry_path) { File.expand_path("fixtures/automated_packs_generation/packs", __dir__) } @@ -250,6 +251,45 @@ module ReactOnRails end end + context "when pack generator is called" do + let(:component_name) { "ComponentWithCommonOnly" } + let(:component_pack) { "#{generated_directory}/#{component_name}.js" } + + before do + stub_webpacker_source_path(component_name: component_name, + webpacker_source_path: webpacker_source_path) + end + + it "does not generate packs if there are no new components or stale files" do + expect { described_class.generate }.to output(GENERATED_PACKS_OUTPUT_REGEX).to_stdout + + expect { described_class.generate }.not_to output(GENERATED_PACKS_OUTPUT_REGEX).to_stdout + end + + it "generate packs if a new component is added" do + expect { described_class.generate }.to output(GENERATED_PACKS_OUTPUT_REGEX).to_stdout + + create_new_component("NewComponent") + + expect { described_class.generate }.to output(GENERATED_PACKS_OUTPUT_REGEX).to_stdout + end + + it "generate packs if an old component is updated" do + expect { described_class.generate }.to output(GENERATED_PACKS_OUTPUT_REGEX).to_stdout + + create_new_component(component_name) + + expect { described_class.generate }.to output(GENERATED_PACKS_OUTPUT_REGEX).to_stdout + end + + def create_new_component(name) + components_subdirectory = ReactOnRails.configuration.components_subdirectory + path = "#{webpacker_source_path}/components/#{component_name}/#{components_subdirectory}/#{name}.jsx" + + File.write(path, "// Empty Test Component\n") + end + end + def generated_server_bundle_file_path "#{webpacker_source_entry_path}/server-bundle-generated.js" end @@ -259,6 +299,5 @@ def stub_webpacker_source_path(webpacker_source_path:, component_name:) .and_return("#{webpacker_source_path}/components/#{component_name}") end end - # rubocop:enable Metrics/BlockLength end -# rubocop:enable Metrics/ModuleLength +# rubocop:enable Metrics/ModuleLength Metrics/BlockLength