custom_emoji_spec.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe CustomEmoji, :attachment_processing do
  4. describe '#search' do
  5. subject { described_class.search(search_term) }
  6. let(:custom_emoji) { Fabricate(:custom_emoji, shortcode: shortcode) }
  7. context 'when shortcode is exact' do
  8. let(:shortcode) { 'blobpats' }
  9. let(:search_term) { 'blobpats' }
  10. it 'finds emoji' do
  11. expect(subject).to include(custom_emoji)
  12. end
  13. end
  14. context 'when shortcode is partial' do
  15. let(:shortcode) { 'blobpats' }
  16. let(:search_term) { 'blob' }
  17. it 'finds emoji' do
  18. expect(subject).to include(custom_emoji)
  19. end
  20. end
  21. end
  22. describe '#local?' do
  23. subject { custom_emoji.local? }
  24. let(:custom_emoji) { Fabricate(:custom_emoji, domain: domain) }
  25. context 'when domain is nil' do
  26. let(:domain) { nil }
  27. it 'returns true' do
  28. expect(subject).to be true
  29. end
  30. end
  31. context 'when domain is present' do
  32. let(:domain) { 'example.com' }
  33. it 'returns false' do
  34. expect(subject).to be false
  35. end
  36. end
  37. end
  38. describe '#object_type' do
  39. it 'returns :emoji' do
  40. custom_emoji = Fabricate(:custom_emoji)
  41. expect(custom_emoji.object_type).to be :emoji
  42. end
  43. end
  44. describe '.from_text' do
  45. subject { described_class.from_text(text, nil) }
  46. let!(:emojo) { Fabricate(:custom_emoji, shortcode: 'coolcat') }
  47. context 'with plain text' do
  48. let(:text) { 'Hello :coolcat:' }
  49. it 'returns records used via shortcodes in text' do
  50. expect(subject).to include(emojo)
  51. end
  52. end
  53. context 'with html' do
  54. let(:text) { '<p>Hello :coolcat:</p>' }
  55. it 'returns records used via shortcodes in text' do
  56. expect(subject).to include(emojo)
  57. end
  58. end
  59. end
  60. describe 'Normalizations' do
  61. describe 'domain' do
  62. it { is_expected.to normalize(:domain).from('wWw.MaStOdOn.CoM').to('www.mastodon.com') }
  63. it { is_expected.to normalize(:domain).from(nil).to(nil) }
  64. end
  65. end
  66. describe 'Validations' do
  67. subject { Fabricate.build :custom_emoji }
  68. it { is_expected.to validate_uniqueness_of(:shortcode).scoped_to(:domain) }
  69. it { is_expected.to validate_length_of(:shortcode).is_at_least(described_class::MINIMUM_SHORTCODE_SIZE) }
  70. it { is_expected.to allow_values('cats').for(:shortcode) }
  71. it { is_expected.to_not allow_values('@#$@#$', 'X').for(:shortcode) }
  72. end
  73. end