Skip to content

Commit

Permalink
test: improve consistency of example names and general layout (#355)
Browse files Browse the repository at this point in the history
* test: improve consistency of example names and general layout

* test: use `Dir.chdir` with a block instead of `ensure`

* test: remove unused parameter
  • Loading branch information
G-Rath authored Sep 4, 2023
1 parent b6b7ff6 commit 54d3bcc
Show file tree
Hide file tree
Showing 27 changed files with 383 additions and 345 deletions.
8 changes: 5 additions & 3 deletions spec/backward_compatibility_specs/compiler_strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

describe "Webpacker::CompilerStrategy" do
describe "#from_config" do
it "returns and instance of MtimeStrategy when compiler_strategy is set to mtime" do
it "returns an instance of MtimeStrategy when compiler_strategy is set to mtime" do
allow(Webpacker.config).to receive(:compiler_strategy).and_return("mtime")

expect(Webpacker::CompilerStrategy.from_config).to be_an_instance_of(Webpacker::MtimeStrategy)
end

it "returns and instance of DigestStrategy when compiler_strategy is set to digest" do
it "returns an instance of DigestStrategy when compiler_strategy is set to digest" do
allow(Webpacker.config).to receive(:compiler_strategy).and_return("digest")

expect(Webpacker::CompilerStrategy.from_config).to be_an_instance_of(Webpacker::DigestStrategy)
end

it "raise exception for unknown compiler_strategy in the config file" do
it "raise an exception for unknown compiler_strategy in the config file" do
expected_error_message = "Unknown strategy 'other'. Available options are 'mtime' and 'digest'."
allow(Webpacker.config).to receive(:compiler_strategy).and_return("other")

Expand Down
16 changes: 12 additions & 4 deletions spec/backward_compatibility_specs/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,43 @@

it "#source_path returns correct path" do
source_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/app/packs").to_s

expect(config.source_path.to_s).to eq source_path
end

it "#source_entry_path returns correct path" do
source_entry_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/app/packs", "entrypoints").to_s

expect(config.source_entry_path.to_s).to eq source_entry_path
end

it "#public_root_path returns correct path" do
public_root_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/public").to_s

expect(config.public_path.to_s).to eq public_root_path
end

it "#public_output_path returns correct path" do
public_output_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/public/packs").to_s

expect(config.public_output_path.to_s).to eq public_output_path
end

it "#public_manifest_path returns correct path" do
public_manifest_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/public/packs", "manifest.json").to_s

expect(config.public_manifest_path.to_s).to eq public_manifest_path
end

it "#manifest_path returns correct path" do
manifest_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/public/packs", "manifest.json").to_s

expect(config.manifest_path.to_s).to eq manifest_path
end

it "#cache_path returns correct path" do
cache_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/tmp/webpacker").to_s

expect(config.cache_path.to_s).to eq cache_path
end

Expand Down Expand Up @@ -186,6 +193,7 @@

it "#public_output_path returns correct path" do
expected_public_output_path = File.expand_path File.join(File.dirname(__FILE__), "public/packs").to_s

expect(config.public_output_path.to_s).to eq expected_public_output_path
end
end
Expand All @@ -199,15 +207,16 @@

it "#manifest_path returns correct expected value" do
expected_manifest_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/app/packs", "manifest.json").to_s

expect(config.manifest_path.to_s).to eq expected_manifest_path
end
end

context "with webpacker_precompile entry set to false" do
describe "#webpacker_precompile?" do
before :each do
ENV.delete("WEBPACKER_PRECOMPILE")
ENV.delete("SHAKAPACKER_PRECOMPILE")
ENV.delete("WEBPACKER_PRECOMPILE")
end

let(:config) {
Expand All @@ -229,9 +238,8 @@
expect(subject).to be true
end

it "returns false with WEBPACKER_PRECOMPILE set to falsy value" do
# ENV["WEBPACKER_PRECOMPILE"] = "no"
ENV.delete("WEBPACKER_PRECOMPILE")
it "returns false with WEBPACKER_PRECOMPILE set to nil" do
ENV["SHAKAPACKER_PRECOMPILE"] = nil
expect(subject).to be false
end
end
Expand Down
33 changes: 17 additions & 16 deletions spec/backward_compatibility_specs/dev_server_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@

let(:test_app_path) { File.expand_path("webpacker_test_app", __dir__) }

it "run cmd via node modules" do
it "supports running via node modules" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]

verify_command(cmd, use_node_modules: true)
end

it "run cmd via yarn" do
it "supports running via yarn" do
cmd = ["yarn", "webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]

verify_command(cmd, use_node_modules: false)
end

it "run cmd argv" do
it "passes on arguments" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--quiet"]

verify_command(cmd, argv: (["--quiet"]))
end

it "run cmd argv with https" do
it "supports the https flag" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"]

dev_server = double()
Expand All @@ -54,26 +57,24 @@
# ENV["WEBPACKER_CONFIG"] is the interface and env["SHAKAPACKER_CONFIG"] is internal
ENV["WEBPACKER_CONFIG"] = env["SHAKAPACKER_CONFIG"] = "#{test_app_path}/config/webpacker_other_location.yml"
env["WEBPACK_SERVE"] = "true"

verify_command(cmd, env: env)
end

private

def verify_command(cmd, use_node_modules: true, argv: [], env: Webpacker::Compiler.env)
cwd = Dir.pwd
Dir.chdir(test_app_path)
klass = Webpacker::DevServerRunner
instance = klass.new(argv)

allow(klass).to receive(:new).and_return(instance)
allow(instance).to receive(:node_modules_bin_exist?).and_return(use_node_modules)
allow(Kernel).to receive(:exec).with(env, *cmd)
Dir.chdir(test_app_path) do
klass = Webpacker::DevServerRunner
instance = klass.new(argv)

klass.run(argv)
allow(klass).to receive(:new).and_return(instance)
allow(instance).to receive(:node_modules_bin_exist?).and_return(use_node_modules)
allow(Kernel).to receive(:exec).with(env, *cmd)

expect(Kernel).to have_received(:exec).with(env, *cmd)
klass.run(argv)

ensure
Dir.chdir(cwd)
expect(Kernel).to have_received(:exec).with(env, *cmd)
end
end
end
2 changes: 1 addition & 1 deletion spec/backward_compatibility_specs/dev_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
end
end

it "users SHAKAPACKER_DEV_SERVER for DEFAULT_ENV_PREFIX" do
it "uses SHAKAPACKER_DEV_SERVER for DEFAULT_ENV_PREFIX" do
expect(Webpacker::DevServer::DEFAULT_ENV_PREFIX).to eq "SHAKAPACKER_DEV_SERVER"
end
end
2 changes: 2 additions & 0 deletions spec/backward_compatibility_specs/digest_strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ def remove_compilation_digest_path

it "is fresh after compilation" do
@digest_strategy.after_compile_hook

expect(@digest_strategy.stale?).to be false
expect(@digest_strategy.fresh?).to be true
end

it "generates correct compilation_digest_path" do
actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
expected_path = "last-compilation-digest-#{Webpacker.env}"

expect(actual_path).to eq expected_path
end
end
3 changes: 2 additions & 1 deletion spec/backward_compatibility_specs/engine_rake_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

it "mounts app:webpacker task successfully" do
output = Dir.chdir(mounted_app_path) { `rake -T` }

expect(output).to match /app:webpacker.+DEPRECATED/
expect(output).to match /app:webpacker:binstubs.+DEPRECATED/
end

it "binstubs adds only expected files to bin directory" do
it "only adds expected files to bin directory when binstubs is run" do
Dir.chdir(mounted_app_path) { `bundle exec rake app:webpacker:binstubs` }
expected_binstub_paths.each { |path| expect(File.exist?(path)).to be true }
end
Expand Down
40 changes: 20 additions & 20 deletions spec/backward_compatibility_specs/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,33 @@ def base_url
@javascript_pack_tag_loaded = nil
end

it "#asset_pack_path generates correct path" do
it "#asset_pack_path generates the correct path" do
expect(asset_pack_path("bootstrap.js")).to eq "/packs/bootstrap-300631c4f0e0f9c865bc.js"
expect(asset_pack_path("bootstrap.css")).to eq "/packs/bootstrap-c38deda30895059837cf.css"
end

it "#asset_pack_url generates correct url" do
it "#asset_pack_url generates the correct url" do
expect(asset_pack_url("bootstrap.js")).to eq "https://example.com/packs/bootstrap-300631c4f0e0f9c865bc.js"
expect(asset_pack_url("bootstrap.css")).to eq "https://example.com/packs/bootstrap-c38deda30895059837cf.css"
end

it "#image_pack_path generates correct path" do
it "#image_pack_path generates the correct path" do
expect(image_pack_path("application.png")).to eq "/packs/application-k344a6d59eef8632c9d1.png"
expect(image_pack_path("image.jpg")).to eq "/packs/static/image-c38deda30895059837cf.jpg"
expect(image_pack_path("static/image.jpg")).to eq "/packs/static/image-c38deda30895059837cf.jpg"
expect(image_pack_path("nested/image.jpg")).to eq "/packs/static/nested/image-c38deda30895059837cf.jpg"
expect(image_pack_path("static/nested/image.jpg")).to eq "/packs/static/nested/image-c38deda30895059837cf.jpg"
end

it "#image_pack_url generates correct path" do
it "#image_pack_url generates the correct path" do
expect(image_pack_url("application.png")).to eq "https://example.com/packs/application-k344a6d59eef8632c9d1.png"
expect(image_pack_url("image.jpg")).to eq "https://example.com/packs/static/image-c38deda30895059837cf.jpg"
expect(image_pack_url("static/image.jpg")).to eq "https://example.com/packs/static/image-c38deda30895059837cf.jpg"
expect(image_pack_url("nested/image.jpg")).to eq "https://example.com/packs/static/nested/image-c38deda30895059837cf.jpg"
expect(image_pack_url("static/nested/image.jpg")).to eq "https://example.com/packs/static/nested/image-c38deda30895059837cf.jpg"
end

it "#image_pack_tag generates correct tags" do
it "#image_pack_tag generates the correct tags" do
expect(image_pack_tag("application.png", size: "16x10", alt: "Edit Entry")).to eq "<img alt=\"Edit Entry\" src=\"/packs/application-k344a6d59eef8632c9d1.png\" width=\"16\" height=\"10\" />"
expect(image_pack_tag("image.jpg", size: "16x10", alt: "Edit Entry")).to eq "<img alt=\"Edit Entry\" src=\"/packs/static/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />"
expect(image_pack_tag("static/image.jpg", size: "16x10", alt: "Edit Entry")).to eq "<img alt=\"Edit Entry\" src=\"/packs/static/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />"
Expand All @@ -57,23 +57,23 @@ def base_url
expect(image_pack_tag("static/image.jpg", srcset: { "static/image-2x.jpg" => "2x" })).to eq "<img srcset=\"/packs/static/image-2x-7cca48e6cae66ec07b8e.jpg 2x\" src=\"/packs/static/image-c38deda30895059837cf.jpg\" />"
end

it "#favicon_pack_tag generates correct tags" do
it "#favicon_pack_tag generates the correct tags" do
expect(favicon_pack_tag("application.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/application-k344a6d59eef8632c9d1.png\" />"
expect(favicon_pack_tag("mb-icon.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/mb-icon-c38deda30895059837cf.png\" />"
expect(favicon_pack_tag("static/mb-icon.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/mb-icon-c38deda30895059837cf.png\" />"
expect(favicon_pack_tag("nested/mb-icon.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/nested/mb-icon-c38deda30895059837cf.png\" />"
expect(favicon_pack_tag("static/nested/mb-icon.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/nested/mb-icon-c38deda30895059837cf.png\" />"
end

it "#preload_pack_asset generates correct tag" do
it "#preload_pack_asset generates the correct tag" do
if self.class.method_defined?(:preload_link_tag)
expect(preload_pack_asset("fonts/fa-regular-400.woff2")).to eq %(<link rel="preload" href="/packs/fonts/fa-regular-400-944fb546bd7018b07190a32244f67dc9.woff2" as="font" type="font/woff2" crossorigin="anonymous">)
else
expect { preload_pack_asset("fonts/fa-regular-400.woff2") }.to raise_error "You need Rails >= 5.2 to use this tag."
end
end

it "#javascript_pack_tag generates correct tags" do
it "#javascript_pack_tag generates the correct tags" do
expected = <<~HTML.chomp
<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>
<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>
Expand All @@ -84,7 +84,7 @@ def base_url
expect(javascript_pack_tag("application", "bootstrap")).to eq expected
end

it "#javascript_pack_tag generates correct tags by passing `defer: false`" do
it "#javascript_pack_tag generates the correct tags when passing `defer: false`" do
expected = <<~HTML.chomp
<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js"></script>
<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js"></script>
Expand All @@ -95,7 +95,7 @@ def base_url
expect(javascript_pack_tag("application", "bootstrap", defer: false)).to eq expected
end

it "#javascript_pack_tag generates correct appended tag" do
it "#javascript_pack_tag generates the correct appended tag" do
append_javascript_pack_tag("bootstrap", defer: false)

expected = <<~HTML.chomp
Expand All @@ -108,7 +108,7 @@ def base_url
expect(javascript_pack_tag("application")).to eq expected
end

it "#javascript_pack_tag generates correct prepended tag" do
it "#javascript_pack_tag generates the correct prepended tag" do
append_javascript_pack_tag("bootstrap")
prepend_javascript_pack_tag("main")

Expand All @@ -123,7 +123,7 @@ def base_url
expect(javascript_pack_tag("application")).to eq expected
end

it "#append_javascript_pack_tag raises error if called after calling #javascript_pack_tag" do
it "#append_javascript_pack_tag raises an error if called after calling #javascript_pack_tag" do
expected_error_message = \
"You can only call append_javascript_pack_tag before javascript_pack_tag helper. " +
"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#view-helper-append_javascript_pack_tag-prepend_javascript_pack_tag-and-append_stylesheet_pack_tag for the usage guide"
Expand All @@ -134,7 +134,7 @@ def base_url
}.to raise_error(expected_error_message)
end

it "#prepend_javascript_pack_tag raises error if called after calling #javascript_pack_tag" do
it "#prepend_javascript_pack_tag raises an error if called after calling #javascript_pack_tag" do
expected_error_message = \
"You can only call prepend_javascript_pack_tag before javascript_pack_tag helper. " +
"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#view-helper-append_javascript_pack_tag-prepend_javascript_pack_tag-and-append_stylesheet_pack_tag for the usage guide"
Expand All @@ -145,7 +145,7 @@ def base_url
}.to raise_error(expected_error_message)
end

it "#javascript_pack_tag generates correct tags by passing `defer: true`" do
it "#javascript_pack_tag generates the correct tags when passing `defer: true`" do
expected = <<~HTML.chomp
<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>
<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>
Expand All @@ -155,7 +155,7 @@ def base_url
expect(javascript_pack_tag("application", defer: true)).to eq expected
end

it "#javascript_pack_tag generates correct tags by passing symbol" do
it "#javascript_pack_tag generates the correct tags when passing a symbol" do
expected = <<~HTML.chomp
<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>
<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>
Expand All @@ -175,7 +175,7 @@ def base_url
}.to raise_error(expected_error_message)
end

it "#stylesheet_pack_tag generates correct link tag with string arguments" do
it "#stylesheet_pack_tag generates the correct link tag with string arguments" do
expected = (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks)
.uniq
.map { |chunk| stylesheet_link_tag(chunk) }
Expand All @@ -184,7 +184,7 @@ def base_url
expect(stylesheet_pack_tag("application", "hello_stimulus")).to eq expected
end

it "#stylesheet_pack_tag generates correct link tag with symbol arguments" do
it "#stylesheet_pack_tag generates the correct link tag with symbol arguments" do
expected = (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks)
.uniq
.map { |chunk| stylesheet_link_tag(chunk) }
Expand All @@ -193,7 +193,7 @@ def base_url
expect(stylesheet_pack_tag(:application, :hello_stimulus)).to eq expected
end

it "#stylesheet_pack_tag generates correct link tag with mixed arguments" do
it "#stylesheet_pack_tag generates the correct link tag with mixed arguments" do
expected = (application_stylesheet_chunks)
.map { |chunk| stylesheet_link_tag(chunk, media: "all") }
.join("\n")
Expand All @@ -215,7 +215,7 @@ def base_url
}.to_not raise_error
end

it "#stylesheet_pack_tag appends" do
it "#stylesheet_pack_tag appends tags" do
append_stylesheet_pack_tag(:hello_stimulus)

expect(stylesheet_pack_tag(:application)).to eq \
Expand All @@ -230,7 +230,7 @@ def base_url
(application_stylesheet_chunks + hello_stimulus_stylesheet_chunks).uniq.map { |chunk| stylesheet_link_tag(chunk) }.join("\n")
end

it "#stylesheet_pack_tag supports multiple invocations with different media attr" do
it "#stylesheet_pack_tag supports multiple invocations with different media attr values" do
app_style = stylesheet_pack_tag(:application)
app_style_with_media = stylesheet_pack_tag(:application, media: "print")
hello_stimulus_style_with_media = stylesheet_pack_tag(:hello_stimulus, media: "all")
Expand Down
Loading

0 comments on commit 54d3bcc

Please sign in to comment.