diff options
Diffstat (limited to 'spec/lib/gitlab/markdown/snippet_reference_filter_spec.rb')
-rw-r--r-- | spec/lib/gitlab/markdown/snippet_reference_filter_spec.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/spec/lib/gitlab/markdown/snippet_reference_filter_spec.rb b/spec/lib/gitlab/markdown/snippet_reference_filter_spec.rb new file mode 100644 index 00000000000..d79a7e544a3 --- /dev/null +++ b/spec/lib/gitlab/markdown/snippet_reference_filter_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' + +module Gitlab::Markdown + describe SnippetReferenceFilter do + include ReferenceFilterSpecHelper + + let(:project) { create(:empty_project) } + let(:snippet) { create(:project_snippet, project: project) } + let(:reference) { "$#{snippet.id}" } + + it 'requires project context' do + expect { described_class.call('Snippet $123', {}) }. + to raise_error(ArgumentError, /:project/) + end + + %w(pre code a style).each do |elem| + it "ignores valid references contained inside '#{elem}' element" do + exp = act = "<#{elem}>Snippet #{reference}</#{elem}>" + expect(filter(act).to_html).to eq exp + end + end + + it 'links to a valid reference' do + doc = filter("See #{reference}") + + expect(doc.css('a').first.attr('href')).to eq urls. + namespace_project_snippet_url(project.namespace, project, snippet) + end + + it 'links with adjacent text' do + doc = filter("Snippet (#{reference}.)") + expect(doc.to_html).to match(/\(<a.+>#{Regexp.escape(reference)}<\/a>\.\)/) + end + + it 'ignores invalid snippet IDs' do + exp = act = "Snippet $#{snippet.id + 1}" + + expect(filter(act).to_html).to eq exp + end + + it 'includes a title attribute' do + doc = filter("Snippet #{reference}") + expect(doc.css('a').first.attr('title')).to eq "Snippet: #{snippet.title}" + end + + it 'includes default classes' do + doc = filter("Snippet #{reference}") + expect(doc.css('a').first.attr('class')).to eq 'gfm gfm-snippet' + end + + it 'includes an optional custom class' do + doc = filter("Snippet #{reference}", reference_class: 'custom') + expect(doc.css('a').first.attr('class')).to include 'custom' + end + + it 'supports an :only_path context' do + doc = filter("Snippet #{reference}", only_path: true) + link = doc.css('a').first.attr('href') + + expect(link).not_to match %r(https?://) + expect(link).to eq urls.namespace_project_snippet_url(project.namespace, project, snippet, only_path: true) + end + end +end |