about summary refs log tree commit diff
path: root/spec/helpers
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2023-04-14 19:22:47 -0500
committerStarfall <us@starfall.systems>2023-04-14 19:22:47 -0500
commit4fe1689de43f4404eb9530fcfbcbfb26d6c1c13a (patch)
tree6811b845bb7f4966b10dcefa3dea404246f161c7 /spec/helpers
parent65c1e53a32cabcdbb7bca57002bb0f6acdebe07e (diff)
parentbed63f6dae0879ac840066b031229e0d139089cd (diff)
Merge remote-tracking branch 'glitch/main' HEAD main
Diffstat (limited to 'spec/helpers')
-rw-r--r--spec/helpers/accounts_helper_spec.rb10
-rw-r--r--spec/helpers/admin/account_moderation_notes_helper_spec.rb10
-rw-r--r--spec/helpers/admin/action_logs_helper_spec.rb (renamed from spec/helpers/admin/action_log_helper_spec.rb)0
-rw-r--r--spec/helpers/admin/dashboard_helper_spec.rb69
-rw-r--r--spec/helpers/admin/filter_helper_spec.rb2
-rw-r--r--spec/helpers/admin/trends/statuses_helper_spec.rb54
-rw-r--r--spec/helpers/application_helper_spec.rb23
-rw-r--r--spec/helpers/home_helper_spec.rb114
-rw-r--r--spec/helpers/jsonld_helper_spec.rb14
-rw-r--r--spec/helpers/languages_helper_spec.rb48
-rw-r--r--spec/helpers/settings_helper_spec.rb37
-rw-r--r--spec/helpers/statuses_helper_spec.rb92
12 files changed, 441 insertions, 32 deletions
diff --git a/spec/helpers/accounts_helper_spec.rb b/spec/helpers/accounts_helper_spec.rb
index 2b35b23b7..184b47dec 100644
--- a/spec/helpers/accounts_helper_spec.rb
+++ b/spec/helpers/accounts_helper_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 require 'rails_helper'
 
 RSpec.describe AccountsHelper, type: :helper do
@@ -13,15 +15,15 @@ RSpec.describe AccountsHelper, type: :helper do
 
   describe '#display_name' do
     it 'uses the display name when it exists' do
-      account = Account.new(display_name: "Display", username: "Username")
+      account = Account.new(display_name: 'Display', username: 'Username')
 
-      expect(helper.display_name(account)).to eq "Display"
+      expect(helper.display_name(account)).to eq 'Display'
     end
 
     it 'uses the username when display name is nil' do
-      account = Account.new(display_name: nil, username: "Username")
+      account = Account.new(display_name: nil, username: 'Username')
 
-      expect(helper.display_name(account)).to eq "Username"
+      expect(helper.display_name(account)).to eq 'Username'
     end
   end
 
diff --git a/spec/helpers/admin/account_moderation_notes_helper_spec.rb b/spec/helpers/admin/account_moderation_notes_helper_spec.rb
index 622ce8806..e01eba51d 100644
--- a/spec/helpers/admin/account_moderation_notes_helper_spec.rb
+++ b/spec/helpers/admin/account_moderation_notes_helper_spec.rb
@@ -42,13 +42,11 @@ RSpec.describe Admin::AccountModerationNotesHelper, type: :helper do
       let(:account) { Fabricate(:account) }
 
       it 'calls #link_to' do
-        expect(helper).to receive(:link_to).with(
-          admin_account_path(account.id),
-          class: name_tag_classes(account, true),
-          title: account.acct
-        )
+        result = helper.admin_account_inline_link_to(account)
 
-        helper.admin_account_inline_link_to(account)
+        expect(result).to match(name_tag_classes(account, true))
+        expect(result).to match(account.acct)
+        expect(result).to match(admin_account_path(account.id))
       end
     end
   end
