This is a test content with markdown
' } before do allow(CommonMarker).to receive(:render_doc).with(markdown_content, :DEFAULT).and_return(doc) - allow(SuperscriptRenderer).to receive(:new).and_return(superscript_renderer) - allow(superscript_renderer).to receive(:render).with(doc).and_return(html_content) + allow(CustomMarkdownRenderer).to receive(:new).and_return(markdown_renderer) + allow(markdown_renderer).to receive(:render).with(doc).and_return(html_content) end describe '#render_article' do diff --git a/spec/lib/custom_markdown_renderer_spec.rb b/spec/lib/custom_markdown_renderer_spec.rb new file mode 100644 index 000000000..623f12de8 --- /dev/null +++ b/spec/lib/custom_markdown_renderer_spec.rb @@ -0,0 +1,117 @@ +require 'rails_helper' + +describe CustomMarkdownRenderer do + let(:renderer) { described_class.new } + + def render_markdown(markdown) + doc = CommonMarker.render_doc(markdown, :DEFAULT) + renderer.render(doc) + end + + describe '#text' do + it 'converts text wrapped in ^ to superscript' do + markdown = 'This is an example of a superscript: ^superscript^.' + expect(render_markdown(markdown)).to include('superscript') + end + + it 'does not convert text not wrapped in ^' do + markdown = 'This is an example without superscript.' + expect(render_markdown(markdown)).not_to include('') + end + + it 'converts multiple superscripts in the same text' do + markdown = 'This is an example with ^multiple^ ^superscripts^.' + rendered_html = render_markdown(markdown) + expect(rendered_html.scan('').length).to eq(2) + expect(rendered_html).to include('multiple') + expect(rendered_html).to include('superscripts') + end + end + + describe 'broken ^ usage' do + it 'does not convert text that only starts with ^' do + markdown = 'This is an example with ^broken superscript.' + expected_output = 'This is an example with ^broken superscript.
' + expect(render_markdown(markdown)).to include(expected_output) + end + + it 'does not convert text that only ends with ^' do + markdown = 'This is an example with broken^ superscript.' + expected_output = 'This is an example with broken^ superscript.
' + expect(render_markdown(markdown)).to include(expected_output) + end + + it 'does not convert text with uneven numbers of ^' do + markdown = 'This is an example with ^broken^ superscript^.' + expected_output = 'This is an example with broken superscript^.
' + expect(render_markdown(markdown)).to include(expected_output) + end + end + + describe '#link' do + def render_markdown_link(link) + doc = CommonMarker.render_doc("[link](#{link})", :DEFAULT) + renderer.render(doc) + end + + context 'when link is a YouTube URL' do + let(:youtube_url) { 'https://www.youtube.com/watch?v=VIDEO_ID' } + + it 'renders an iframe with YouTube embed code' do + output = render_markdown_link(youtube_url) + expect(output).to include(` +