2021-02-08 13:09:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe IssuablesDescriptionTemplatesHelper, :clean_gitlab_redis_cache do
|
|
|
|
describe '#issuable_templates' do
|
2022-07-22 05:11:01 -04:00
|
|
|
include_context 'project issuable templates context'
|
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
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) }
|
|
|
|
|
2021-05-28 02:10:48 -04:00
|
|
|
it 'returns empty hash when template type does not exist' do
|
|
|
|
expect(helper.issuable_templates(build(:project), 'non-existent-template-type')).to eq({})
|
2021-02-18 07:09:34 -05:00
|
|
|
end
|
2021-02-08 13:09:49 -05:00
|
|
|
|
2021-02-18 07:09:34 -05:00
|
|
|
context 'with cached issuable templates' do
|
2021-02-08 13:09:49 -05:00
|
|
|
it 'does not call TemplateFinder' do
|
2021-02-18 07:09:34 -05:00
|
|
|
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')
|
2021-02-08 13:09:49 -05:00
|
|
|
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
|
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
describe '#available_service_desk_templates_for' do
|
2021-02-18 07:09:34 -05:00
|
|
|
let_it_be(:project) { build(:project) }
|
2021-02-08 13:09:49 -05:00
|
|
|
|
2021-02-18 07:09:34 -05:00
|
|
|
before do
|
2021-02-08 13:09:49 -05:00
|
|
|
allow(helper).to receive(:ref_project).and_return(project)
|
|
|
|
allow(helper).to receive(:issuable_templates).and_return(templates)
|
2021-02-18 07:09:34 -05:00
|
|
|
end
|
|
|
|
|
2021-08-18 08:11:19 -04:00
|
|
|
context 'with project templates' do
|
2021-02-18 07:09:34 -05:00
|
|
|
let(:templates) do
|
2021-05-28 02:10:48 -04:00
|
|
|
{
|
|
|
|
"" => [
|
2021-09-15 05:09:47 -04:00
|
|
|
{ name: "another_issue_template", id: "another_issue_template", project_id: project.id },
|
|
|
|
{ name: "custom_issue_template", id: "custom_issue_template", project_id: project.id }
|
2021-05-28 02:10:48 -04:00
|
|
|
]
|
|
|
|
}
|
2021-02-18 07:09:34 -05:00
|
|
|
end
|
|
|
|
|
2021-09-15 05:09:47 -04:00
|
|
|
it 'returns project templates' do
|
2021-11-09 07:12:15 -05:00
|
|
|
value = [
|
2022-09-21 08:13:58 -04:00
|
|
|
"",
|
|
|
|
[
|
|
|
|
{ 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
|
2021-11-09 07:12:15 -05:00
|
|
|
expect(helper.available_service_desk_templates_for(@project)).to eq(value)
|
2021-02-18 07:09:34 -05:00
|
|
|
end
|
2022-07-22 05:11:01 -04:00
|
|
|
end
|
2022-01-27 22:15:57 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
context 'when there are no 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)
|
2022-01-27 22:15:57 -05:00
|
|
|
end
|
2022-07-22 05:11:01 -04:00
|
|
|
end
|
|
|
|
end
|
2022-01-27 22:15:57 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
describe '#selected_template_name' do
|
|
|
|
let(:template_names) { %w(another_issue_template custom_issue_template) }
|
2022-01-27 22:15:57 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
context 'when no issuable_template parameter is provided' do
|
|
|
|
it 'does not select a template' do
|
|
|
|
expect(helper.selected_template_name(template_names)).to be_nil
|
2022-01-27 22:15:57 -05:00
|
|
|
end
|
2022-07-22 05:11:01 -04:00
|
|
|
end
|
2022-01-27 22:15:57 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
context 'when an issuable_template parameter has been provided' do
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:params).and_return({ issuable_template: template_param_value })
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when param matches existing templates' do
|
|
|
|
let(:template_param_value) { 'another_issue_template' }
|
|
|
|
|
|
|
|
it 'returns the matching issuable template' do
|
|
|
|
expect(helper.selected_template_name(template_names)).to eq('another_issue_template')
|
2022-01-27 22:15:57 -05:00
|
|
|
end
|
2022-07-22 05:11:01 -04:00
|
|
|
end
|
2022-01-27 22:15:57 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
context 'when param does not match any templates' do
|
|
|
|
let(:template_param_value) { 'non_matching_issue_template' }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(helper.selected_template_name(template_names)).to be_nil
|
2022-01-27 22:15:57 -05:00
|
|
|
end
|
|
|
|
end
|
2021-02-18 07:09:34 -05:00
|
|
|
end
|
2022-07-22 05:11:01 -04:00
|
|
|
end
|
2021-02-18 07:09:34 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
describe '#default_template_name' do
|
|
|
|
context 'when a default template is available' do
|
|
|
|
let(:template_names) { %w(another_issue_template deFault) }
|
2021-02-08 13:09:49 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
it 'returns the default template' do
|
|
|
|
issue = build(:issue)
|
|
|
|
|
|
|
|
expect(helper.default_template_name(template_names, issue)).to be('deFault')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when issuable has a description set' do
|
|
|
|
issue = build(:issue, description: 'from template in project settings')
|
|
|
|
|
|
|
|
expect(helper.default_template_name(template_names, issue)).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when issuable is persisted' do
|
|
|
|
issue = create(:issue)
|
|
|
|
|
|
|
|
expect(helper.default_template_name(template_names, issue)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no default template' do
|
|
|
|
let(:template_names) { %w(another_issue_template) }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(helper.default_template_name(template_names, build(:issue))).to be_nil
|
2021-02-08 13:09:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-07-22 05:11:01 -04:00
|
|
|
|
|
|
|
describe '#template_names' do
|
|
|
|
let(:project) { build(:project) }
|
|
|
|
let(:templates) do
|
|
|
|
{
|
|
|
|
"Project templates" => [
|
|
|
|
{ name: "another_issue_template", id: "another_issue_template", project_id: project.id },
|
|
|
|
{ name: "custom_issue_template", id: "custom_issue_template", project_id: project.id }
|
|
|
|
],
|
|
|
|
"Group templates" => [
|
|
|
|
{ name: "another_issue_template", id: "another_issue_template", project_id: project.id }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:ref_project).and_return(project)
|
|
|
|
allow(helper).to receive(:issuable_templates).and_return(templates)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns unique list of template names' do
|
|
|
|
expect(helper.template_names(build(:issue))).to contain_exactly('another_issue_template', 'custom_issue_template')
|
|
|
|
end
|
|
|
|
end
|
2021-02-08 13:09:49 -05:00
|
|
|
end
|