diff --git a/spec/helpers/admin/action_log_helper_spec.rb b/spec/helpers/admin/action_logs_helper_spec.rb
index 9d7ed4ab7..9d7ed4ab7 100644
--- a/spec/helpers/admin/action_log_helper_spec.rb
+++ b/spec/helpers/admin/action_logs_helper_spec.rb
diff --git a/spec/helpers/admin/dashboard_helper_spec.rb b/spec/helpers/admin/dashboard_helper_spec.rb
new file mode 100644
index 000000000..59062e483
--- /dev/null
+++ b/spec/helpers/admin/dashboard_helper_spec.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe Admin::DashboardHelper do
+  describe 'relevant_account_timestamp' do
+    context 'with an account with older sign in' do
+      let(:account) { Fabricate(:account) }
+      let(:stamp) { 10.days.ago }
+
+      it 'returns a time element' do
+        account.user.update(current_sign_in_at: stamp)
+        result = helper.relevant_account_timestamp(account)
+
+        expect(result).to match('time-ago')
+        expect(result).to match(I18n.l(stamp))
+      end
+    end
+
+    context 'with an account with newer sign in' do
+      let(:account) { Fabricate(:account) }
+
+      it 'returns a time element' do
+        account.user.update(current_sign_in_at: 10.hours.ago)
+        result = helper.relevant_account_timestamp(account)
+
+        expect(result).to eq(I18n.t('generic.today'))
+      end
+    end
+
+    context 'with an account where the user is pending' do
+      let(:account) { Fabricate(:account) }
+
+      it 'returns a time element' do
+        account.user.update(current_sign_in_at: nil)
+        account.user.update(approved: false)
+        result = helper.relevant_account_timestamp(account)
+
+        expect(result).to match('time-ago')
+        expect(result).to match(I18n.l(account.user.created_at))
+      end
+    end
+
+    context 'with an account with a last status value' do
+      let(:account) { Fabricate(:account) }
+      let(:stamp) { 5.minutes.ago }
+
+      it 'returns a time element' do
+        account.user.update(current_sign_in_at: nil)
+        account.account_stat.update(last_status_at: stamp)
+        result = helper.relevant_account_timestamp(account)
+
+        expect(result).to match('time-ago')
+        expect(result).to match(I18n.l(stamp))
+      end
+    end
+
+    context 'with an account without sign in or last status or pending' do
+      let(:account) { Fabricate(:account) }
+
+      it 'returns a time element' do
+        account.user.update(current_sign_in_at: nil)
+        result = helper.relevant_account_timestamp(account)
+
+        expect(result).to eq('-')
+      end
+    end
+  end
+end
diff --git a/spec/helpers/admin/filter_helper_spec.rb b/spec/helpers/admin/filter_helper_spec.rb
index 9d4ea2829..bbf90a996 100644
--- a/spec/helpers/admin/filter_helper_spec.rb
+++ b/spec/helpers/admin/filter_helper_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 require 'rails_helper'
 
 describe Admin::FilterHelper do
diff --git a/spec/helpers/admin/trends/statuses_helper_spec.rb b/spec/helpers/admin/trends/statuses_helper_spec.rb
new file mode 100644
index 000000000..92caae690
--- /dev/null
+++ b/spec/helpers/admin/trends/statuses_helper_spec.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe Admin::Trends::StatusesHelper do
+  describe '.one_line_preview' do
+    before do
+      allow(helper).to receive(:current_user).and_return(Fabricate.build(:user))
+    end
+
+    context 'with a local status' do
+      let(:status) { Fabricate.build(:status, text: 'Test local status') }
+
+      it 'renders a correct preview text' do
+        result = helper.one_line_preview(status)
+
+        expect(result).to eq 'Test local status'
+      end
+    end
+
+    context 'with a remote status' do
+      let(:status) { Fabricate.build(:status, uri: 'https://sfd.sdf', text: '<html><body><p>Test remote status</p><p>text</p></body></html>') }
+
+      it 'renders a correct preview text' do
+        result = helper.one_line_preview(status)
+
+        expect(result).to eq 'Test remote status'
+      end
+    end
+
+    context 'with a status that has empty text' do
+      let(:status) { Fabricate.build(:status, text: '') }
+
+      it 'renders a correct preview text' do
+        result = helper.one_line_preview(status)
+
+        expect(result).to eq ''
+      end
+    end
+
+    context 'with a status that has emoji' do
+      before { Fabricate(:custom_emoji, shortcode: 'florpy') }
+
+      let(:status) { Fabricate(:status, text: 'hello there :florpy:') }
+
+      it 'renders a correct preview text' do
+        result = helper.one_line_preview(status)
+
+        expect(result).to match 'hello there'
+        expect(result).to match '<img rel="emoji"'
+      end
+    end
+  end
+end
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index 1dbd985bf..2db2ee288 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 require 'rails_helper'
 
 describe ApplicationHelper do
