Welcome to mirror list, hosted at ThFree Co, Russian Federation.

link_spec.rb « docs « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c6d74659d2f313caeac23fe258c7c55045bacc7d (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Docs::Link do
  let(:page) { instance_double(Gitlab::Docs::Page) }
  let(:href) { '../some/page.html#some-anchor' }

  subject(:link) { described_class.new(href, page) }

  describe '#to_anchor?' do
    context 'when link contains anchor name' do
      let(:href) { '../some/page.html' }

      it { is_expected.not_to be_to_anchor }
    end

    context 'when link does not contain anchor name' do
      let(:href) { '../some/page.html#some-anchor' }

      it { is_expected.to be_to_anchor }
    end

    context 'when link contains an empty anchor' do
      let(:href) { '../some/page.html#' }

      it { is_expected.not_to be_to_anchor }
    end
  end

  describe '#internal_anchor?' do
    context 'when link contains internal anchor name' do
      let(:href) { '#some-internal-anchor' }

      it { is_expected.to be_internal_anchor }
    end

    context 'when link does not contain internal anchor' do
      let(:href) { '../some/page.html#some-anchor' }

      it { is_expected.not_to be_internal_anchor }
    end
  end

  describe '#internal?' do
    context 'when link is an external link' do
      let(:href) { 'https://gitlab.com/some/page.html' }

      it { is_expected.not_to be_internal }
    end

    context 'when link is an internal link' do
      let(:href) { '../some/page.html' }

      it { is_expected.to be_internal }
    end
  end

  describe '#absolute_path' do
    before do
      allow(page).to receive(:directory).and_return('/my/path')
      allow(Gitlab::Docs::Nanoc).to receive(:output_dir).and_return('/nanoc')
    end

    context 'when link is an external link' do
      let(:href) { 'https://gitlab.com/some/page.html' }

      it 'raises an error' do
        expect { link.absolute_path }.to raise_error(RuntimeError)
      end
    end

    context 'when link is relative' do
      let(:href) { '../some/page.html' }

      it 'expands relative link' do
        expect(link.absolute_path).to eq '/my/some/page.html'
      end
    end

    context 'when link is absolute' do
      let(:href) { '/some/page.html' }

      it 'expands absolute path' do
        expect(link.absolute_path).to eq '/nanoc/some/page.html'
      end
    end
  end

  describe '#destination_page' do
    context 'when the link is an internal link' do
      let(:href) { '#some-anchor' }

      it 'returns the page it is assigned by itself' do
        expect(link.destination_page).to eq page
      end
    end

    context 'when the link is an external link' do
      let(:href) { '../some/page.html#my-anchor' }
      let(:destination) { instance_double(Gitlab::Docs::Page) }

      before do
        allow(page).to receive(:directory).and_return('/my/docs/page')
      end

      it 'builds a new page' do
        expect(Gitlab::Docs::Page).to receive(:build)
          .with('/my/docs/some/page.html')
          .and_return(destination)

        expect(link.destination_page).to eq destination
      end
    end
  end

  describe '#source_file' do
    it 'returns page file' do
      expect(page).to receive(:file)

      link.source_file
    end
  end

  describe '#destination_page_not_found?' do
    before do
      allow(page).to receive(:directory).and_return('/my/dir')
      allow(link).to receive(:destination_file)
        .and_return(instance_double(Gitlab::Docs::Page, exists?: false))
    end

    it 'returns false if page does not exist' do
      expect(link.destination_page_not_found?).to be true
    end
  end

  describe '#destination_anchor_not_found?' do
    before do
      allow(page).to receive(:directory).and_return('/my/dir')
      allow(link).to receive(:destination_file)
        .and_return(instance_double(Gitlab::Docs::Page, has_anchor?: false))
    end

    it 'returns false if anchor does not exist' do
      expect(link.destination_anchor_not_found?).to be true
    end
  end
end