summaryrefslogtreecommitdiff
path: root/spec/helpers/issuables_description_templates_helper_spec.rb
blob: 768ce5975c1e5c0eee1771a3e0c1b81e0b63e574 (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
113
114
115
116
117
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe IssuablesDescriptionTemplatesHelper, :clean_gitlab_redis_cache do
  include_context 'project issuable templates context'

  describe '#issuable_templates' do
    let_it_be(:inherited_from) { nil }
    let_it_be(:user) { create(:user) }
    let_it_be(:parent_group, reload: true) { create(:group) }
    let_it_be(:project, reload: true) { create(:project, :custom_repo, files: issuable_template_files) }
    let_it_be(:group_member) { create(:group_member, :developer, group: parent_group, user: user) }
    let_it_be(:project_member) { create(:project_member, :developer, user: user, project: project) }

    it 'returns empty hash when template type does not exist' do
      expect(helper.issuable_templates(build(:project), 'non-existent-template-type')).to eq({})
    end

    context 'with cached issuable templates' do
      it 'does not call TemplateFinder' do
        expect(Gitlab::Template::IssueTemplate).to receive(:template_names).once.and_call_original
        expect(Gitlab::Template::MergeRequestTemplate).to receive(:template_names).once.and_call_original

        helper.issuable_templates(project, 'issues')
        helper.issuable_templates(project, 'merge_request')
        helper.issuable_templates(project, 'issues')
        helper.issuable_templates(project, 'merge_request')
      end
    end

    context 'when project has no parent group' do
      it_behaves_like 'project issuable templates'
    end

    context 'when project has parent group' do
      before do
        project.update!(group: parent_group)
      end

      context 'when project parent group does not have a file template project' do
        it_behaves_like 'project issuable templates'
      end
    end
  end

  describe '#selected_template' do
    let_it_be(:project) { build(:project) }

    before do
      allow(helper).to receive(:ref_project).and_return(project)
      allow(helper).to receive(:issuable_templates).and_return(templates)
    end

    context 'with project templates' do
      let(:templates) do
        {
          "" => [
            { name: "another_issue_template", id: "another_issue_template", project_id: project.id },
            { name: "custom_issue_template", id: "custom_issue_template", project_id: project.id }
          ]
        }
      end

      it 'returns project templates' do
        value = [
            "",
            [
              { name: "another_issue_template", id: "another_issue_template", project_id: project.id },
              { name: "custom_issue_template", id: "custom_issue_template", project_id: project.id }
            ]
          ].to_json
        expect(helper.available_service_desk_templates_for(@project)).to eq(value)
      end

      context 'when no issuable_template parameter or default template is present' do
        it 'does not select a template' do
          expect(helper.selected_template(project)).to be(nil)
        end
      end

      context 'when an issuable_template parameter has been provided' do
        before do
          allow(helper).to receive(:params).and_return({ issuable_template: 'another_issue_template' })
        end

        it 'selects the issuable template' do
          expect(helper.selected_template(project)).to eq('another_issue_template')
        end
      end

      context 'when there is a default template' do
        let(:templates) do
          {
            "" => [
              { name: "another_issue_template", id: "another_issue_template", project_id: project.id },
              { name: "default", id: "default", project_id: project.id }
            ]
          }
        end

        it 'selects the default template' do
          expect(helper.selected_template(project)).to eq('default')
        end
      end
    end

    context 'when there are not templates in the project' do
      let(:templates) { {} }

      it 'returns empty array' do
        value = [].to_json
        expect(helper.available_service_desk_templates_for(@project)).to eq(value)
      end
    end
  end
end