@@ -5,8 +7,8 @@ describe ApplicationHelper do
     it 'returns active when on the current page' do
       allow(helper).to receive(:current_page?).and_return(true)
 
-      result = helper.active_nav_class("/test")
-      expect(result).to eq "active"
+      result = helper.active_nav_class('/test')
+      expect(result).to eq 'active'
     end
 
     it 'returns active when on a current page' do
@@ -14,14 +16,14 @@ describe ApplicationHelper do
       allow(helper).to receive(:current_page?).with('/test').and_return(true)
 
       result = helper.active_nav_class('/foo', '/test')
-      expect(result).to eq "active"
+      expect(result).to eq 'active'
     end
 
     it 'returns empty string when not on current page' do
       allow(helper).to receive(:current_page?).and_return(false)
 
-      result = helper.active_nav_class("/test")
-      expect(result).to eq ""
+      result = helper.active_nav_class('/test')
+      expect(result).to eq ''
     end
   end
 
@@ -65,7 +67,7 @@ describe ApplicationHelper do
         expect(Setting).to receive(:registrations_mode).and_return('open')
       end
 
-      expect(helper.open_registrations?).to eq true
+      expect(helper.open_registrations?).to be true
     end
 
     it 'returns false when closed for registrations' do
@@ -73,7 +75,7 @@ describe ApplicationHelper do
         expect(Setting).to receive(:registrations_mode).and_return('none')
       end
 
-      expect(helper.open_registrations?).to eq false
+      expect(helper.open_registrations?).to be false
     end
   end
 
@@ -82,8 +84,9 @@ describe ApplicationHelper do
       before do
         allow(helper).to receive(:user_signed_in?).and_return(true)
       end
+
       it 'does not show landing strip' do
-        expect(helper.show_landing_strip?).to eq false
+        expect(helper.show_landing_strip?).to be false
       end
     end
 
@@ -95,13 +98,13 @@ describe ApplicationHelper do
       it 'does not show landing strip on single user instance' do
         allow(helper).to receive(:single_user_mode?).and_return(true)
 
-        expect(helper.show_landing_strip?).to eq false
+        expect(helper.show_landing_strip?).to be false
       end
 
       it 'shows landing strip on multi user instance' do
         allow(helper).to receive(:single_user_mode?).and_return(false)
 
-        expect(helper.show_landing_strip?).to eq true
+        expect(helper.show_landing_strip?).to be true
       end
     end
   end
diff --git a/spec/helpers/home_helper_spec.rb b/spec/helpers/home_helper_spec.rb
index a3dc6f836..3d2c5fe24 100644
--- a/spec/helpers/home_helper_spec.rb
+++ b/spec/helpers/home_helper_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 require 'rails_helper'
 
 RSpec.describe HomeHelper, type: :helper do
@@ -6,4 +8,116 @@ RSpec.describe HomeHelper, type: :helper do
       expect(helper.default_props).to eq locale: I18n.locale
     end
   end
