summaryrefslogtreecommitdiff
path: root/spec/helpers/ci/pipeline_editor_helper_spec.rb
blob: 12456deb538a5941832b0655b05a1fb60fda771c (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::PipelineEditorHelper do
  let_it_be(:project) { create(:project) }

  describe 'can_view_pipeline_editor?' do
    subject { helper.can_view_pipeline_editor?(project) }

    it 'user can view editor if they can collaborate' do
      allow(helper).to receive(:can_collaborate_with_project?).and_return(true)

      expect(subject).to be true
    end

    it 'user can not view editor if they cannot collaborate' do
      allow(helper).to receive(:can_collaborate_with_project?).and_return(false)

      expect(subject).to be false
    end
  end

  describe '#js_pipeline_editor_data' do
    let(:project) { create(:project, :repository) }

    before do
      allow(helper)
        .to receive(:namespace_project_new_merge_request_path)
        .and_return('/mock/project/-/merge_requests/new')

      allow(helper)
        .to receive(:image_path)
        .and_return('foo')
    end

    subject(:pipeline_editor_data) { helper.js_pipeline_editor_data(project) }

    context 'with a project with commits' do
      it 'returns pipeline editor data' do
        expect(pipeline_editor_data).to eq({
          "ci-config-path": project.ci_config_path_or_default,
          "ci-examples-help-page-path" => help_page_path('ci/examples/index'),
          "ci-help-page-path" => help_page_path('ci/index'),
          "default-branch" => project.default_branch_or_main,
          "empty-state-illustration-path" => 'foo',
          "initial-branch-name" => nil,
          "lint-help-page-path" => help_page_path('ci/lint', anchor: 'check-cicd-syntax'),
          "lint-unavailable-help-page-path" => help_page_path('ci/pipeline_editor/index', anchor: 'configuration-validation-currently-not-available-message'),
          "needs-help-page-path" => help_page_path('ci/yaml/index', anchor: 'needs'),
          "new-merge-request-path" => '/mock/project/-/merge_requests/new',
          "pipeline_etag" => graphql_etag_pipeline_sha_path(project.commit.sha),
          "pipeline-page-path" => project_pipelines_path(project),
          "project-path" => project.path,
          "project-full-path" => project.full_path,
          "project-namespace" => project.namespace.full_path,
          "runner-help-page-path" => help_page_path('ci/runners/index'),
          "total-branches" => project.repository.branches.length,
          "yml-help-page-path" => help_page_path('ci/yaml/index')
        })
      end
    end

    context 'with an empty project' do
      let(:project) { create(:project, :empty_repo) }

      it 'returns pipeline editor data' do
        expect(pipeline_editor_data).to eq({
          "ci-config-path": project.ci_config_path_or_default,
          "ci-examples-help-page-path" => help_page_path('ci/examples/index'),
          "ci-help-page-path" => help_page_path('ci/index'),
          "default-branch" => project.default_branch_or_main,
          "empty-state-illustration-path" => 'foo',
          "initial-branch-name" => nil,
          "lint-help-page-path" => help_page_path('ci/lint', anchor: 'check-cicd-syntax'),
          "lint-unavailable-help-page-path" => help_page_path('ci/pipeline_editor/index', anchor: 'configuration-validation-currently-not-available-message'),
          "needs-help-page-path" => help_page_path('ci/yaml/index', anchor: 'needs'),
          "new-merge-request-path" => '/mock/project/-/merge_requests/new',
          "pipeline_etag" => '',
          "pipeline-page-path" => project_pipelines_path(project),
          "project-path" => project.path,
          "project-full-path" => project.full_path,
          "project-namespace" => project.namespace.full_path,
          "runner-help-page-path" => help_page_path('ci/runners/index'),
          "total-branches" => 0,
          "yml-help-page-path" => help_page_path('ci/yaml/index')
        })
      end
    end

    context 'with a project with no repository' do
      let(:project) { create(:project) }

      it 'returns pipeline editor data' do
        expect(pipeline_editor_data).to include({
          "pipeline_etag" => '',
          "total-branches" => 0
        })
      end
    end

    context 'with a non-default branch name' do
      let(:user) { create(:user) }

      before do
        create_commit('Message', project, user, 'feature')
        controller.params[:branch_name] = 'feature'
      end

      it 'returns correct values' do
        expect(pipeline_editor_data['initial-branch-name']).to eq('feature')
      end
    end
  end
end