require "test_helper" class HelperTest < ActionView::TestCase tests Webpacker::Helper attr_reader :request def setup @request = Class.new do def send_early_hints(links) end def base_url "https://example.com" end end.new end def test_asset_pack_path assert_equal "/packs/bootstrap-300631c4f0e0f9c865bc.js", asset_pack_path("bootstrap.js") assert_equal "/packs/bootstrap-c38deda30895059837cf.css", asset_pack_path("bootstrap.css") end def test_asset_pack_url assert_equal "https://example.com/packs/bootstrap-300631c4f0e0f9c865bc.js", asset_pack_url("bootstrap.js") assert_equal "https://example.com/packs/bootstrap-c38deda30895059837cf.css", asset_pack_url("bootstrap.css") end def test_image_pack_path assert_equal "/packs/application-k344a6d59eef8632c9d1.png", image_pack_path("application.png") assert_equal "/packs/media/images/image-c38deda30895059837cf.jpg", image_pack_path("image.jpg") assert_equal "/packs/media/images/image-c38deda30895059837cf.jpg", image_pack_path("media/images/image.jpg") assert_equal "/packs/media/images/nested/image-c38deda30895059837cf.jpg", image_pack_path("nested/image.jpg") assert_equal "/packs/media/images/nested/image-c38deda30895059837cf.jpg", image_pack_path("media/images/nested/image.jpg") end def test_image_pack_url assert_equal "https://example.com/packs/application-k344a6d59eef8632c9d1.png", image_pack_url("application.png") assert_equal "https://example.com/packs/media/images/image-c38deda30895059837cf.jpg", image_pack_url("image.jpg") assert_equal "https://example.com/packs/media/images/image-c38deda30895059837cf.jpg", image_pack_url("media/images/image.jpg") assert_equal "https://example.com/packs/media/images/nested/image-c38deda30895059837cf.jpg", image_pack_url("nested/image.jpg") assert_equal "https://example.com/packs/media/images/nested/image-c38deda30895059837cf.jpg", image_pack_url("media/images/nested/image.jpg") end def test_image_pack_tag assert_equal \ "\"Edit", image_pack_tag("application.png", size: "16x10", alt: "Edit Entry") assert_equal \ "\"Edit", image_pack_tag("image.jpg", size: "16x10", alt: "Edit Entry") assert_equal \ "\"Edit", image_pack_tag("media/images/image.jpg", size: "16x10", alt: "Edit Entry") assert_equal \ "\"Edit", image_pack_tag("nested/image.jpg", size: "16x10", alt: "Edit Entry") assert_equal \ "\"Edit", image_pack_tag("media/images/nested/image.jpg", size: "16x10", alt: "Edit Entry") assert_equal \ "", image_pack_tag("media/images/image.jpg", srcset: { "media/images/image-2x.jpg" => "2x" }) end def test_favicon_pack_tag assert_equal \ "", favicon_pack_tag("application.png", rel: "apple-touch-icon", type: "image/png") assert_equal \ "", favicon_pack_tag("mb-icon.png", rel: "apple-touch-icon", type: "image/png") assert_equal \ "", favicon_pack_tag("media/images/mb-icon.png", rel: "apple-touch-icon", type: "image/png") assert_equal \ "", favicon_pack_tag("nested/mb-icon.png", rel: "apple-touch-icon", type: "image/png") assert_equal \ "", favicon_pack_tag("media/images/nested/mb-icon.png", rel: "apple-touch-icon", type: "image/png") end def test_javascript_pack_tag assert_equal \ %(), javascript_pack_tag("bootstrap.js") end def test_javascript_pack_tag_symbol assert_equal \ %(), javascript_pack_tag(:bootstrap) end def test_javascript_pack_tag_splat assert_equal \ %(\n) + %(), javascript_pack_tag("bootstrap.js", "application.js", defer: true) end def test_javascript_pack_tag_split_chunks assert_equal \ %(\n) + %(\n) + %(), javascript_packs_with_chunks_tag("application") end def test_preload_pack_asset if self.class.method_defined?(:preload_link_tag) assert_equal \ %(), preload_pack_asset("fonts/fa-regular-400.woff2") else error = assert_raises do preload_pack_asset("fonts/fa-regular-400.woff2") end assert_equal \ "You need Rails >= 5.2 to use this tag.", error.message end end def test_stylesheet_pack_tag_split_chunks assert_equal \ %(\n) + %(\n) + %(), stylesheet_packs_with_chunks_tag("application", "hello_stimulus") end def test_stylesheet_pack_tag assert_equal \ %(), stylesheet_pack_tag("bootstrap.css") end def test_stylesheet_pack_tag_symbol assert_equal \ %(), stylesheet_pack_tag(:bootstrap) end def test_stylesheet_pack_tag_splat assert_equal \ %(\n) + %(), stylesheet_pack_tag("bootstrap.css", "application.css", media: "all") end end