show_spec.rb 5.46 KB
Newer Older
1
2
# frozen_string_literal: true

Douwe Maan's avatar
Douwe Maan committed
3
4
require 'spec_helper'

5
describe 'Snippet', :js do
Douwe Maan's avatar
Douwe Maan committed
6
7
8
  let(:project) { create(:project, :repository) }
  let(:snippet) { create(:personal_snippet, :public, file_name: file_name, content: content) }

9
10
11
12
  before do
    stub_feature_flags(snippets_vue: false)
  end

Douwe Maan's avatar
Douwe Maan committed
13
14
15
16
17
18
19
  context 'Ruby file' do
    let(:file_name) { 'popen.rb' }
    let(:content) { project.repository.blob_at('master', 'files/ruby/popen.rb').data }

    before do
      visit snippet_path(snippet)

20
      wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
21
22
23
24
25
26
27
28
29
30
31
32
    end

    it 'displays the blob' do
      aggregate_failures do
        # shows highlighted Ruby code
        expect(page).to have_content("require 'fileutils'")

        # does not show a viewer switcher
        expect(page).not_to have_selector('.js-blob-viewer-switcher')

        # shows an enabled copy button
        expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
33
34
35
36
37
38

        # shows a raw button
        expect(page).to have_link('Open raw')

        # shows a download button
        expect(page).to have_link('Download')
Douwe Maan's avatar
Douwe Maan committed
39
40
41
42
43
44
45
46
47
48
49
50
      end
    end
  end

  context 'Markdown file' do
    let(:file_name) { 'ruby-style-guide.md' }
    let(:content) { project.repository.blob_at('master', 'files/markdown/ruby-style-guide.md').data }

    context 'visiting directly' do
      before do
        visit snippet_path(snippet)

51
        wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
      end

      it 'displays the blob using the rich viewer' do
        aggregate_failures do
          # hides the simple viewer
          expect(page).to have_selector('.blob-viewer[data-type="simple"]', visible: false)
          expect(page).to have_selector('.blob-viewer[data-type="rich"]')

          # shows rendered Markdown
          expect(page).to have_link("PEP-8")

          # shows a viewer switcher
          expect(page).to have_selector('.js-blob-viewer-switcher')

          # shows a disabled copy button
          expect(page).to have_selector('.js-copy-blob-source-btn.disabled')
68
69
70
71
72
73

          # shows a raw button
          expect(page).to have_link('Open raw')

          # shows a download button
          expect(page).to have_link('Download')
Douwe Maan's avatar
Douwe Maan committed
74
75
76
        end
      end

77
78
      context 'Markdown rendering' do
        let(:snippet) { create(:personal_snippet, :public, file_name: file_name, content: content) }
79
80
81
        let(:file_name) { 'test.md' }
        let(:content) { "1. one\n  - sublist\n" }

82
83
84
85
86
        context 'when rendering default markdown' do
          it 'renders using CommonMark' do
            expect(page).to have_content("sublist")
            expect(page).not_to have_xpath("//ol//li//ul")
          end
87
88
89
        end
      end

Douwe Maan's avatar
Douwe Maan committed
90
91
92
93
      context 'switching to the simple viewer' do
        before do
          find('.js-blob-viewer-switch-btn[data-viewer=simple]').click

94
          wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        end

        it 'displays the blob using the simple viewer' do
          aggregate_failures do
            # hides the rich viewer
            expect(page).to have_selector('.blob-viewer[data-type="simple"]')
            expect(page).to have_selector('.blob-viewer[data-type="rich"]', visible: false)

            # shows highlighted Markdown code
            expect(page).to have_content("[PEP-8](http://www.python.org/dev/peps/pep-0008/)")

            # shows an enabled copy button
            expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
          end
        end

        context 'switching to the rich viewer again' do
          before do
            find('.js-blob-viewer-switch-btn[data-viewer=rich]').click

115
            wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
          end

          it 'displays the blob using the rich viewer' do
            aggregate_failures do
              # hides the simple viewer
              expect(page).to have_selector('.blob-viewer[data-type="simple"]', visible: false)
              expect(page).to have_selector('.blob-viewer[data-type="rich"]')

              # shows an enabled copy button
              expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
            end
          end
        end
      end
    end

    context 'visiting with a line number anchor' do
      before do
        visit snippet_path(snippet, anchor: 'L1')

136
        wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
      end

      it 'displays the blob using the simple viewer' do
        aggregate_failures do
          # hides the rich viewer
          expect(page).to have_selector('.blob-viewer[data-type="simple"]')
          expect(page).to have_selector('.blob-viewer[data-type="rich"]', visible: false)

          # highlights the line in question
          expect(page).to have_selector('#LC1.hll')

          # shows highlighted Markdown code
          expect(page).to have_content("[PEP-8](http://www.python.org/dev/peps/pep-0008/)")

          # shows an enabled copy button
          expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
        end
      end
    end
  end
157
158
159
160
161
162
163
164

  it_behaves_like 'showing user status' do
    let(:file_name) { 'popen.rb' }
    let(:content) { project.repository.blob_at('master', 'files/ruby/popen.rb').data }
    let(:user_with_status) { snippet.author }

    subject { visit snippet_path(snippet) }
  end
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

  context 'when user cannot create snippets' do
    let(:user) { create(:user, :external) }
    let(:snippet) { create(:personal_snippet, :public) }

    before do
      sign_in(user)

      visit snippet_path(snippet)

      wait_for_requests
    end

    it 'does not show the "New Snippet" button' do
      expect(page).not_to have_link('New snippet')
    end
  end
Douwe Maan's avatar
Douwe Maan committed
182
end