summaryrefslogtreecommitdiff
path: root/spec/services/design_management/move_designs_service_spec.rb
blob: 519378a8dd47db911f7a1f1f7760908c6812aa50 (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe DesignManagement::MoveDesignsService do
  include DesignManagementTestHelpers

  let_it_be(:issue) { create(:issue) }
  let_it_be(:developer) { create(:user, developer_projects: [issue.project]) }
  let_it_be(:designs) { create_list(:design, 3, :with_relative_position, issue: issue) }

  let(:project) { issue.project }

  let(:service) { described_class.new(current_user, params) }

  let(:params) do
    {
      current_design: current_design,
      previous_design: previous_design,
      next_design: next_design
    }
  end

  let(:current_user) { developer }
  let(:current_design) { nil }
  let(:previous_design) { nil }
  let(:next_design) { nil }

  before do
    enable_design_management
  end

  describe '#execute' do
    subject { service.execute }

    context 'the user cannot move designs' do
      let(:current_design) { designs.first }
      let(:current_user) { build_stubbed(:user) }

      it 'raises cannot_move' do
        expect(subject).to be_error.and(have_attributes(message: :cannot_move))
      end
    end

    context 'the designs are not distinct' do
      let(:current_design) { designs.first }
      let(:previous_design) { designs.first }

      it 'raises not_distinct' do
        expect(subject).to be_error.and(have_attributes(message: :not_distinct))
      end
    end

    context 'the designs are not on the same issue' do
      let(:current_design) { designs.first }
      let(:previous_design) { create(:design) }

      it 'raises not_same_issue' do
        expect(subject).to be_error.and(have_attributes(message: :not_same_issue))
      end
    end

    context 'no focus is passed' do
      let(:previous_design) { designs.second }
      let(:next_design) { designs.third }

      it 'raises no_focus' do
        expect(subject).to be_error.and(have_attributes(message: :no_focus))
      end
    end

    context 'no neighbours are passed' do
      let(:current_design) { designs.first }

      it 'raises no_neighbors' do
        expect(subject).to be_error.and(have_attributes(message: :no_neighbors))
      end
    end

    context 'moving a design with neighbours' do
      let(:current_design) { designs.first }
      let(:previous_design) { designs.second }
      let(:next_design) { designs.third }

      it 'repositions existing designs and correctly places the given design' do
        other_design1 = create(:design, issue: issue, relative_position: 10)
        other_design2 = create(:design, issue: issue, relative_position: 20)
        other_design3, other_design4 = create_list(:design, 2, issue: issue)

        expect(subject).to be_success

        expect(issue.designs.ordered).to eq(
          [
            # Existing designs which already had a relative_position set.
            # These should stay at the beginning, in the same order.
            other_design1,
            other_design2,

            # The designs we're passing into the service.
            # These should be placed between the existing designs, in the correct order.
            previous_design,
            current_design,
            next_design,

            # Existing designs which didn't have a relative_position set.
            # These should be placed at the end, in the order of their IDs.
            other_design3,
            other_design4
          ])
      end
    end
  end
end