File: finds_asset_paths_spec.rb

package info (click to toggle)
ruby-inline-svg 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: ruby: 1,711; makefile: 4
file content (97 lines) | stat: -rw-r--r-- 3,310 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'pathname'
begin
  require_relative '../lib/inline_svg'
rescue LoadError
  require "inline_svg"
end

describe InlineSvg::FindsAssetPaths do
  context "when sprockets finder returns an object which supports only the pathname method" do
    it "returns fully qualified file paths from Sprockets" do
      sprockets = double('SprocketsDouble')

      expect(sprockets).to receive(:find_asset).with('some-file').
        and_return(double(pathname: Pathname('/full/path/to/some-file')))

      InlineSvg.configure do |config|
        config.asset_finder = sprockets
      end

      expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
    end
  end

  context "when sprockets finder returns an object which supports only the filename method" do
    it "returns fully qualified file paths from Sprockets" do
      sprockets = double('SprocketsDouble')

      expect(sprockets).to receive(:find_asset).with('some-file').
        and_return(double(filename: Pathname('/full/path/to/some-file')))

      InlineSvg.configure do |config|
        config.asset_finder = sprockets
      end

      expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
    end
  end

  context "when asset is not found" do
    it "returns nil" do
      sprockets = double('SprocketsDouble')

      expect(sprockets).to receive(:find_asset).with('some-file').and_return(nil)

      InlineSvg.configure do |config|
        config.asset_finder = sprockets
      end

      expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to be_nil
    end
  end

  context "when propshaft finder returns an object which supports only the pathname method" do
    it "returns fully qualified file paths from Propshaft" do
      propshaft = double('PropshaftDouble')

      expect(propshaft).to receive(:find_asset).with('some-file').
        and_return(double(pathname: Pathname('/full/path/to/some-file')))

      InlineSvg.configure do |config|
        config.asset_finder = propshaft
      end

      expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
    end
  end

  context "when webpack finder returns an object with a relative asset path" do
    it "returns the fully qualified file path" do
      webpacker = double('WebpackerDouble')

      expect(webpacker).to receive(:find_asset).with('some-file').
        and_return(double(filename: Pathname('/full/path/to/some-file')))

      InlineSvg.configure do |config|
        config.asset_finder = webpacker
      end

      expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
    end
  end

  context "when webpack finder returns an object with an absolute http asset path" do
    it "returns the fully qualified file path" do
      webpacker = double('WebpackerDouble')

      expect(webpacker).to receive(:find_asset).with('some-file').
        and_return(double(filename: Pathname('https://my-fancy-domain.test/full/path/to/some-file')))

      InlineSvg.configure do |config|
        config.asset_finder = webpacker
      end

      expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('https://my-fancy-domain.test/full/path/to/some-file')
    end
  end
end