+
+  describe 'account_link_to' do
+    context 'with a missing account' do
+      let(:account) { nil }
+
+      it 'returns a button' do
+        result = helper.account_link_to(account)
+
+        expect(result).to match t('about.contact_missing')
+      end
+    end
+
+    context 'with a valid account' do
+      let(:account) { Fabricate(:account) }
+
+      it 'returns a link to the account' do
+        without_partial_double_verification do
+          allow(helper).to receive(:current_account).and_return(account)
+          allow(helper).to receive(:prefers_autoplay?).and_return(false)
+          result = helper.account_link_to(account)
+
+          expect(result).to match "@#{account.acct}"
+        end
+      end
+    end
+  end
+
+  describe 'obscured_counter' do
+    context 'with a value of less than zero' do
+      let(:count) { -10 }
+
+      it 'returns the correct string' do
+        expect(helper.obscured_counter(count)).to eq '0'
+      end
+    end
+
+    context 'with a value of zero' do
+      let(:count) { 0 }
+
+      it 'returns the correct string' do
+        expect(helper.obscured_counter(count)).to eq '0'
+      end
+    end
+
+    context 'with a value of one' do
+      let(:count) { 1 }
+
+      it 'returns the correct string' do
+        expect(helper.obscured_counter(count)).to eq '1'
+      end
+    end
+
+    context 'with a value of more than one' do
+      let(:count) { 10 }
+
+      it 'returns the correct string' do
+        expect(helper.obscured_counter(count)).to eq '1+'
+      end
+    end
+  end
+
+  describe 'custom_field_classes' do
+    context 'with a verified field' do
+      let(:field) { instance_double(Account::Field, verified?: true) }
+
+      it 'returns verified string' do
+        result = helper.custom_field_classes(field)
+        expect(result).to eq 'verified'
+      end
+    end
+
+    context 'with a non-verified field' do
+      let(:field) { instance_double(Account::Field, verified?: false) }
+
+      it 'returns verified string' do
+        result = helper.custom_field_classes(field)
+        expect(result).to eq 'emojify'
+      end
+    end
+  end
+
+  describe 'sign_up_messages' do
+    context 'with closed registrations' do
+      it 'returns correct sign up message' do
+        allow(helper).to receive(:closed_registrations?).and_return(true)
+        result = helper.sign_up_message
+
+        expect(result).to eq t('auth.registration_closed', instance: 'cb6e6126.ngrok.io')
+      end
+    end
+
+    context 'with open registrations' do
+      it 'returns correct sign up message' do
+        allow(helper).to receive(:closed_registrations?).and_return(false)
+        allow(helper).to receive(:open_registrations?).and_return(true)
+        result = helper.sign_up_message
+
+        expect(result).to eq t('auth.register')
+      end
+    end
+
+    context 'with approved registrations' do
+      it 'returns correct sign up message' do
+        allow(helper).to receive(:closed_registrations?).and_return(false)
+        allow(helper).to receive(:open_registrations?).and_return(false)
+        allow(helper).to receive(:approved_registrations?).and_return(true)
+        result = helper.sign_up_message
+
+        expect(result).to eq t('auth.apply_for_account')
+      end
+    end
+  end
 end
diff --git a/spec/helpers/jsonld_helper_spec.rb b/spec/helpers/jsonld_helper_spec.rb
index 744a14f26..ddd4bfe62 100644
--- a/spec/helpers/jsonld_helper_spec.rb
+++ b/spec/helpers/jsonld_helper_spec.rb
@@ -66,14 +66,14 @@ describe JsonLdHelper do
         stub_request(:get, 'https://mallory.test/').to_return body: '{"id": "https://marvin.test/"}'
         stub_request(:get, 'https://marvin.test/').to_return body: '{"id": "https://alice.test/"}'
 
-        expect(fetch_resource('https://mallory.test/', false)).to eq nil
+        expect(fetch_resource('https://mallory.test/', false)).to be_nil
       end
     end
 
     context 'when the second argument is true' do
       it 'returns nil if the retrieved ID and the given URI does not match' do
         stub_request(:get, 'https://mallory.test/').to_return body: '{"id": "https://alice.test/"}'
-        expect(fetch_resource('https://mallory.test/', true)).to eq nil
+        expect(fetch_resource('https://mallory.test/', true)).to be_nil
       end
     end
   end
@@ -81,7 +81,7 @@ describe JsonLdHelper do
   describe '#fetch_resource_without_id_validation' do
     it 'returns nil if the status code is not 200' do
       stub_request(:get, 'https://host.test/').to_return status: 400, body: '{}'
