1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe Integrations::ChatMessage::IssueMessage do
6  subject { described_class.new(args) }
7
8  let(:args) do
9    {
10      user: {
11        name: 'Test User',
12        username: 'test.user',
13        avatar_url: 'http://someavatar.com'
14      },
15      project_name: 'project_name',
16      project_url: 'http://somewhere.com',
17
18      object_attributes: {
19        title: 'Issue title',
20        id: 10,
21        iid: 100,
22        assignee_id: 1,
23        url: 'http://url.com',
24        action: 'open',
25        state: 'opened',
26        description: 'issue description'
27      }
28    }
29  end
30
31  it_behaves_like Integrations::ChatMessage
32
33  context 'without markdown' do
34    let(:color) { '#C95823' }
35
36    describe '#initialize' do
37      before do
38        args[:object_attributes][:description] = nil
39      end
40
41      it 'returns a non-null description' do
42        expect(subject.description).to eq('')
43      end
44    end
45
46    context 'open' do
47      it 'returns a message regarding opening of issues' do
48        expect(subject.pretext).to eq(
49          '[<http://somewhere.com|project_name>] Issue <http://url.com|#100 Issue title> opened by Test User (test.user)')
50        expect(subject.attachments).to eq([
51          {
52            title: "#100 Issue title",
53            title_link: "http://url.com",
54            text: "issue description",
55            color: color
56          }
57        ])
58      end
59    end
60
61    context 'close' do
62      before do
63        args[:object_attributes][:action] = 'close'
64        args[:object_attributes][:state] = 'closed'
65      end
66
67      it 'returns a message regarding closing of issues' do
68        expect(subject.pretext). to eq(
69          '[<http://somewhere.com|project_name>] Issue <http://url.com|#100 Issue title> closed by Test User (test.user)')
70        expect(subject.attachments).to be_empty
71      end
72    end
73
74    context 'reopen' do
75      before do
76        args[:object_attributes][:action] = 'reopen'
77        args[:object_attributes][:state] = 'opened'
78      end
79
80      it 'returns a message regarding reopening of issues' do
81        expect(subject.pretext)
82          .to eq('[<http://somewhere.com|project_name>] Issue <http://url.com|#100 Issue title> opened by Test User (test.user)')
83        expect(subject.attachments).to be_empty
84      end
85    end
86  end
87
88  context 'with markdown' do
89    before do
90      args[:markdown] = true
91    end
92
93    context 'open' do
94      it 'returns a message regarding opening of issues' do
95        expect(subject.pretext).to eq(
96          '[[project_name](http://somewhere.com)] Issue [#100 Issue title](http://url.com) opened by Test User (test.user)')
97        expect(subject.attachments).to eq('issue description')
98        expect(subject.activity).to eq({
99          title: 'Issue opened by Test User (test.user)',
100          subtitle: 'in [project_name](http://somewhere.com)',
101          text: '[#100 Issue title](http://url.com)',
102          image: 'http://someavatar.com'
103        })
104      end
105    end
106
107    context 'close' do
108      before do
109        args[:object_attributes][:action] = 'close'
110        args[:object_attributes][:state] = 'closed'
111      end
112
113      it 'returns a message regarding closing of issues' do
114        expect(subject.pretext). to eq(
115          '[[project_name](http://somewhere.com)] Issue [#100 Issue title](http://url.com) closed by Test User (test.user)')
116        expect(subject.attachments).to be_empty
117        expect(subject.activity).to eq({
118          title: 'Issue closed by Test User (test.user)',
119          subtitle: 'in [project_name](http://somewhere.com)',
120          text: '[#100 Issue title](http://url.com)',
121          image: 'http://someavatar.com'
122        })
123      end
124    end
125  end
126end
127