Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Test embedded_view related code in a helper (mastodon#2282)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JoelQ authored and Gargron committed Apr 23, 2017
1 parent 0c2fe22 commit 1cf9e14
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/helpers/stream_entries_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,7 +13,11 @@ def stream_link_target
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)
Expand Down Expand Up @@ -58,6 +65,6 @@ def rtl_size(characters)
end

def embedded_view?
params[:controller] == 'stream_entries' && params[:action] == 'embed'
params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
end
end
63 changes: 63 additions & 0 deletions spec/helpers/stream_entries_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,69 @@
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)
Expand Down

0 comments on commit 1cf9e14

Please sign in to comment.