Welcome to mirror list, hosted at ThFree Co, Russian Federation.

task_list_reference_replacement_service_spec.rb « work_items « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e7914eb4a92f54e237afcd36b2dea6d8947bbe71 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WorkItems::TaskListReferenceReplacementService do
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:single_line_work_item, refind: true) { create(:work_item, project: project, description: '- [ ] single line', lock_version: 3) }
  let_it_be(:multiple_line_work_item, refind: true) { create(:work_item, project: project, description: "Any text\n\n* [ ] Item to be converted\n    second line\n    third line", lock_version: 3) }

  let(:line_number_start) { 3 }
  let(:line_number_end) { 5 }
  let(:title) { 'work item title' }
  let(:reference) { 'any reference' }
  let(:work_item) { multiple_line_work_item }
  let(:lock_version) { 3 }
  let(:expected_additional_text) { '' }

  shared_examples 'successful work item task reference replacement service' do
    it { is_expected.to be_success }

    it 'replaces the original issue markdown description with new work item reference' do
      result

      expect(work_item.description).to eq("#{expected_additional_text}#{task_prefix} #{reference}+")
    end
  end

  shared_examples 'failing work item task reference replacement service' do |error_message|
    it { is_expected.to be_error }

    it 'returns an error message' do
      expect(result.errors).to contain_exactly(error_message)
    end
  end

  describe '#execute' do
    subject(:result) do
      described_class.new(
        work_item: work_item,
        work_item_reference: reference,
        line_number_start: line_number_start,
        line_number_end: line_number_end,
        title: title,
        lock_version: lock_version
      ).execute
    end

    context 'when task mardown spans a single line' do
      let(:line_number_start) { 1 }
      let(:line_number_end) { 1 }
      let(:work_item) { single_line_work_item }
      let(:task_prefix) { '- [ ]' }

      it_behaves_like 'successful work item task reference replacement service'
    end

    context 'when task mardown spans multiple lines' do
      let(:task_prefix) { '* [ ]' }
      let(:expected_additional_text) { "Any text\n\n" }

      it_behaves_like 'successful work item task reference replacement service'
    end

    context 'when description does not contain a task' do
      let_it_be(:no_matching_work_item) { create(:work_item, project: project, description: 'no matching task') }

      let(:work_item) { no_matching_work_item }

      it_behaves_like 'failing work item task reference replacement service', 'Unable to detect a task on line 3'
    end

    context 'when description is empty' do
      let_it_be(:empty_work_item) { create(:work_item, project: project, description: '') }

      let(:work_item) { empty_work_item }

      it_behaves_like 'failing work item task reference replacement service', "Work item description can't be blank"
    end

    context 'when line_number_start is lower than 1' do
      let(:line_number_start) { 0 }

      it_behaves_like 'failing work item task reference replacement service', 'line_number_start must be greater than 0'
    end

    context 'when line_number_end is lower than line_number_start' do
      let(:line_number_end) { line_number_start - 1 }

      it_behaves_like 'failing work item task reference replacement service', 'line_number_end must be greater or equal to line_number_start'
    end

    context 'when lock_version is older than current' do
      let(:lock_version) { 2 }

      it_behaves_like 'failing work item task reference replacement service', 'Stale work item. Check lock version'
    end

    context 'when work item is stale before updating' do
      it_behaves_like 'failing work item task reference replacement service', 'Stale work item. Check lock version' do
        before do
          ::WorkItem.where(id: work_item.id).update_all(lock_version: lock_version + 1)
        end
      end
    end
  end
end