Skip to content

Commit

Permalink
add some specs to InlineSvg::WebpackAssetFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
kylefox committed Feb 13, 2020
1 parent 24d27e0 commit 17ca964
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions inline_svg.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec_junit_formatter", "0.2.2"
spec.add_development_dependency "pry"
spec.add_development_dependency "rubocop"
spec.add_development_dependency "webmock"

spec.add_runtime_dependency "activesupport", ">= 3.0"
spec.add_runtime_dependency "nokogiri", ">= 1.6"
Expand Down
6 changes: 5 additions & 1 deletion lib/inline_svg/webpack_asset_finder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'tempfile'
require 'net/http'
require 'openssl'

This comment has been minimized.

Copy link
@kylefox

kylefox Feb 13, 2020

Author Owner

Gross that these are included, even when not required (i.e. webpack dev server is not running).

module InlineSvg
class WebpackAssetFinder
def self.find_asset(filename)
Expand All @@ -10,7 +14,7 @@ def initialize(filename)
end

def pathname
return if @asset_path.blank?
return unless @asset_path

This comment has been minimized.

Copy link
@kylefox

kylefox Feb 13, 2020

Author Owner

.blank? isn't available because activesupport isn't loaded.


if Webpacker.dev_server.running?
dev_server_asset(@asset_path)
Expand Down
78 changes: 78 additions & 0 deletions spec/webpack_asset_finder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require_relative '../lib/inline_svg/webpack_asset_finder'
require 'webmock'

describe InlineSvg::WebpackAssetFinder do
include WebMock::API
SVG_STRING = '<svg><!-- Pretty shapes go here --></svg>'.freeze

before do
WebMock.enable!

Webpacker = double(
'Webpacker',
manifest: double('Webpacker.manifest'),
dev_server: double('Webpacker.dev_server',
host: 'webpack.test', port: 3035, https?: dev_server_uses_https, running?: dev_server_running
)
)

allow(Webpacker.manifest).to receive(:lookup) { '/packs/media/example.svg' if asset_exists }

stub_request(
:get,
"#{dev_server_uses_https ? 'https' : 'http'}://webpack.test:3035/packs/media/example.svg"
).to_return(
status: asset_exists ? 200 : 404,
body: SVG_STRING
)

Rails = double('Rails', logger: double)
allow(Rails.logger).to receive(:error)
end

shared_examples 'valid SVG' do
it { expect(subject.pathname.read).to eq SVG_STRING }
end

shared_examples 'invalid SVG' do
it { expect(subject.pathname).to be nil }
end

subject { InlineSvg::WebpackAssetFinder.new('media/example.svg') }

context 'asset exists' do
let(:asset_exists) { true }

context 'dev server is running' do
let(:dev_server_running) { true }

context 'dev server is using HTTPS' do
let(:dev_server_uses_https) { true }
it_behaves_like 'valid SVG'
end

context 'dev server is using HTTP' do
let(:dev_server_uses_https) { false }
it_behaves_like 'valid SVG'
end
end
end

context 'asset does not exist' do
let(:asset_exists) { false }

context 'dev server is running' do
let(:dev_server_running) { true }

context 'dev server is using HTTPS' do
let(:dev_server_uses_https) { true }
it_behaves_like 'invalid SVG'
end

context 'dev server is using HTTP' do
let(:dev_server_uses_https) { false }
it_behaves_like 'invalid SVG'
end
end
end
end

0 comments on commit 17ca964

Please sign in to comment.