File: inline_svg_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 (132 lines) | stat: -rw-r--r-- 4,578 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
begin
  require_relative '../lib/inline_svg'
rescue LoadError
  require "inline_svg"
end

class MyCustomTransform
  def self.create_with_value(value); end
  def transform(doc); end
end

class MyInvalidCustomTransformKlass
  def transform(doc); end
end

class MyInvalidCustomTransformInstance
  def self.create_with_value(value); end
end

class MyCustomAssetFile
  def self.named(filename); end
end

describe InlineSvg do
  describe "configuration" do
    context "when a block is not given" do
      it "complains" do
        expect do
          InlineSvg.configure
        end.to raise_error(InlineSvg::Configuration::Invalid)
      end
    end

    context "asset finder" do
      it "allows an asset finder to be assigned" do
        sprockets = double('SomethingLikeSprockets', find_asset: 'some asset')
        InlineSvg.configure do |config|
          config.asset_finder = sprockets
        end

        expect(InlineSvg.configuration.asset_finder).to eq sprockets
      end

      it "falls back to StaticAssetFinder when the provided asset finder does not implement #find_asset" do
        InlineSvg.configure do |config|
          config.asset_finder = 'Not a real asset finder'
        end

        expect(InlineSvg.configuration.asset_finder).to eq InlineSvg::StaticAssetFinder
      end
    end

    context "configuring a custom asset file" do
      it "falls back to the built-in asset file implementation by default" do
        expect(InlineSvg.configuration.asset_file).to eq(InlineSvg::AssetFile)
      end

      it "adds a collaborator that meets the interface specification" do
        InlineSvg.configure do |config|
          config.asset_file = MyCustomAssetFile
        end

        expect(InlineSvg.configuration.asset_file).to eq MyCustomAssetFile
      end

      it "rejects a collaborator that does not conform to the interface spec" do
        bad_asset_file = double("bad_asset_file")

        expect do
          InlineSvg.configure do |config|
            config.asset_file = bad_asset_file
          end
        end.to raise_error(InlineSvg::Configuration::Invalid, /asset_file should implement the #named method/)
      end

      it "rejects a collaborator that implements the correct interface with the wrong arity" do
        bad_asset_file = double("bad_asset_file", named: nil)

        expect do
          InlineSvg.configure do |config|
            config.asset_file = bad_asset_file
          end
        end.to raise_error(InlineSvg::Configuration::Invalid, /asset_file should implement the #named method with arity 1/)
      end
    end

    context "configuring the default svg-not-found class" do
      it "sets the class name" do
        InlineSvg.configure do |config|
          config.svg_not_found_css_class = 'missing-svg'
        end

        expect(InlineSvg.configuration.svg_not_found_css_class).to eq 'missing-svg'
      end
    end

    context "configuring custom transformation" do
      it "allows a custom transformation to be added" do
        InlineSvg.configure do |config|
          config.add_custom_transformation(attribute: :my_transform, transform: MyCustomTransform)
        end

        expect(InlineSvg.configuration.custom_transformations).to eq({my_transform: {attribute: :my_transform, transform: MyCustomTransform}})
      end

      it "rejects transformations that do not implement .create_with_value" do
        expect do
          InlineSvg.configure do |config|
            config.add_custom_transformation(attribute: :irrelevant, transform: MyInvalidCustomTransformKlass)
          end
        end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformKlass} should implement the .create_with_value and #transform methods/)
      end

      it "rejects transformations that does not implement #transform" do
        expect do
          InlineSvg.configure do |config|
            config.add_custom_transformation(attribute: :irrelevant, transform: MyInvalidCustomTransformInstance)
          end
        end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformInstance} should implement the .create_with_value and #transform methods/)
      end

      it "rejects transformations that are not classes" do
        expect do
          InlineSvg.configure do |config|
            config.add_custom_transformation(attribute: :irrelevant, transform: :not_a_class)
          end
        end.to raise_error(InlineSvg::Configuration::Invalid, /#{:not_a_class} should implement the .create_with_value and #transform methods/)
      end

    end
  end
end