summaryrefslogtreecommitdiff
path: root/spec/services/markdown_content_rewriter_service_spec.rb
blob: 37c8a210ba58c9a0cace38f957889b82eca2b5e8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe MarkdownContentRewriterService do
  let_it_be(:user) { create(:user) }
  let_it_be(:source_parent) { create(:project, :public) }
  let_it_be(:target_parent) { create(:project, :public) }

  let(:content) { 'My content' }

  describe '#initialize' do
    it 'raises an error if source_parent is not a Project' do
      expect do
        described_class.new(user, content, create(:group), target_parent)
      end.to raise_error(ArgumentError, 'The rewriter classes require that `source_parent` is a `Project`')
    end
  end

  describe '#execute' do
    subject { described_class.new(user, content, source_parent, target_parent).execute }

    it 'calls the rewriter classes successfully', :aggregate_failures do
      [Gitlab::Gfm::ReferenceRewriter, Gitlab::Gfm::UploadsRewriter].each do |rewriter_class|
        service = double

        expect(service).to receive(:rewrite).with(target_parent)
        expect(rewriter_class).to receive(:new).and_return(service)
      end

      subject
    end

    # Perform simple integration-style tests for each rewriter class.
    # to prove they run correctly.
    context 'when content contains a reference' do
      let_it_be(:issue) { create(:issue, project: source_parent) }

      let(:content) { "See ##{issue.iid}" }

      it 'rewrites content' do
        expect(subject).to eq("See #{source_parent.full_path}##{issue.iid}")
      end
    end

    context 'when content contains an upload' do
      let(:image_uploader) { build(:file_uploader, project: source_parent) }
      let(:content) { "Text and #{image_uploader.markdown_link}" }

      it 'rewrites content' do
        new_content = subject

        expect(new_content).not_to eq(content)
        expect(new_content.length).to eq(content.length)
      end
    end
  end
end