preview_markdown_service_spec.rb 2.97 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe PreviewMarkdownService do
  let(:user) { create(:user) }
5
  let(:project) { create(:project) }
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

  before do
    project.add_developer(user)
  end

  describe 'user references' do
    let(:params) { { text: "Take a look #{user.to_reference}" } }
    let(:service) { described_class.new(project, user, params) }

    it 'returns users referenced in text' do
      result = service.execute

      expect(result[:users]).to eq [user.username]
    end
  end

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
  describe 'suggestions' do
    let(:params) { { text: "```suggestion\nfoo\n```", preview_suggestions: preview_suggestions } }
    let(:service) { described_class.new(project, user, params) }

    context 'when preview markdown param is present' do
      let(:preview_suggestions) { true }

      it 'returns users referenced in text' do
        result = service.execute

        expect(result[:suggestions]).to eq(['foo'])
      end
    end

    context 'when preview markdown param is not present' do
      let(:preview_suggestions) { false }

      it 'returns users referenced in text' do
        result = service.execute

        expect(result[:suggestions]).to eq([])
      end
    end
  end

47
  context 'new note with quick actions' do
48 49 50 51
    let(:issue) { create(:issue, project: project) }
    let(:params) do
      {
        text: "Please do it\n/assign #{user.to_reference}",
52 53
        quick_actions_target_type: 'Issue',
        quick_actions_target_id: issue.id
54 55 56 57
      }
    end
    let(:service) { described_class.new(project, user, params) }

58
    it 'removes quick actions from text' do
59 60 61 62 63
      result = service.execute

      expect(result[:text]).to eq 'Please do it'
    end

64
    it 'explains quick actions effect' do
65 66 67 68 69 70 71 72 73 74
      result = service.execute

      expect(result[:commands]).to eq "Assigns #{user.to_reference}."
    end
  end

  context 'merge request description' do
    let(:params) do
      {
        text: "My work\n/estimate 2y",
75
        quick_actions_target_type: 'MergeRequest'
76 77 78 79
      }
    end
    let(:service) { described_class.new(project, user, params) }

80
    it 'removes quick actions from text' do
81 82 83 84 85
      result = service.execute

      expect(result[:text]).to eq 'My work'
    end

86
    it 'explains quick actions effect' do
87 88 89
      result = service.execute

      expect(result[:commands]).to eq 'Sets time estimate to 2y.'
90
    end
91
  end
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
  context 'commit description' do
    let(:project) { create(:project, :repository) }
    let(:commit) { project.commit }
    let(:params) do
      {
        text: "My work\n/tag v1.2.3 Stable release",
        quick_actions_target_type: 'Commit',
        quick_actions_target_id: commit.id
      }
    end
    let(:service) { described_class.new(project, user, params) }

    it 'removes quick actions from text' do
      result = service.execute

      expect(result[:text]).to eq 'My work'
    end

    it 'explains quick actions effect' do
      result = service.execute

      expect(result[:commands]).to eq 'Tags this commit to v1.2.3 with "Stable release".'
    end
  end
117
end