summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/services/boards/issues_move_service.rb
blob: 737863ea41126cddd6881b0475449ae492896dbf (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
shared_examples 'issues move service' do |group|
  context 'when moving an issue between lists' do
    let(:issue)  { create(:labeled_issue, project: project, labels: [bug, development]) }
    let(:params) { { board_id: board1.id, 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(parent, 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(parent, user, params).execute(issue)

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

  context 'when moving to closed' do
    let!(:list3) { create(:list, board: board2, label: regression, position: 1) }

    let(:issue)  { create(:labeled_issue, project: project, labels: [bug, development, testing, regression]) }
    let(:params) { { board_id: board1.id, from_list_id: list2.id, to_list_id: closed.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(parent, user, params).execute(issue)
    end

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

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

  context 'when moving from closed' do
    let(:issue)  { create(:labeled_issue, :closed, project: project, labels: [bug]) }
    let(:params) { { board_id: board1.id, from_list_id: closed.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(parent, user, params).execute(issue)
    end

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

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

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

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

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

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

    it 'sorts issues' do
      [issue, issue1, issue2].each do |issue|
        issue.move_to_end && issue.save!
      end

      params.merge!(move_after_id: issue1.id, move_before_id: issue2.id)

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

      expect(issue.relative_position).to be_between(issue1.relative_position, issue2.relative_position)
    end

    if group
      context 'when on a group board' do
        it 'sends the board_group_id parameter' do
          params.merge!(move_after_id: issue1.id, move_before_id: issue2.id)

          match_params = { move_between_ids: [issue1.id, issue2.id], board_group_id: parent.id }
          expect(Issues::UpdateService).to receive(:new).with(issue.project, user, match_params).and_return(double(execute: build(:issue)))

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