# frozen_string_literal: true require 'spec_helper' RSpec.describe Markup::RenderingService do describe '#execute' do let_it_be(:project) { create(:project, :repository) } let_it_be(:user) do user = create(:user, username: 'gfm') project.add_maintainer(user) user end let_it_be(:context) { { project: project } } let_it_be(:postprocess_context) { { current_user: user } } let(:file_name) { nil } let(:text) { 'Noël' } subject do described_class .new(text, file_name: file_name, context: context, postprocess_context: postprocess_context) .execute end context 'when text is missing' do let(:text) { nil } it 'returns an empty string' do is_expected.to eq('') end end context 'when file_name is missing' do it 'returns html (rendered by Banzai)' do expected_html = '
Noël
' expect(Banzai).to receive(:render).with(text, context) { expected_html } is_expected.to eq(expected_html) end end context 'when postprocess_context is missing' do let(:file_name) { 'foo.txt' } let(:postprocess_context) { nil } it 'returns html (rendered by Banzai)' do expected_html = 'Noël' expect(Banzai).not_to receive(:post_process) { expected_html } is_expected.to eq(expected_html) end end context 'when rendered context is present' do let(:rendered) { 'rendered text' } let(:file_name) { 'foo.md' } it 'returns an empty string' do context[:rendered] = rendered is_expected.to eq(rendered) end end context 'when file is a markdown file' do let(:file_name) { 'foo.md' } it 'returns html (rendered by Banzai)' do expected_html = '
Noël
' expect(Banzai).to receive(:render).with(text, context) { expected_html } is_expected.to eq(expected_html) end context 'when renderer returns an error' do before do allow(Banzai).to receive(:render).and_raise(StandardError, "An error") end it 'returns html (rendered by ActionView:TextHelper)' do is_expected.to eq('Noël
') end it 'logs the error' do expect(Gitlab::ErrorTracking).to receive(:track_exception).with( instance_of(StandardError), project_id: context[:project].id, file_name: 'foo.md' ) subject end end end context 'when file is asciidoc file' do let(:file_name) { 'foo.adoc' } it 'returns html (rendered by Gitlab::Asciidoc)' do expected_html = "Noël
\n