Skip to content

Commit

Permalink
Add Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkitkkr committed Jan 27, 2023
1 parent 39d2757 commit ddf4b6b
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions spec/dummy/spec/packs_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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__) }
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit ddf4b6b

Please sign in to comment.