Commit 8af21fd3 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'fj-improve-snippet-presenter-spec' into 'master'

Improve snippet presenter spec with repo data

See merge request gitlab-org/gitlab!43255
parents 4272dff1 009659f9
......@@ -3,70 +3,75 @@
require 'spec_helper'
RSpec.describe SnippetBlobPresenter do
let_it_be(:snippet) { create(:personal_snippet, :repository) }
let(:branch) { snippet.default_branch }
let(:blob) { snippet.blobs.first }
describe '#rich_data' do
let(:data_endpoint_url) { "/-/snippets/#{snippet.id}/raw/#{branch}/#{file}" }
before do
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:current_user).and_return(nil)
end
blob.name = File.basename(file)
blob.path = file
end
subject { described_class.new(snippet.blob).rich_data }
subject { described_class.new(blob).rich_data }
context 'with PersonalSnippet' do
let(:snippet) { create(:personal_snippet, :repository) }
context 'when blob is binary' do
it 'returns the HTML associated with the binary' do
allow(snippet).to receive(:blob).and_return(snippet.repository.blob_at('master', 'files/images/logo-black.png'))
let(:file) { 'files/images/logo-black.png' }
let(:blob) { blob_at(file) }
it 'returns the HTML associated with the binary' do
expect(subject).to include('file-content image_file')
end
end
context 'with markdown format' do
let(:snippet) { create(:personal_snippet, file_name: 'test.md', content: '*foo*') }
let(:file) { 'README.md' }
let(:blob) { blob_at(file) }
it 'returns rich markdown content' do
expected = <<~HTML
<div class="file-content md">
<p data-sourcepos="1:1-1:5" dir="auto"><em>foo</em></p>
</div>
HTML
expect(subject).to eq(expected)
expect(subject).to include('file-content md')
end
end
context 'with notebook format' do
let(:snippet) { create(:personal_snippet, file_name: 'test.ipynb') }
let(:file) { 'test.ipynb' }
it 'returns rich notebook content' do
expect(subject.strip).to eq %Q(<div class="file-content" data-endpoint="/-/snippets/#{snippet.id}/raw" id="js-notebook-viewer"></div>)
expect(subject.strip).to eq %Q(<div class="file-content" data-endpoint="#{data_endpoint_url}" id="js-notebook-viewer"></div>)
end
end
context 'with openapi format' do
let(:snippet) { create(:personal_snippet, file_name: 'openapi.yml') }
let(:file) { 'openapi.yml' }
it 'returns rich openapi content' do
expect(subject).to eq %Q(<div class="file-content" data-endpoint="/-/snippets/#{snippet.id}/raw" id="js-openapi-viewer"></div>\n)
expect(subject).to eq %Q(<div class="file-content" data-endpoint="#{data_endpoint_url}" id="js-openapi-viewer"></div>\n)
end
end
context 'with svg format' do
let(:snippet) { create(:personal_snippet, file_name: 'test.svg') }
let(:file) { 'files/images/wm.svg' }
let(:blob) { blob_at(file) }
it 'returns rich svg content' do
result = Nokogiri::HTML::DocumentFragment.parse(subject)
image_tag = result.search('img').first
expect(image_tag.attr('src')).to include("data:#{snippet.blob.mime_type};base64")
expect(image_tag.attr('alt')).to eq('test.svg')
expect(image_tag.attr('src')).to include("data:#{blob.mime_type};base64")
expect(image_tag.attr('alt')).to eq(File.basename(file))
end
end
context 'with other format' do
let(:snippet) { create(:personal_snippet, file_name: 'test') }
let(:file) { 'test' }
it 'does not return no rich content' do
expect(subject).to be_nil
......@@ -76,36 +81,41 @@ RSpec.describe SnippetBlobPresenter do
end
describe '#plain_data' do
let(:snippet) { build(:personal_snippet) }
let(:blob) { blob_at(file) }
subject { described_class.new(snippet.blob).plain_data }
subject { described_class.new(blob).plain_data }
it 'returns nil when the snippet blob is binary' do
allow(snippet.blob).to receive(:binary?).and_return(true)
context 'when blob is binary' do
let(:file) { 'files/images/logo-black.png' }
expect(subject).to be_nil
it 'returns nil' do
expect(subject).to be_nil
end
end
it 'returns plain content when snippet file is markup' do
snippet.file_name = 'test.md'
snippet.content = '*foo*'
context 'when blob is markup' do
let(:file) { 'README.md' }
expect(subject).to eq '<span id="LC1" class="line" lang="markdown"><span class="ge">*foo*</span></span>'
it 'returns plain content' do
expect(subject).to include('<span id="LC1" class="line" lang="markdown">')
end
end
it 'returns highlighted syntax content' do
snippet.file_name = 'test.rb'
snippet.content = 'class Foo;end'
context 'when blob has syntax' do
let(:file) { 'files/ruby/regex.rb' }
expect(subject)
.to eq '<span id="LC1" class="line" lang="ruby"><span class="k">class</span> <span class="nc">Foo</span><span class="p">;</span><span class="k">end</span></span>'
it 'returns highlighted syntax content' do
expect(subject)
.to include '<span id="LC1" class="line" lang="ruby"><span class="k">module</span> <span class="nn">Gitlab</span>'
end
end
it 'returns plain text highlighted content' do
snippet.file_name = 'test'
snippet.content = 'foo'
context 'when blob has plain data' do
let(:file) { 'LICENSE' }
expect(subject).to eq '<span id="LC1" class="line" lang="plaintext">foo</span>'
it 'returns plain text highlighted content' do
expect(subject).to include('<span id="LC1" class="line" lang="plaintext">The MIT License (MIT)</span>')
end
end
end
......@@ -179,4 +189,8 @@ RSpec.describe SnippetBlobPresenter do
end
end
end
def blob_at(path)
snippet.repository.blob_at(branch, path)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment