summaryrefslogtreecommitdiff
path: root/spec/features/dashboard/snippets_spec.rb
blob: 287af7e7b114d65a3af74a159683d80ab02f278f (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true

require 'spec_helper'

describe 'Dashboard snippets' do
  let_it_be(:user) { create(:user) }

  context 'when the project has snippets' do
    let(:project) { create(:project, :public, creator: user) }
    let!(:snippets) { create_list(:project_snippet, 2, :public, author: project.owner, project: project) }

    before do
      allow(Snippet).to receive(:default_per_page).and_return(1)
      sign_in(project.owner)
      visit dashboard_snippets_path
    end

    it_behaves_like 'paginated snippets'

    it 'shows new snippet button in header' do
      parent_element = page.find('.page-title-controls')
      expect(parent_element).to have_link('New snippet')
    end
  end

  context 'when there are no project snippets', :js do
    let(:project) { create(:project, :public, creator: user) }

    before do
      sign_in(project.owner)
      visit dashboard_snippets_path
    end

    it 'shows the empty state when there are no snippets' do
      element = page.find('.row.empty-state')

      expect(element).to have_content("Code snippets")
      expect(element.find('.svg-content img')['src']).to have_content('illustrations/snippets_empty')
    end

    it 'shows new snippet button in main content area' do
      parent_element = page.find('.row.empty-state')
      expect(parent_element).to have_link('New snippet')
    end

    it 'shows documentation button in main comment area' do
      parent_element = page.find('.row.empty-state')
      expect(parent_element).to have_link('Documentation', href: help_page_path('user/snippets.md'))
    end
  end

  context 'rendering file names' do
    let_it_be(:snippet) { create(:personal_snippet, :public, author: user, file_name: 'foo.txt') }
    let_it_be(:versioned_snippet) { create(:personal_snippet, :repository, :public, author: user, file_name: 'bar.txt') }

    before do
      sign_in(user)
    end

    context 'when feature flag :version_snippets is disabled' do
      before do
        stub_feature_flags(version_snippets: false)

        visit dashboard_snippets_path
      end

      it 'contains the snippet file names from the DB' do
        aggregate_failures do
          expect(page).to have_content 'foo.txt'
          expect(page).to have_content('bar.txt')
          expect(page).not_to have_content('.gitattributes')
        end
      end
    end

    context 'when feature flag :version_snippets is enabled' do
      before do
        stub_feature_flags(version_snippets: true)

        visit dashboard_snippets_path
      end

      it 'contains both the versioned and non-versioned filenames' do
        aggregate_failures do
          expect(page).to have_content 'foo.txt'
          expect(page).to have_content('.gitattributes')
          expect(page).not_to have_content('bar.txt')
        end
      end
    end
  end

  context 'filtering by visibility' do
    let_it_be(:snippets) do
      [
        create(:personal_snippet, :public, author: user),
        create(:personal_snippet, :internal, author: user),
        create(:personal_snippet, :private, author: user),
        create(:personal_snippet, :public)
      ]
    end

    before do
      sign_in(user)

      visit dashboard_snippets_path
    end

    it_behaves_like 'tabs with counts' do
      let_it_be(:counts) { { all: '3', public: '1', private: '1', internal: '1' } }
    end

    it 'contains all snippets of logged user' do
      expect(page).to have_selector('.snippet-row', count: 3)

      expect(page).to have_content(snippets[0].title)
      expect(page).to have_content(snippets[1].title)
      expect(page).to have_content(snippets[2].title)
    end

    it 'contains all private snippets of logged user when clicking on private' do
      click_link('Private')

      expect(page).to have_selector('.snippet-row', count: 1)
      expect(page).to have_content(snippets[2].title)
    end

    it 'contains all internal snippets of logged user when clicking on internal' do
      click_link('Internal')

      expect(page).to have_selector('.snippet-row', count: 1)
      expect(page).to have_content(snippets[1].title)
    end

    it 'contains all public snippets of logged user when clicking on public' do
      click_link('Public')

      expect(page).to have_selector('.snippet-row', count: 1)
      expect(page).to have_content(snippets[0].title)
    end
  end

  context 'as an external user' do
    let_it_be(:user) { create(:user, :external) }

    before do
      sign_in(user)
      visit dashboard_snippets_path
    end

    context 'without snippets' do
      it 'hides new snippet button' do
        expect(page).not_to have_link('New snippet')
      end
    end

    context 'with snippets' do
      let!(:snippets) { create(:personal_snippet, author: user) }

      it 'hides new snippet button' do
        expect(page).not_to have_link('New snippet')
      end
    end
  end
end