BigW Consortium Gitlab

note_message_spec.rb 5.81 KB
Newer Older
1 2
require 'spec_helper'

3
describe ChatMessage::NoteMessage, models: true do
Tiago Botelho committed
4
  subject { described_class.new(args) }
5

Tiago Botelho committed
6 7 8 9 10 11 12 13 14 15 16 17
  let(:color) { '#345' }
  let(:args) do
    {
      user: {
        name: 'Test User',
        username: 'test.user',
        avatar_url: 'http://fakeavatar'
      },
      project_name: 'project_name',
      project_url: 'http://somewhere.com',
      repository: {
        name: 'project_name',
18
        url: 'http://somewhere.com'
Tiago Botelho committed
19 20 21 22 23 24 25
      },
      object_attributes: {
        id: 10,
        note: 'comment on a commit',
        url: 'http://url.com',
        noteable_type: 'Commit'
      }
26 27 28 29 30
    }
  end

  context 'commit notes' do
    before do
Tiago Botelho committed
31 32 33 34 35
      args[:object_attributes][:note] = 'comment on a commit'
      args[:object_attributes][:noteable_type] = 'Commit'
      args[:commit] = {
        id: '5f163b2b95e6f53cbd428f5f0b103702a52b9a23',
        message: "Added a commit message\ndetails\n123\n"
36 37 38
      }
    end

Tiago Botelho committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    context 'without markdown' do
      it 'returns a message regarding notes on commits' do
        expect(subject.pretext).to eq("test.user <http://url.com|commented on " \
          "commit 5f163b2b> in <http://somewhere.com|project_name>: " \
          "*Added a commit message*")
        expect(subject.attachments).to eq([{
          text: 'comment on a commit',
          color: color
        }])
      end
    end

    context 'with markdown' do
      before do
        args[:markdown] = true
      end

      it 'returns a message regarding notes on commits' do
        expect(subject.pretext).to eq(
          'test.user [commented on commit 5f163b2b](http://url.com) in [project_name](http://somewhere.com): *Added a commit message*'
        )
        expect(subject.attachments).to eq('comment on a commit')
        expect(subject.activity).to eq({
          title: 'test.user [commented on commit 5f163b2b](http://url.com)',
          subtitle: 'in [project_name](http://somewhere.com)',
          text: 'Added a commit message',
          image: 'http://fakeavatar'
        })
      end
68 69 70 71 72
    end
  end

  context 'merge request notes' do
    before do
Tiago Botelho committed
73 74 75 76 77 78
      args[:object_attributes][:note] = 'comment on a merge request'
      args[:object_attributes][:noteable_type] = 'MergeRequest'
      args[:merge_request] = {
        id: 1,
        iid: 30,
        title: "merge request title\ndetails\n"
79 80
      }
    end
81

Tiago Botelho committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    context 'without markdown' do
      it 'returns a message regarding notes on a merge request' do
        expect(subject.pretext).to eq("test.user <http://url.com|commented on " \
          "merge request !30> in <http://somewhere.com|project_name>: " \
          "*merge request title*")
        expect(subject.attachments).to eq([{
          text: 'comment on a merge request',
          color: color
        }])
      end
    end

    context 'with markdown' do
      before do
        args[:markdown] = true
      end

      it 'returns a message regarding notes on a merge request' do
        expect(subject.pretext).to eq(
          'test.user [commented on merge request !30](http://url.com) in [project_name](http://somewhere.com): *merge request title*')
        expect(subject.attachments).to eq('comment on a merge request')
        expect(subject.activity).to eq({
          title: 'test.user [commented on merge request !30](http://url.com)',
          subtitle: 'in [project_name](http://somewhere.com)',
          text: 'merge request title',
          image: 'http://fakeavatar'
        })
      end
110 111 112 113 114
    end
  end

  context 'issue notes' do
    before do
Tiago Botelho committed
115 116 117 118 119 120
      args[:object_attributes][:note] = 'comment on an issue'
      args[:object_attributes][:noteable_type] = 'Issue'
      args[:issue] = {
        id: 1,
        iid: 20,
        title: "issue title\ndetails\n"
121 122 123
      }
    end

Tiago Botelho committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    context 'without markdown' do
      it 'returns a message regarding notes on an issue' do
        expect(subject.pretext).to eq(
          "test.user <http://url.com|commented on " \
            "issue #20> in <http://somewhere.com|project_name>: " \
            "*issue title*")
        expect(subject.attachments).to eq([{
          text: 'comment on an issue',
          color: color
        }])
      end
    end

    context 'with markdown' do
      before do
        args[:markdown] = true
      end

      it 'returns a message regarding notes on an issue' do
        expect(subject.pretext).to eq(
          'test.user [commented on issue #20](http://url.com) in [project_name](http://somewhere.com): *issue title*')
        expect(subject.attachments).to eq('comment on an issue')
        expect(subject.activity).to eq({
          title: 'test.user [commented on issue #20](http://url.com)',
          subtitle: 'in [project_name](http://somewhere.com)',
          text: 'issue title',
          image: 'http://fakeavatar'
        })
      end
153 154 155 156 157
    end
  end

  context 'project snippet notes' do
    before do
Tiago Botelho committed
158 159 160 161 162
      args[:object_attributes][:note] = 'comment on a snippet'
      args[:object_attributes][:noteable_type] = 'Snippet'
      args[:snippet] = {
        id: 5,
        title: "snippet title\ndetails\n"
163 164 165
      }
    end

Tiago Botelho committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    context 'without markdown' do
      it 'returns a message regarding notes on a project snippet' do
        expect(subject.pretext).to eq("test.user <http://url.com|commented on " \
          "snippet $5> in <http://somewhere.com|project_name>: " \
          "*snippet title*")
        expect(subject.attachments).to eq([{
          text: 'comment on a snippet',
          color: color
        }])
      end
    end

    context 'with markdown' do
      before do
        args[:markdown] = true
      end

      it 'returns a message regarding notes on a project snippet' do
        expect(subject.pretext).to eq(
          'test.user [commented on snippet $5](http://url.com) in [project_name](http://somewhere.com): *snippet title*')
        expect(subject.attachments).to eq('comment on a snippet')
      end
188 189 190
    end
  end
end