emoji_formatter_spec.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe EmojiFormatter do
  4. let!(:emoji) { Fabricate(:custom_emoji, shortcode: 'coolcat') }
  5. def preformat_text(str)
  6. TextFormatter.new(str).to_s
  7. end
  8. describe '#to_s' do
  9. subject { described_class.new(text, emojis).to_s }
  10. let(:emojis) { [emoji] }
  11. context 'when given text that is not marked as html-safe' do
  12. let(:text) { 'Foo' }
  13. it 'raises an argument error' do
  14. expect { subject }.to raise_error ArgumentError
  15. end
  16. end
  17. context 'when given text with an emoji shortcode at the start' do
  18. let(:text) { preformat_text(':coolcat: Beep boop') }
  19. it 'converts the shortcode to an image tag' do
  20. expect(subject).to match(/<img rel="emoji" draggable="false" width="16" height="16" class="emojione custom-emoji" alt=":coolcat:"/)
  21. end
  22. end
  23. context 'when given text with an emoji shortcode in the middle' do
  24. let(:text) { preformat_text('Beep :coolcat: boop') }
  25. it 'converts the shortcode to an image tag' do
  26. expect(subject).to match(/Beep <img rel="emoji" draggable="false" width="16" height="16" class="emojione custom-emoji" alt=":coolcat:"/)
  27. end
  28. end
  29. context 'when given text with concatenated emoji shortcodes' do
  30. let(:text) { preformat_text(':coolcat::coolcat:') }
  31. it 'does not touch the shortcodes' do
  32. expect(subject).to match(/:coolcat::coolcat:/)
  33. end
  34. end
  35. context 'when given text with an emoji shortcode at the end' do
  36. let(:text) { preformat_text('Beep boop :coolcat:') }
  37. it 'converts the shortcode to an image tag' do
  38. expect(subject).to match(/boop <img rel="emoji" draggable="false" width="16" height="16" class="emojione custom-emoji" alt=":coolcat:"/)
  39. end
  40. end
  41. end
  42. end