about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJoël Quenneville <joel.quen@gmail.com>2017-04-23 00:05:52 -0400
committerEugen <eugen@zeonfederated.com>2017-04-23 06:05:52 +0200
commit1cf9e14a416cd70aaa85453c411d95a3b3c82472 (patch)
treed0d38bf3ebf38b4c3c0eb00caae079a6fa053371
parent0c2fe22bc19a82899c7577410bc869db7d42955a (diff)
Test embedded_view related code in a helper (#2282)
The two methods `StreamEntriesHelper#stream_link_target` and
`StreamEntriesHelper#acct` are based on checking whether we are running
in an embedded view.

This adds some test helper code to make the testing easier. We extracted
some "magic strings" to constants to lower the coupling in the specs.
-rw-r--r--app/helpers/stream_entries_helper.rb11
-rw-r--r--spec/helpers/stream_entries_helper_spec.rb63
2 files changed, 72 insertions, 2 deletions
diff --git a/app/helpers/stream_entries_helper.rb b/app/helpers/stream_entries_helper.rb
index a1dfe2ecf..ba989a84d 100644
--- a/app/helpers/stream_entries_helper.rb
+++ b/app/helpers/stream_entries_helper.rb
@@ -1,6 +1,9 @@
 # frozen_string_literal: true
 
 module StreamEntriesHelper
+  EMBEDDED_CONTROLLER = 'stream_entries'.freeze
+  EMBEDDED_ACTION = 'embed'.freeze
+
   def display_name(account)
     account.display_name.presence || account.username
   end
@@ -10,7 +13,11 @@ module StreamEntriesHelper
   end
 
   def acct(account)
-    "@#{account.acct}#{embedded_view? && account.local? ? "@#{Rails.configuration.x.local_domain}" : ''}"
+    if embedded_view? && account.local?
+      "@#{account.acct}@#{Rails.configuration.x.local_domain}"
+    else
+      "@#{account.acct}"
+    end
   end
 
   def style_classes(status, is_predecessor, is_successor, include_threads)
@@ -58,6 +65,6 @@ module StreamEntriesHelper
   end
 
   def embedded_view?
-    params[:controller] == 'stream_entries' && params[:action] == 'embed'
+    params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
   end
 end
diff --git a/spec/helpers/stream_entries_helper_spec.rb b/spec/helpers/stream_entries_helper_spec.rb
index a9e2d9c4b..1ef49a3ab 100644
--- a/spec/helpers/stream_entries_helper_spec.rb
+++ b/spec/helpers/stream_entries_helper_spec.rb
@@ -15,6 +15,69 @@ RSpec.describe StreamEntriesHelper, type: :helper do
     end
   end
 
+  describe '#stream_link_target' do
+    it 'returns nil if it is not an embedded view' do
+      set_not_embedded_view
+
+      expect(helper.stream_link_target).to be_nil
+    end
+
+    it 'returns _blank if it is an embedded view' do
+      set_embedded_view
+
+      expect(helper.stream_link_target).to eq '_blank'
+    end
+  end
+
+  describe '#acct' do
+    it 'is fully qualified for embedded local accounts' do
+      allow(Rails.configuration.x).to receive(:local_domain).and_return('local_domain')
+      set_embedded_view
+      account = Account.new(domain: nil, username: 'user')
+
+      acct = helper.acct(account)
+
+      expect(acct).to eq '@user@local_domain'
+    end
+
+    it 'is fully qualified for embedded foreign accounts' do
+      set_embedded_view
+      account = Account.new(domain: 'foreign_server.com', username: 'user')
+
+      acct = helper.acct(account)
+
+      expect(acct).to eq '@user@foreign_server.com'
+    end
+
+    it 'is fully qualified for non embedded foreign accounts' do
+      set_not_embedded_view
+      account = Account.new(domain: 'foreign_server.com', username: 'user')
+
+      acct = helper.acct(account)
+
+      expect(acct).to eq '@user@foreign_server.com'
+    end
+
+    it 'is the shortname for non embedded local accounts' do
+      set_not_embedded_view
+      account = Account.new(domain: nil, username: 'user')
+
+      acct = helper.acct(account)
+
+      expect(acct).to eq '@user'
+    end
+  end
+
+  def set_not_embedded_view
+    params[:controller] = "not_#{StreamEntriesHelper::EMBEDDED_CONTROLLER}"
+    params[:action] = "not_#{StreamEntriesHelper::EMBEDDED_ACTION}"
+  end
+
+  def set_embedded_view
+    params[:controller] = StreamEntriesHelper::EMBEDDED_CONTROLLER
+    params[:action] = StreamEntriesHelper::EMBEDDED_ACTION
+  end
+  
   describe '#style_classes' do
     it do
       status = double(reblog?: false)