summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/services/boards/lists_move_service.rb
blob: 0b3bfd8e2a839e795d5336ac3974b7542c1d41f8 (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
# frozen_string_literal: true

shared_examples 'lists move service' do
  let!(:planning)    { create(:list, board: board, position: 0) }
  let!(:development) { create(:list, board: board, position: 1) }
  let!(:review)      { create(:list, board: board, position: 2) }
  let!(:staging)     { create(:list, board: board, position: 3) }
  let!(:closed)      { create(:closed_list, board: board) }

  context 'when list type is set to label' do
    it 'keeps position of lists when new position is nil' do
      service = described_class.new(parent, user, position: nil)

      service.execute(planning)

      expect(current_list_positions).to eq [0, 1, 2, 3]
    end

    it 'keeps position of lists when new position is equal to old position' do
      service = described_class.new(parent, user, position: planning.position)

      service.execute(planning)

      expect(current_list_positions).to eq [0, 1, 2, 3]
    end

    it 'keeps position of lists when new position is negative' do
      service = described_class.new(parent, user, position: -1)

      service.execute(planning)

      expect(current_list_positions).to eq [0, 1, 2, 3]
    end

    it 'keeps position of lists when new position is equal to number of labels lists' do
      service = described_class.new(parent, user, position: board.lists.label.size)

      service.execute(planning)

      expect(current_list_positions).to eq [0, 1, 2, 3]
    end

    it 'keeps position of lists when new position is greater than number of labels lists' do
      service = described_class.new(parent, user, position: board.lists.label.size + 1)

      service.execute(planning)

      expect(current_list_positions).to eq [0, 1, 2, 3]
    end

    it 'increments position of intermediate lists when new position is equal to first position' do
      service = described_class.new(parent, user, position: 0)

      service.execute(staging)

      expect(current_list_positions).to eq [1, 2, 3, 0]
    end

    it 'decrements position of intermediate lists when new position is equal to last position' do
      service = described_class.new(parent, user, position: board.lists.label.last.position)

      service.execute(planning)

      expect(current_list_positions).to eq [3, 0, 1, 2]
    end

    it 'decrements position of intermediate lists when new position is greater than old position' do
      service = described_class.new(parent, user, position: 2)

      service.execute(planning)

      expect(current_list_positions).to eq [2, 0, 1, 3]
    end

    it 'increments position of intermediate lists when new position is lower than old position' do
      service = described_class.new(parent, user, position: 1)

      service.execute(staging)

      expect(current_list_positions).to eq [0, 2, 3, 1]
    end
  end

  it 'keeps position of lists when list type is closed' do
    service = described_class.new(parent, user, position: 2)

    service.execute(closed)

    expect(current_list_positions).to eq [0, 1, 2, 3]
  end

  def current_list_positions
    [planning, development, review, staging].map { |list| list.reload.position }
  end
end