diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-03 00:08:01 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-03 00:08:01 +0300 |
commit | 561e1b470f0a99fe6304c8f197348c47a637d594 (patch) | |
tree | 6b361b6b0b412b70450aca167079c50a13bd88d8 /spec/presenters | |
parent | 7b52c7cb634ef7047d30b0337fe477bcdcedf41d (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/presenters')
-rw-r--r-- | spec/presenters/snippet_blob_presenter_spec.rb | 83 |
1 files changed, 60 insertions, 23 deletions
diff --git a/spec/presenters/snippet_blob_presenter_spec.rb b/spec/presenters/snippet_blob_presenter_spec.rb index 89f4d65b47f..eb7621cc591 100644 --- a/spec/presenters/snippet_blob_presenter_spec.rb +++ b/spec/presenters/snippet_blob_presenter_spec.rb @@ -4,36 +4,73 @@ require 'spec_helper' describe SnippetBlobPresenter do describe '#rich_data' do - let(:snippet) { build(:personal_snippet) } + before do + allow_next_instance_of(described_class) do |instance| + allow(instance).to receive(:current_user).and_return(nil) + end + end subject { described_class.new(snippet.blob).rich_data } - it 'returns nil when the snippet blob is binary' do - allow(snippet.blob).to receive(:binary?).and_return(true) + context 'with PersonalSnippet' do + let(:raw_url) { "http://127.0.0.1:3000/snippets/#{snippet.id}/raw" } + let(:snippet) { build(:personal_snippet) } - expect(subject).to be_nil - end + it 'returns nil when the snippet blob is binary' do + allow(snippet.blob).to receive(:binary?).and_return(true) - it 'returns markdown content when snippet file is markup' do - snippet.file_name = 'test.md' - snippet.content = '*foo*' + expect(subject).to be_nil + end - expect(subject).to eq '<p data-sourcepos="1:1-1:5" dir="auto"><em>foo</em></p>' - end + context 'with markdown format' do + let(:snippet) { create(:personal_snippet, file_name: 'test.md', content: '*foo*') } - it 'returns syntax highlighted content' do - snippet.file_name = 'test.rb' - snippet.content = 'class Foo;end' + 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 '<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>' - end + expect(subject).to eq(expected) + end + end - it 'returns plain text highlighted content' do - snippet.file_name = 'test' - snippet.content = 'foo' + context 'with notebook format' do + let(:snippet) { create(:personal_snippet, file_name: 'test.ipynb') } - expect(subject).to eq '<span id="LC1" class="line" lang="plaintext">foo</span>' + 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 end end @@ -55,19 +92,19 @@ describe SnippetBlobPresenter do expect(subject).to eq '<span id="LC1" class="line" lang="markdown"><span class="ge">*foo*</span></span>' end - it 'returns plain syntax content' do + it 'returns highlighted syntax content' do snippet.file_name = 'test.rb' snippet.content = 'class Foo;end' expect(subject) - .to eq '<span id="LC1" class="line" lang="">class Foo;end</span>' + .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>' end it 'returns plain text highlighted content' do snippet.file_name = 'test' snippet.content = 'foo' - expect(subject).to eq '<span id="LC1" class="line" lang="">foo</span>' + expect(subject).to eq '<span id="LC1" class="line" lang="plaintext">foo</span>' end end |