summaryrefslogtreecommitdiff
path: root/spec/helpers/releases_helper_spec.rb
blob: 5a9deb5c63b85978cdfd5e5873e8be3361b77a71 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ReleasesHelper do
  describe '#illustration' do
    it 'returns the correct image path' do
      expect(helper.illustration).to match(%r{illustrations/releases-(\w+)\.svg})
    end
  end

  describe '#releases_help_page_path' do
    it 'returns the correct link to the help page' do
      expect(helper.releases_help_page_path).to include('user/project/releases/index')
    end
  end

  context 'url helpers' do
    let(:project) { build(:project, namespace: create(:group)) }
    let(:release) { create(:release, project: project) }
    let(:user) { create(:user) }
    let(:can_user_create_release) { false }
    let(:common_keys) { [:project_id, :project_path, :illustration_path, :documentation_path] }

    # rubocop: disable CodeReuse/ActiveRecord
    before do
      helper.instance_variable_set(:@project, project)
      helper.instance_variable_set(:@release, release)
      allow(helper).to receive(:current_user).and_return(user)
      allow(helper).to receive(:can?)
                    .with(user, :create_release, project)
                    .and_return(can_user_create_release)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    describe '#data_for_releases_page' do
      it 'includes the required data for displaying release blocks' do
        expect(helper.data_for_releases_page.keys).to contain_exactly(*common_keys)
      end

      context 'when the user is allowed to create a new release' do
        let(:can_user_create_release) { true }

        it 'includes new_release_path' do
          expect(helper.data_for_releases_page.keys).to contain_exactly(*common_keys, :new_release_path)
        end

        it 'points new_release_path to the "New Release" page' do
          expect(helper.data_for_releases_page[:new_release_path]).to eq(new_project_release_path(project))
        end
      end

      context 'new releases redirect new milestone creation' do
        it 'redirects new_milestone_path back to the release page' do
          expect(helper.data_for_new_release_page[:new_milestone_path]).to include('redirect_path')
        end
      end
    end

    describe '#data_for_edit_release_page' do
      it 'has the needed data to display the "edit release" page' do
        keys = %i(project_id
                  group_id
                  group_milestones_available
                  project_path
                  tag_name
                  markdown_preview_path
                  markdown_docs_path
                  releases_page_path
                  release_assets_docs_path
                  manage_milestones_path
                  new_milestone_path
                  upcoming_release_docs_path
                  edit_release_docs_path
                  delete_release_docs_path)

        expect(helper.data_for_edit_release_page.keys).to match_array(keys)
      end
    end

    describe '#data_for_new_release_page' do
      it 'has the needed data to display the "new release" page' do
        keys = %i(project_id
                  group_id
                  group_milestones_available
                  project_path
                  tag_name
                  releases_page_path
                  markdown_preview_path
                  markdown_docs_path
                  release_assets_docs_path
                  manage_milestones_path
                  new_milestone_path
                  default_branch
                  upcoming_release_docs_path
                  edit_release_docs_path)

        expect(helper.data_for_new_release_page.keys).to match_array(keys)
      end
    end

    describe '#data_for_show_page' do
      it 'has the needed data to display the individual "release" page' do
        keys = %i(project_id
                  project_path
                  tag_name)

        expect(helper.data_for_show_page.keys).to match_array(keys)
      end
    end
  end

  describe 'startup queries' do
    describe 'use_startup_query_for_index_page?' do
      it 'allows startup queries for non-paginated requests' do
        allow(helper).to receive(:params).and_return({ unrelated_query_param: 'value' })

        expect(helper.use_startup_query_for_index_page?).to be(true)
      end

      it 'disallows startup queries for requests paginated with a "before" cursor' do
        allow(helper).to receive(:params).and_return({ unrelated_query_param: 'value', before: 'cursor' })

        expect(helper.use_startup_query_for_index_page?).to be(false)
      end

      it 'disallows startup queries for requests paginated with an "after" cursor' do
        allow(helper).to receive(:params).and_return({ unrelated_query_param: 'value', after: 'cursor' })

        expect(helper.use_startup_query_for_index_page?).to be(false)
      end
    end

    describe '#index_page_startup_query_variables' do
      let_it_be(:project) { build(:project, namespace: create(:group)) }

      before do
        helper.instance_variable_set(:@project, project)
      end

      it 'returns the correct GraphQL variables for the startup query' do
        expect(helper.index_page_startup_query_variables).to eq({
          fullPath: project.full_path,
          sort: 'RELEASED_AT_DESC',
          first: 1
        })
      end
    end
  end
end