summaryrefslogtreecommitdiff
path: root/spec/services/note_summary_spec.rb
blob: 038e0cdb703844331d6ee96c10e060ff4a8ca4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
62
# frozen_string_literal: true

require 'spec_helper'

describe NoteSummary do
  let(:project)  { build(:project) }
  let(:noteable) { build(:issue) }
  let(:user)     { build(:user) }

  def create_note_summary
    described_class.new(noteable, project, user, 'note', action: 'icon', commit_count: 5)
  end

  describe '#metadata?' do
    it 'returns true when metadata present' do
      expect(create_note_summary.metadata?).to be_truthy
    end

    it 'returns false when metadata not present' do
      expect(described_class.new(noteable, project, user, 'note').metadata?).to be_falsey
    end
  end

  describe '#note' do
    it 'returns note hash' do
      Timecop.freeze do
        expect(create_note_summary.note).to eq(noteable: noteable, project: project, author: user, note: 'note',
                                               created_at: Time.current)
      end
    end

    context 'when noteable is a commit' do
      let(:noteable) { build(:commit, system_note_timestamp: Time.zone.at(43)) }

      it 'returns note hash specific to commit' do
        expect(create_note_summary.note).to eq(
          noteable: nil, project: project, author: user, note: 'note',
          noteable_type: 'Commit', commit_id: noteable.id,
          created_at: Time.zone.at(43)
        )
      end
    end
  end

  describe '#metadata' do
    it 'returns metadata hash' do
      expect(create_note_summary.metadata).to eq(action: 'icon', commit_count: 5)
    end

    context 'description action and noteable has saved_description_version' do
      before do
        noteable.saved_description_version = 1
      end

      subject { described_class.new(noteable, project, user, 'note', action: 'description') }

      it 'sets the description_version metadata' do
        expect(subject.metadata).to include(description_version: 1)
      end
    end
  end
end