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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# frozen_string_literal: true
RSpec.shared_examples 'issuable update service' do
def update_issuable(opts)
described_class.new(project, user, opts).execute(open_issuable)
end
describe 'changing state' do
let(:hook_event) { :"#{closed_issuable.class.name.underscore.to_sym}_hooks" }
describe 'to reopened' do
let(:expected_payload) do
include(
changes: include(
state_id: { current: 1, previous: 2 },
updated_at: { current: kind_of(Time), previous: kind_of(Time) }
),
object_attributes: include(
state: 'opened',
action: 'reopen'
)
)
end
it 'executes hooks' do
hooks_container = described_class < Issues::BaseService ? project.project_namespace : project
expect(hooks_container).to receive(:execute_hooks).with(expected_payload, hook_event)
expect(hooks_container).to receive(:execute_integrations).with(expected_payload, hook_event)
described_class.new(
**described_class.constructor_container_arg(project),
current_user: user,
params: { state_event: 'reopen' }
).execute(closed_issuable)
end
end
describe 'to closed' do
let(:expected_payload) do
include(
changes: include(
state_id: { current: 2, previous: 1 },
updated_at: { current: kind_of(Time), previous: kind_of(Time) }
),
object_attributes: include(
state: 'closed',
action: 'close'
)
)
end
it 'executes hooks' do
hooks_container = described_class < Issues::BaseService ? project.project_namespace : project
expect(hooks_container).to receive(:execute_hooks).with(expected_payload, hook_event)
expect(hooks_container).to receive(:execute_integrations).with(expected_payload, hook_event)
described_class.new(
**described_class.constructor_container_arg(project),
current_user: user,
params: { state_event: 'close' }
).execute(open_issuable)
end
end
end
end
RSpec.shared_examples 'updating issuable labels' do
context 'when add_label_ids and label_ids are passed' do
let(:params) { { label_ids: [label_a.id], add_label_ids: [label_c.id] } }
it 'replaces the labels with the ones in label_ids and adds those in add_label_ids' do
issuable.update!(labels: [label_b])
update_issuable(params)
expect(issuable.label_ids).to contain_exactly(label_a.id, label_c.id)
end
end
context 'when remove_label_ids and label_ids are passed' do
let(:params) { { label_ids: [label_a.id, label_b.id, label_c.id], remove_label_ids: [label_a.id] } }
it 'replaces the labels with the ones in label_ids and removes those in remove_label_ids' do
issuable.update!(labels: [label_a, label_c])
update_issuable(params)
expect(issuable.label_ids).to contain_exactly(label_b.id, label_c.id)
end
end
context 'when add_label_ids and remove_label_ids are passed' do
let(:params) { { add_label_ids: [label_c.id], remove_label_ids: [label_a.id] } }
before do
issuable.update!(labels: [label_a])
update_issuable(params)
end
it 'adds the passed labels' do
expect(issuable.label_ids).to include(label_c.id)
end
it 'removes the passed labels' do
expect(issuable.label_ids).not_to include(label_a.id)
end
end
context 'when same id is passed as add_label_ids and remove_label_ids' do
let(:params) { { add_label_ids: [label_a.id], remove_label_ids: [label_a.id] } }
context 'for a label assigned to an issue' do
it 'removes the label' do
issuable.update!(labels: [label_a])
update_issuable(params)
expect(issuable.label_ids).to be_empty
end
end
context 'for a label not assigned to an issue' do
it 'does not add the label' do
expect(issuable.label_ids).to be_empty
end
end
end
context 'when duplicate label titles are given' do
let(:params) { { labels: [label_c.title, label_c.title] } }
it 'assigns the label once' do
update_issuable(params)
expect(issuable.labels).to contain_exactly(label_c)
end
end
context 'when remove_label_ids contains a locked label' do
let(:params) { { remove_label_ids: [label_locked.id] } }
it 'removes locked labels for non-merged issuables' do
issuable.update!(labels: [label_a, label_locked])
update_issuable(params)
expect(issuable.label_ids).to contain_exactly(label_a.id)
end
end
end
RSpec.shared_examples 'keeps issuable labels sorted after update' do
before do
update_issuable(label_ids: [label_b.id])
end
context 'when label is changed' do
it 'keeps the labels sorted by title ASC' do
update_issuable({ add_label_ids: [label_a.id] })
expect(issuable.labels).to eq([label_a, label_b])
end
end
end
RSpec.shared_examples 'broadcasting issuable labels updates' do
before do
update_issuable(label_ids: [label_a.id])
end
context 'when label is added' do
it 'triggers the GraphQL subscription' do
expect(GraphqlTriggers).to receive(:issuable_labels_updated).with(issuable)
update_issuable(add_label_ids: [label_b.id])
end
end
context 'when label is removed' do
it 'triggers the GraphQL subscription' do
expect(GraphqlTriggers).to receive(:issuable_labels_updated).with(issuable)
update_issuable(remove_label_ids: [label_a.id])
end
end
context 'when label is unchanged' do
it 'does not trigger the GraphQL subscription' do
expect(GraphqlTriggers).not_to receive(:issuable_labels_updated).with(issuable)
update_issuable(label_ids: [label_a.id])
end
end
end
RSpec.shared_examples_for 'issuable update service updating last_edited_at values' do
context 'when updating the title of the issuable' do
let(:update_params) { { title: 'updated title' } }
it 'does not update last_edited values' do
expect { update_issuable }.to change { issuable.title }.from(issuable.title).to('updated title').and(
not_change(issuable, :last_edited_at)
).and(
not_change(issuable, :last_edited_by)
)
end
end
context 'when updating the description of the issuable' do
let(:update_params) { { description: 'updated description' } }
it 'updates last_edited values' do
expect do
update_issuable
end.to change { issuable.description }.from(issuable.description).to('updated description').and(
change { issuable.last_edited_at }
).and(
change { issuable.last_edited_by }
)
end
end
end
|