-      expect(fetch_resource_without_id_validation('https://host.test/')).to eq nil
+      expect(fetch_resource_without_id_validation('https://host.test/')).to be_nil
     end
 
     it 'returns hash' do
@@ -113,7 +113,7 @@ describe JsonLdHelper do
             {
               'type' => 'Mention',
               'href' => ['foo'],
-            }
+            },
           ],
         },
         'signature' => {
@@ -150,7 +150,7 @@ describe JsonLdHelper do
         patch_for_forwarding!(json, compacted)
         expect(compacted['to']).to eq ['https://www.w3.org/ns/activitystreams#Public']
         expect(compacted.dig('object', 'tag', 0, 'href')).to eq ['foo']
-        expect(safe_for_forwarding?(json, compacted)).to eq true
+        expect(safe_for_forwarding?(json, compacted)).to be true
       end
     end
 
@@ -160,14 +160,14 @@ describe JsonLdHelper do
         compacted = compact(json)
         deemed_compatible = patch_for_forwarding!(json, compacted)
         expect(compacted['to']).to eq ['https://www.w3.org/ns/activitystreams#Public']
-        expect(safe_for_forwarding?(json, compacted)).to eq true
+        expect(safe_for_forwarding?(json, compacted)).to be true
       end
 
       it 'deems an unsafe compacting as such' do
         compacted = compact(json)
         deemed_compatible = patch_for_forwarding!(json, compacted)
         expect(compacted['to']).to eq ['https://www.w3.org/ns/activitystreams#Public']
-        expect(safe_for_forwarding?(json, compacted)).to eq false
+        expect(safe_for_forwarding?(json, compacted)).to be false
       end
     end
   end
diff --git a/spec/helpers/languages_helper_spec.rb b/spec/helpers/languages_helper_spec.rb
index 217c9b239..98c8064a3 100644
--- a/spec/helpers/languages_helper_spec.rb
+++ b/spec/helpers/languages_helper_spec.rb
@@ -10,14 +10,54 @@ describe LanguagesHelper do
   end
 
   describe 'native_locale_name' do
-    it 'finds the human readable native name from a key' do
-      expect(helper.native_locale_name(:de)).to eq('Deutsch')
+    context 'with a blank locale' do
+      it 'defaults to a generic value' do
+        expect(helper.native_locale_name(nil)).to eq(I18n.t('generic.none'))
+      end
+    end
+
+    context 'with a locale of `und`' do
+      it 'defaults to a generic value' do
+        expect(helper.native_locale_name('und')).to eq(I18n.t('generic.none'))
+      end
+    end
+
+    context 'with a supported locale' do
+      it 'finds the human readable native name from a key' do
+        expect(helper.native_locale_name(:de)).to eq('Deutsch')
+      end
+    end
+
+    context 'with a regional locale' do
+      it 'finds the human readable regional name from a key' do
+        expect(helper.native_locale_name('en-GB')).to eq('English (British)')
+      end
+    end
+
+    context 'with a non-existent locale' do
+      it 'returns the supplied locale value' do
+        expect(helper.native_locale_name(:xxx)).to eq(:xxx)
+      end
     end
   end
 
   describe 'standard_locale_name' do
-    it 'finds the human readable standard name from a key' do
-      expect(helper.standard_locale_name(:de)).to eq('German')
+    context 'with a blank locale' do
+      it 'defaults to a generic value' do
+        expect(helper.standard_locale_name(nil)).to eq(I18n.t('generic.none'))
+      end
+    end
+
+    context 'with a non-existent locale' do
+      it 'returns the supplied locale value' do
+        expect(helper.standard_locale_name(:xxx)).to eq(:xxx)
+      end
+    end
+
+    context 'with a supported locale' do
+      it 'finds the human readable standard name from a key' do
+        expect(helper.standard_locale_name(:de)).to eq('German')
+      end
     end
   end
 end
diff --git a/spec/helpers/settings_helper_spec.rb b/spec/helpers/settings_helper_spec.rb
new file mode 100644
index 000000000..cba5c6ee8
--- /dev/null
+++ b/spec/helpers/settings_helper_spec.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe SettingsHelper do
+  describe 'session_device_icon' do
+    context 'with a mobile device' do
+      let(:session) { SessionActivation.new(user_agent: 'Mozilla/5.0 (iPhone)') }
+
+      it 'detects the device and returns a descriptive string' do
+        result = helper.session_device_icon(session)
+
+        expect(result).to eq('mobile')
+      end
+    end
+
+    context 'with a tablet device' do
+      let(:session) { SessionActivation.new(user_agent: 'Mozilla/5.0 (iPad)') }
+
+      it 'detects the device and returns a descriptive string' do
+        result = helper.session_device_icon(session)
+
+        expect(result).to eq('tablet')
+      end
+    end
+
+    context 'with a desktop device' do
+      let(:session) { SessionActivation.new(user_agent: 'Mozilla/5.0 (Macintosh)') }
+
+      it 'detects the device and returns a descriptive string' do
+        result = helper.session_device_icon(session)
+
+        expect(result).to eq('desktop')
+      end
+    end
+  end
+end
diff --git a/spec/helpers/statuses_helper_spec.rb b/spec/helpers/statuses_helper_spec.rb
index cba659bfb..105da7e1b 100644
--- a/spec/helpers/statuses_helper_spec.rb
+++ b/spec/helpers/statuses_helper_spec.rb
@@ -1,6 +1,96 @@
+# frozen_string_literal: true
+
 require 'rails_helper'
 
-RSpec.describe StatusesHelper, type: :helper do
+describe StatusesHelper do
+  describe 'status_text_summary' do
+    context 'with blank text' do
+      let(:status) { Status.new(spoiler_text: '') }
+
+      it 'returns immediately with nil' do
+        result = helper.status_text_summary(status)
+        expect(result).to be_nil
+      end
+    end
+
+    context 'with present text' do
+      let(:status) { Status.new(spoiler_text: 'SPOILERS!!!') }
+
+      it 'returns the content warning' do
+        result = helper.status_text_summary(status)
+        expect(result).to eq(I18n.t('statuses.content_warning', warning: 'SPOILERS!!!'))
+      end
+    end
+  end
+
+  def status_text_summary(status)
+    return if status.spoiler_text.blank?
+
+    I18n.t('statuses.content_warning', warning: status.spoiler_text)
+  end
+
+  describe 'link_to_newer' do
+    it 'returns a link to newer content' do
+      url = 'https://example.com'
+      result = helper.link_to_newer(url)
+
+      expect(result).to match('load-more')
+      expect(result).to match(I18n.t('statuses.show_newer'))
+    end
+  end
+
+  describe 'link_to_older' do
+    it 'returns a link to older content' do
+      url = 'https://example.com'
+      result = helper.link_to_older(url)
+
+      expect(result).to match('load-more')
+      expect(result).to match(I18n.t('statuses.show_older'))
+    end
+  end
+
+  describe 'fa_visibility_icon' do
+    context 'with a status that is public' do
+      let(:status) { Status.new(visibility: 'public') }
+
+      it 'returns the correct fa icon' do
+        result = helper.fa_visibility_icon(status)
+
+        expect(result).to match('fa-globe')
+      end
+    end
+
+    context 'with a status that is unlisted' do
+      let(:status) { Status.new(visibility: 'unlisted') }
+
+      it 'returns the correct fa icon' do
+        result = helper.fa_visibility_icon(status)
+
+        expect(result).to match('fa-unlock')
+      end
+    end
+
+    context 'with a status that is private' do
+      let(:status) { Status.new(visibility: 'private') }
+
+      it 'returns the correct fa icon' do
+        result = helper.fa_visibility_icon(status)
+
+        expect(result).to match('fa-lock')
+      end
+    end
+
+    context 'with a status that is direct' do
+      let(:status) { Status.new(visibility: 'direct') }
+
+      it 'returns the correct fa icon' do
+        result = helper.fa_visibility_icon(status)
+
+        expect(result).to match('fa-at')
+      end
+    end
+  end
+
   describe '#stream_link_target' do
     it 'returns nil if it is not an embedded view' do
       set_not_embedded_view