mentions_container.rb 2.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
shared_examples_for "it is mentions container" do
  let(:people) { [alice, bob, eve].map(&:person) }
  let(:test_string) {
    "@{Raphael; #{people[0].diaspora_handle}} can mention people like @{Ilya; #{people[1].diaspora_handle}}"\
    "can mention people like @{Daniel; #{people[2].diaspora_handle}}"
  }
  let(:target) { FactoryGirl.build(described_class.to_s.underscore.to_sym, text: test_string, author: alice.person) }
  let(:target_persisted) {
    target.save!
    target
  }

13 14 15 16 17 18 19 20
  describe ".before_create" do
    it "backports mention syntax to old syntax" do
      text = "mention @{#{people[0].diaspora_handle}} text"
      expected_text = "mention @{#{people[0].name}; #{people[0].diaspora_handle}} text"
      obj = FactoryGirl.build(described_class.to_s.underscore.to_sym, text: text, author: alice.person)
      obj.save
      expect(obj.text).to eq(expected_text)
    end
21 22 23 24 25 26 27

    it "doesn't backport mention syntax if author is not local" do
      text = "mention @{#{people[0].diaspora_handle}} text"
      obj = FactoryGirl.build(described_class.to_s.underscore.to_sym, text: text, author: remote_raphael)
      obj.save
      expect(obj.text).to eq(text)
    end
28 29
  end

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  describe ".after_create" do
    it "calls create_mentions" do
      expect(target).to receive(:create_mentions).and_call_original
      target.save
    end
  end

  describe "#create_mentions" do
    it "creates a mention for everyone mentioned in the message" do
      people.each do |person|
        expect(target.mentions).to receive(:find_or_create_by).with(person_id: person.id)
      end
      target.create_mentions
    end

    it "does not barf if it gets called twice" do
      expect {
        target_persisted.create_mentions
      }.to_not raise_error
    end
  end

  describe "#mentioned_people" do
    it "returns the mentioned people if non-persisted" do
      expect(target.mentioned_people).to match_array(people)
    end

    it "returns the mentioned people if persisted" do
      expect(target_persisted.mentioned_people).to match_array(people)
    end
  end
end