Welcome to mirror list, hosted at ThFree Co, Russian Federation.

event_presenter_spec.rb « presenters « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3f34c96ad8e4239cfb5df46a7ef826d760543152 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe EventPresenter do
  include Gitlab::Routing.url_helpers

  let_it_be(:group) { create(:group) }
  let_it_be(:project) { create(:project, group: group) }
  let_it_be(:target) { create(:milestone, project: project) }
  let_it_be(:group_event) { create(:event, :created, project: nil, group: group, target: target) }
  let_it_be(:project_event) { create(:event, :created, project: project, target: target) }

  describe '#resource_parent_name' do
    context 'with group event' do
      subject { group_event.present.resource_parent_name }

      it { is_expected.to eq(group.full_name) }
    end

    context 'with project label' do
      subject { project_event.present.resource_parent_name }

      it { is_expected.to eq(project.full_name) }
    end
  end

  describe '#target_link_options' do
    context 'with group event' do
      subject { group_event.present.target_link_options }

      it { is_expected.to eq([group, target]) }
    end

    context 'with project label' do
      subject { project_event.present.target_link_options }

      it { is_expected.to eq([project, target]) }
    end
  end

  describe '#target_type_name' do
    it 'returns design for a design event' do
      expect(build(:design_event).present).to have_attributes(target_type_name: 'design')
    end

    it 'returns project for a project event' do
      expect(build(:project_created_event).present).to have_attributes(target_type_name: 'project')
    end

    it 'returns milestone for a milestone event' do
      expect(group_event.present).to have_attributes(target_type_name: 'milestone')
    end

    it 'returns the issue_type for issue events' do
      expect(build(:event, :for_issue, :created).present).to have_attributes(target_type_name: 'issue')
    end

    it 'returns the issue_type for work item events' do
      expect(build(:event, :for_work_item, :created).present).to have_attributes(target_type_name: 'task')
    end
  end

  describe '#note_target_type_name' do
    it 'returns design for an event on a comment on a design' do
      expect(build(:event, :commented, :for_design).present)
        .to have_attributes(note_target_type_name: 'design')
    end

    it 'returns nil for an event without a target' do
      expect(build(:event).present).to have_attributes(note_target_type_name: be_nil)
    end

    it 'returns issue for an issue comment event' do
      expect(build(:event, :commented, target: build(:note_on_issue)).present)
        .to have_attributes(note_target_type_name: 'issue')
    end
  end

  describe '#push_activity_description' do
    subject { event.present.push_activity_description }

    context 'when event is a regular event' do
      let(:event) { build(:event, project: project) }

      it { is_expected.to be_nil }
    end

    context 'when event is a push event' do
      let!(:push_event_payload) { build(:push_event_payload, event: event, ref_count: ref_count) }
      let(:event) { build(:push_event, project: project) }

      context 'when it is an individual event' do
        let(:ref_count) { nil }

        it { is_expected.to eq 'pushed to branch' }
      end

      context 'when it is a batch event' do
        let(:ref_count) { 1 }

        it { is_expected.to eq 'pushed to 1 branch' }
      end
    end
  end

  describe '#batch_push?' do
    subject { event.present.batch_push? }

    context 'when event is a regular event' do
      let(:event) { build(:event, project: project) }

      it { is_expected.to be_falsey }
    end

    context 'when event is a push event' do
      let!(:push_event_payload) { build(:push_event_payload, event: event, ref_count: ref_count) }
      let(:event) { build(:push_event, project: project) }

      context 'when it is an individual event' do
        let(:ref_count) { nil }

        it { is_expected.to be_falsey }
      end

      context 'when it is a batch event' do
        let(:ref_count) { 1 }

        it { is_expected.to be_truthy }
      end
    end
  end

  describe '#linked_to_reference?' do
    subject { event.present.linked_to_reference? }

    context 'when event is a regular event' do
      let(:event) { build(:event, project: project) }

      it { is_expected.to be_falsey }
    end

    context 'when event is a push event' do
      let!(:push_event_payload) { build(:push_event_payload, event: event, ref: ref, ref_type: ref_type) }
      let(:ref) { 'master' }
      let(:ref_type) { :branch }

      context 'when event belongs to group' do
        let(:event) { build(:push_event, group: group) }

        it { is_expected.to be_falsey }
      end

      context 'when event belongs to project' do
        let(:event) { build(:push_event, project: project) }

        it { is_expected.to be_falsey }

        context 'when matching tag exists' do
          let(:ref_type) { :tag }

          before do
            allow(project.repository).to receive(:tag_exists?).with(ref).and_return(true)
          end

          it { is_expected.to be_truthy }
        end

        context 'when matching branch exists' do
          before do
            allow(project.repository).to receive(:branch_exists?).with(ref).and_return(true)
          end

          it { is_expected.to be_truthy }
        end
      end
    end
  end
end