|
| 1 | +require "test_helper" |
| 2 | +require "minitest/mock" |
| 3 | +require "propshaft/asset" |
| 4 | +require "propshaft/assembly" |
| 5 | +require "propshaft/compilers" |
| 6 | + |
| 7 | +require "propshaft/compiler/js_asset_urls" |
| 8 | + |
| 9 | +module Propshaft |
| 10 | + class Compiler |
| 11 | + class JsAssetUrlsTest < ActiveSupport::TestCase |
| 12 | + setup do |
| 13 | + @options = ActiveSupport::OrderedOptions.new.tap do |config| |
| 14 | + config.paths = [Pathname.new("#{__dir__}/../../fixtures/assets/vendor")] |
| 15 | + config.output_path = Pathname.new("#{__dir__}/../../fixtures/output") |
| 16 | + config.prefix = "/assets" |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + test "the asset exists" do |
| 21 | + js_content = <<~JS |
| 22 | + export default class extends Controller { |
| 23 | + init() { |
| 24 | + this.img = rails_asset_url("/foobar/source/file.svg"); |
| 25 | + } |
| 26 | + } |
| 27 | + JS |
| 28 | + |
| 29 | + compiled = compile_asset_with_content(js_content) |
| 30 | + |
| 31 | + assert_match(%r{this\.img = "/assets/foobar/source/file-[a-z0-9]{8}.svg"\;}, compiled) |
| 32 | + end |
| 33 | + |
| 34 | + test "the asset does not exist" do |
| 35 | + js_content = <<~JS |
| 36 | + export default class extends Controller { |
| 37 | + init() { |
| 38 | + this.img = rails_asset_url("missing.svg"); |
| 39 | + } |
| 40 | + } |
| 41 | + JS |
| 42 | + |
| 43 | + compiled = compile_asset_with_content(js_content) |
| 44 | + |
| 45 | + assert_match(/this\.img = "missing.svg"\;/, compiled) |
| 46 | + end |
| 47 | + |
| 48 | + private |
| 49 | + |
| 50 | + def compile_asset_with_content(content) |
| 51 | + # This has one more set of .. than it would in the propshaft repo |
| 52 | + root_path = Pathname.new("#{__dir__}/../../fixtures/assets/vendor") |
| 53 | + logical_path = "foobar/source/test.js" |
| 54 | + |
| 55 | + assembly = Propshaft::Assembly.new(@options) |
| 56 | + assembly.compilers.register("text/javascript", Propshaft::Compiler::JsAssetUrls) |
| 57 | + |
| 58 | + asset = Propshaft::Asset.new(root_path.join(logical_path), logical_path: logical_path, load_path: assembly.load_path) |
| 59 | + asset.stub(:content, content) do |
| 60 | + assembly.compilers.compile(asset) |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | +end |
0 commit comments