2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe SnippetBlobPresenter do
|
|
|
|
describe '#rich_data' do
|
2020-04-08 14:13:33 +05:30
|
|
|
before do
|
|
|
|
allow_next_instance_of(described_class) do |instance|
|
|
|
|
allow(instance).to receive(:current_user).and_return(nil)
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
subject { described_class.new(snippet.blob).rich_data }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:raw_url) { "http://127.0.0.1:3000/snippets/#{snippet.id}/raw" }
|
|
|
|
let(:snippet) { build(:personal_snippet) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it 'returns nil when the snippet blob is binary' do
|
|
|
|
allow(snippet.blob).to receive(:binary?).and_return(true)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'with markdown format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: 'test.md', content: '*foo*') }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
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
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(subject).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'with notebook format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: 'test.ipynb') }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
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>)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with openapi format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: '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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with svg format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: 'test.svg') }
|
|
|
|
|
|
|
|
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')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with other format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: 'test') }
|
|
|
|
|
|
|
|
it 'does not return no rich content' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#plain_data' do
|
|
|
|
let(:snippet) { build(:personal_snippet) }
|
|
|
|
|
|
|
|
subject { described_class.new(snippet.blob).plain_data }
|
|
|
|
|
|
|
|
it 'returns nil when the snippet blob is binary' do
|
|
|
|
allow(snippet.blob).to receive(:binary?).and_return(true)
|
|
|
|
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns plain content when snippet file is markup' do
|
|
|
|
snippet.file_name = 'test.md'
|
|
|
|
snippet.content = '*foo*'
|
|
|
|
|
|
|
|
expect(subject).to eq '<span id="LC1" class="line" lang="markdown"><span class="ge">*foo*</span></span>'
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it 'returns highlighted syntax content' do
|
2020-03-13 15:44:24 +05:30
|
|
|
snippet.file_name = 'test.rb'
|
|
|
|
snippet.content = 'class Foo;end'
|
|
|
|
|
|
|
|
expect(subject)
|
2020-04-08 14:13:33 +05:30
|
|
|
.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>'
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns plain text highlighted content' do
|
|
|
|
snippet.file_name = 'test'
|
|
|
|
snippet.content = 'foo'
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(subject).to eq '<span id="LC1" class="line" lang="plaintext">foo</span>'
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#raw_path' do
|
|
|
|
subject { described_class.new(snippet.blob).raw_path }
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let!(:project) { create(:project) }
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:snippet) { create(:project_snippet, project: project) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
it 'returns the raw path' do
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(subject).to eq "/#{snippet.project.full_path}/snippets/#{snippet.id}/raw"
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:snippet) { create(:personal_snippet) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
it 'returns the raw path' do
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(subject).to eq "/snippets/#{snippet.id}/raw"
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|