summaryrefslogtreecommitdiff
path: root/spec/services/boards/issues/move_service_spec.rb
blob: 0122159cab8066218a47a52673679a7401ee52bc (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
require 'spec_helper'

describe Boards::Issues::MoveService, services: true do
  describe '#execute' do
    let(:user)    { create(:user) }
    let(:project) { create(:project_with_board) }
    let(:board)   { project.board }

    let(:bug) { create(:label, project: project, name: 'Bug') }
    let(:development) { create(:label, project: project, name: 'Development') }
    let(:testing)  { create(:label, project: project, name: 'Testing') }

    let!(:backlog) { create(:backlog_list, board: board) }
    let!(:list1)   { create(:list, board: board, label: development, position: 0) }
    let!(:list2)   { create(:list, board: board, label: testing, position: 1) }
    let!(:done)    { create(:done_list, board: board) }

    before do
      project.team << [user, :developer]
    end

    context 'when moving from backlog' do
      it 'adds the label of the list it goes to' do
        issue = create(:labeled_issue, project: project, labels: [bug])
        params = { from_list_id: backlog.id, to_list_id: list1.id }

        described_class.new(project, user, params).execute(issue)

        expect(issue.reload.labels).to contain_exactly(bug, development)
      end
    end

    context 'when moving to backlog' do
      it 'removes all list-labels' do
        issue = create(:labeled_issue, project: project, labels: [bug, development, testing])
        params = { from_list_id: list1.id, to_list_id: backlog.id }

        described_class.new(project, user, params).execute(issue)

        expect(issue.reload.labels).to contain_exactly(bug)
      end
    end

    context 'when moving from backlog to done' do
      it 'closes the issue' do
        issue = create(:labeled_issue, project: project, labels: [bug])
        params = { from_list_id: backlog.id, to_list_id: done.id }

        described_class.new(project, user, params).execute(issue)
        issue.reload

        expect(issue.labels).to contain_exactly(bug)
        expect(issue).to be_closed
      end
    end

    context 'when moving an issue between lists' do
      let(:issue)  { create(:labeled_issue, project: project, labels: [bug, development]) }
      let(:params) { { from_list_id: list1.id, to_list_id: list2.id } }

      it 'delegates the label changes to Issues::UpdateService' do
        expect_any_instance_of(Issues::UpdateService).to receive(:execute).with(issue).once

        described_class.new(project, user, params).execute(issue)
      end

      it 'removes the label from the list it came from and adds the label of the list it goes to' do
        described_class.new(project, user, params).execute(issue)

        expect(issue.reload.labels).to contain_exactly(bug, testing)
      end
    end

    context 'when moving to done' do
      let(:issue)  { create(:labeled_issue, project: project, labels: [bug, development, testing]) }
      let(:params) { { from_list_id: list2.id, to_list_id: done.id } }

      it 'delegates the close proceedings to Issues::CloseService' do
        expect_any_instance_of(Issues::CloseService).to receive(:execute).with(issue).once

        described_class.new(project, user, params).execute(issue)
      end

      it 'removes all list-labels and close the issue' do
        described_class.new(project, user, params).execute(issue)
        issue.reload

        expect(issue.labels).to contain_exactly(bug)
        expect(issue).to be_closed
      end
    end

    context 'when moving from done' do
      let(:issue)  { create(:labeled_issue, :closed, project: project, labels: [bug]) }
      let(:params) { { from_list_id: done.id, to_list_id: list2.id } }

      it 'delegates the re-open proceedings to Issues::ReopenService' do
        expect_any_instance_of(Issues::ReopenService).to receive(:execute).with(issue).once

        described_class.new(project, user, params).execute(issue)
      end

      it 'adds the label of the list it goes to and reopen the issue' do
        described_class.new(project, user, params).execute(issue)
        issue.reload

        expect(issue.labels).to contain_exactly(bug, testing)
        expect(issue).to be_reopened
      end
    end

    context 'when moving from done to backlog' do
      it 'reopens the issue' do
        issue = create(:labeled_issue, :closed, project: project, labels: [bug])
        params = { from_list_id: done.id, to_list_id: backlog.id }

        described_class.new(project, user, params).execute(issue)
        issue.reload

        expect(issue.labels).to contain_exactly(bug)
        expect(issue).to be_reopened
      end
    end

    context 'when moving to same list' do
      let(:issue)  { create(:labeled_issue, project: project, labels: [bug, development]) }
      let(:params) { { from_list_id: list1.id, to_list_id: list1.id } }

      it 'returns false' do
        expect(described_class.new(project, user, params).execute(issue)).to eq false
      end

      it 'keeps issues labels' do
        described_class.new(project, user, params).execute(issue)

        expect(issue.reload.labels).to contain_exactly(bug, development)
      end
    end
  end
end