summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/file_markdown_link_builder_spec.rb
blob: feb2776c5d02322cf4afa8f7fb40a66ab85b09d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true
require 'rails_helper'

describe Gitlab::FileMarkdownLinkBuilder do
  let(:custom_class) do
    Class.new do
      include Gitlab::FileMarkdownLinkBuilder
    end.new
  end

  before do
    allow(custom_class).to receive(:filename).and_return(filename)
  end

  describe 'markdown_link' do
    let(:url) { "/uploads/#{filename}"}

    before do
      allow(custom_class).to receive(:secure_url).and_return(url)
    end

    context 'when file name has the character ]' do
      let(:filename) { 'd]k.png' }

      it 'escapes the character' do
        expect(custom_class.markdown_link).to eq '![d\\]k](/uploads/d]k.png)'
      end
    end

    context 'when file is an image or video' do
      let(:filename) { 'dk.png' }

      it 'returns preview markdown link' do
        expect(custom_class.markdown_link).to eq '![dk](/uploads/dk.png)'
      end
    end

    context 'when file is not an image or video' do
      let(:filename) { 'dk.zip' }

      it 'returns markdown link' do
        expect(custom_class.markdown_link).to eq '[dk.zip](/uploads/dk.zip)'
      end
    end

    context 'when file name is blank' do
      let(:filename) { nil }

      it 'returns nil' do
        expect(custom_class.markdown_link).to eq nil
      end
    end
  end

  describe 'mardown_name' do
    context 'when file is an image or video' do
      let(:filename) { 'dk.png' }

      it 'retrieves the name without the extension' do
        expect(custom_class.markdown_name).to eq 'dk'
      end
    end

    context 'when file is not an image or video' do
      let(:filename) { 'dk.zip' }

      it 'retrieves the name with the extesion' do
        expect(custom_class.markdown_name).to eq 'dk.zip'
      end
    end

    context 'when file name is blank' do
      let(:filename) { nil }

      it 'returns nil' do
        expect(custom_class.markdown_name).to eq nil
      end
    end
  end
end