require 'rails_helper' require 'discourse_diff' describe DiscourseDiff do describe "inline_html" do it "does not lead to XSS" do a = "start" b = "end" prev = "
#{CGI::escapeHTML(a)}
" cur = "
#{CGI::escapeHTML(b)}
" diff = DiscourseDiff.new(prev,cur) expect(diff.inline_html).not_to match(/<\/?test>/) expect(diff.side_by_side_html).not_to match(/<\/?test>/) end it "returns an empty div when no content is diffed" do expect(DiscourseDiff.new("", "").inline_html).to eq("
") end it "returns the diffed content when there is no difference" do before = after = "

this is a paragraph

" expect(DiscourseDiff.new(before, after).inline_html).to eq("

this is a paragraph

") end it "adds tags around added text" do before = "

this is a paragraph

" after = "

this is a great paragraph

" expect(DiscourseDiff.new(before, after).inline_html).to eq("

this is a great paragraph

") end it "adds tags around removed text" do before = "

this is a great paragraph

" after = "

this is a paragraph

" expect(DiscourseDiff.new(before, after).inline_html).to eq("

this is a great paragraph

") end it "adds .diff-ins class when a paragraph is added" do before = "

this is the first paragraph

" after = "

this is the first paragraph

this is the second paragraph

" expect(DiscourseDiff.new(before, after).inline_html).to eq("

this is the first paragraph

this is the second paragraph

") end it "adds .diff-del class when a paragraph is removed" do before = "

this is the first paragraph

this is the second paragraph

" after = "

this is the second paragraph

" expect(DiscourseDiff.new(before, after).inline_html).to eq("

this is the first paragraph

this is the second paragraph

") end end describe "side_by_side_html" do it "returns two empty divs when no content is diffed" do expect(DiscourseDiff.new("", "").side_by_side_html).to eq("
") end it "returns the diffed content on both sides when there is no difference" do before = after = "

this is a paragraph

" expect(DiscourseDiff.new(before, after).side_by_side_html).to eq("

this is a paragraph

this is a paragraph

") end it "adds tags around added text on the right div" do before = "

this is a paragraph

" after = "

this is a great paragraph

" expect(DiscourseDiff.new(before, after).side_by_side_html).to eq("

this is a paragraph

this is a great paragraph

") end it "adds tags around removed text on the left div" do before = "

this is a great paragraph

" after = "

this is a paragraph

" expect(DiscourseDiff.new(before, after).side_by_side_html).to eq("

this is a great paragraph

this is a paragraph

") end it "adds .diff-ins class when a paragraph is added" do before = "

this is the first paragraph

" after = "

this is the first paragraph

this is the second paragraph

" expect(DiscourseDiff.new(before, after).side_by_side_html).to eq("

this is the first paragraph

this is the first paragraph

this is the second paragraph

") end it "adds .diff-del class when a paragraph is removed" do before = "

this is the first paragraph

this is the second paragraph

" after = "

this is the second paragraph

" expect(DiscourseDiff.new(before, after).side_by_side_html).to eq("

this is the first paragraph

this is the second paragraph

this is the second paragraph

") end end describe "side_by_side_markdown" do it "returns an empty table when no content is diffed" do expect(DiscourseDiff.new("", "").side_by_side_markdown).to eq("
") end it "properly escape html tags" do before = "" after = "\"" expect(DiscourseDiff.new(before, after).side_by_side_markdown).to eq("
<img src="//domain.com/image.png>"
") end it "returns the diffed content on both columns when there is no difference" do before = after = "this is a paragraph" expect(DiscourseDiff.new(before, after).side_by_side_markdown).to eq("
this is a paragraphthis is a paragraph
") end it "adds tags around added text on the second column" do before = "this is a paragraph" after = "this is a great paragraph" expect(DiscourseDiff.new(before, after).side_by_side_markdown).to eq("
this is a paragraphthis is a great paragraph
") end it "adds tags around removed text on the first column" do before = "this is a great paragraph" after = "this is a paragraph" expect(DiscourseDiff.new(before, after).side_by_side_markdown).to eq("
this is a great paragraphthis is a paragraph
") end it "adds .diff-ins class when a paragraph is added" do before = "this is the first paragraph" after = "this is the first paragraph\nthis is the second paragraph" expect(DiscourseDiff.new(before, after).side_by_side_markdown).to eq("
this is the first paragraphthis is the first paragraph\nthis is the second paragraph
") end it "adds .diff-del class when a paragraph is removed" do before = "this is the first paragraph\nthis is the second paragraph" after = "this is the second paragraph" expect(DiscourseDiff.new(before, after).side_by_side_markdown).to eq("
this is the first paragraph\n
this is the second paragraphthis is the second paragraph
") end end end