summaryrefslogtreecommitdiff
path: root/spec/services/boards/lists/create_service_spec.rb
blob: 90764b86b16edf2ee5fc1d9dbea3a7f92d21caa3 (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
require 'spec_helper'

describe Boards::Lists::CreateService, services: true do
  describe '#execute' do
    let(:project) { create(:project_with_board) }
    let(:board)   { project.board }
    let(:user)    { create(:user) }
    let(:label)   { create(:label, project: project, name: 'in-progress') }

    subject(:service) { described_class.new(project, user, label_id: label.id) }

    context 'when board lists is empty' do
      it 'creates a new list at beginning of the list' do
        list = service.execute

        expect(list.position).to eq 0
      end
    end

    context 'when board lists has only a backlog list' do
      it 'creates a new list at beginning of the list' do
        create(:backlog_list, board: board)

        list = service.execute

        expect(list.position).to eq 0
      end
    end

    context 'when board lists has only labels lists' do
      it 'creates a new list at end of the lists' do
        create(:list, board: board, position: 0)
        create(:list, board: board, position: 1)

        list = service.execute

        expect(list.position).to eq 2
      end
    end

    context 'when board lists has backlog, label and done lists' do
      it 'creates a new list at end of the label lists' do
        create(:backlog_list, board: board)
        create(:done_list, board: board)
        list1 = create(:list, board: board, position: 0)

        list2 = service.execute

        expect(list1.reload.position).to eq 0
        expect(list2.reload.position).to eq 1
      end
    end

    context 'when provided label does not belongs to the project' do
      it 'raises an error' do
        label = create(:label, name: 'in-development')
        service = described_class.new(project, user, label_id: label.id)

        expect { service.execute }.to raise_error(ActiveRecord::RecordNotFound)
      end
    end
  end
end