Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/components/icon_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ def initialize(icon:, **tag_options)
def icon_path
# Revert back to using design system image once upstream CSP fix has been patched.
# See: https://github.com/uswds/uswds/pull/4487
[image_path('sprite.svg'), '#', icon].join
[image_path('sprite.svg', host: asset_host), '#', icon].join
end

private

def asset_host
if Rails.env.production?
IdentityConfig.store.domain_name
Comment on lines 269 to 270
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes from the fallback here:

(IdentityConfig.store.asset_host.presence || IdentityConfig.store.domain_name) if request

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this branch is not covered by tests, I'd consider adding it

if we don't want to stub Rails.env, we could do some dependency injection:

def asset_host(rails_env = Rails.env)
  if rails_env.production?

and then in the test pass in a ActiveSupport::EnvironmentInquirer.new("production") or ActiveSupport::EnvironmentInquirer.new("development") etc

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this branch is not covered by tests, I'd consider adding it

👍 Added specs in rebased 8f82e3b.

Went for stubbing due to light preference to keep asset_host a private method.

elsif request
request.base_url
Comment on lines 271 to 272
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end
end
end
24 changes: 23 additions & 1 deletion spec/components/icon_component_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
require 'rails_helper'

RSpec.describe IconComponent, type: :component do
let(:asset_host) { 'http://wrong.example.com' }
let(:domain_name) { 'http://correct.example.com' }

before do
allow(IdentityConfig.store).to receive(:asset_host).and_return(asset_host)
allow(IdentityConfig.store).to receive(:domain_name).and_return(domain_name)
end

it 'renders icon svg' do
rendered = render_inline IconComponent.new(icon: :print)

expect(rendered).to have_css('.usa-icon use[href$=".svg#print"]')
expect(rendered).to have_css(".usa-icon use[href^='#{request.base_url}'][href$='.svg#print']")
end

context 'with invalid icon' do
Expand All @@ -28,4 +36,18 @@
expect(rendered).to have_css('.usa-icon[data-foo="bar"]')
end
end

context 'in production' do
before do
allow(Rails.env).to receive(:production?).and_return(true)
end

it 'bypasses configured asset_host and uses domain_name instead' do
rendered = render_inline IconComponent.new(icon: :print)

href = rendered.css('use').first['href']

expect(href).to start_with(domain_name)
end
end
end