-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests to the Keep Markup plugin (#1646)
Uses JSDOM to simulate the DOM in node.
- Loading branch information
1 parent
439ea1e
commit a944c41
Showing
4 changed files
with
97 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const expect = require('chai').expect; | ||
const jsdom = require('jsdom') | ||
const { JSDOM } = jsdom | ||
|
||
require('../../../prism') | ||
// fake DOM | ||
global.self = {} | ||
global.self.Prism = Prism | ||
global.document = {} | ||
document.createRange = function () { | ||
} | ||
global.self.document = document | ||
|
||
require('../../../plugins/keep-markup/prism-keep-markup') | ||
|
||
describe('Prism Keep Markup Plugin', function () { | ||
|
||
function execute (code) { | ||
const start = []; | ||
const end = []; | ||
const nodes = []; | ||
document.createRange = function () { | ||
return { | ||
setStart: function (node, offset) { | ||
start.push({ node, offset }) | ||
}, | ||
setEnd: function (node, offset) { | ||
end.push({ node, offset }) | ||
}, | ||
extractContents: function () { | ||
return new JSDOM('').window.document.createTextNode('') | ||
}, | ||
insertNode: function (node) { | ||
nodes.push(node) | ||
}, | ||
detach: function () { | ||
} | ||
} | ||
} | ||
const beforeHighlight = Prism.hooks.all['before-highlight'][0] | ||
const afterHighlight = Prism.hooks.all['after-highlight'][0] | ||
const env = { | ||
element: new JSDOM(code).window.document.getElementsByTagName('code')[0], | ||
language: "javascript" | ||
} | ||
beforeHighlight(env) | ||
afterHighlight(env) | ||
return { start, end, nodes } | ||
} | ||
|
||
it('should keep <span> markup', function () { | ||
const result = execute(`<code class="language-javascript">x<span>a</span>y</code>`) | ||
expect(result.start.length).to.equal(1) | ||
expect(result.end.length).to.equal(1) | ||
expect(result.nodes.length).to.equal(1) | ||
expect(result.nodes[0].nodeName).to.equal('SPAN') | ||
}) | ||
it('should preserve markup order', function () { | ||
const result = execute(`<code class="language-javascript">x<a></a><b></b>y</code>`) | ||
expect(result.start.length).to.equal(2) | ||
expect(result.start[0].offset).to.equal(0) | ||
expect(result.start[0].node.textContent).to.equal('y') | ||
expect(result.start[1].offset).to.equal(0) | ||
expect(result.start[1].node.textContent).to.equal('y') | ||
expect(result.end.length).to.equal(2) | ||
expect(result.end[0].offset).to.equal(0) | ||
expect(result.end[0].node.textContent).to.equal('y') | ||
expect(result.end[1].offset).to.equal(0) | ||
expect(result.end[1].node.textContent).to.equal('y') | ||
expect(result.nodes.length).to.equal(2) | ||
expect(result.nodes[0].nodeName).to.equal('A') | ||
expect(result.nodes[1].nodeName).to.equal('B') | ||
}) | ||
it('should keep last <span> markup', function () { | ||
const result = execute(`<code class="language-javascript">xy<span>a</span></code>`) | ||
expect(result.start.length).to.equal(1) | ||
expect(result.end.length).to.equal(1) | ||
expect(result.nodes.length).to.equal(1) | ||
expect(result.nodes[0].nodeName).to.equal('SPAN') | ||
}) | ||
// The markup is removed if it's the last element and the element's name is a single letter: a(nchor), b(old), i(talic)... | ||
// https://github.com/PrismJS/prism/issues/1618 | ||
/* | ||
it('should keep last single letter empty markup', function () { | ||
const result = execute(`<code class="language-javascript">xy<a></a></code>`) | ||
expect(result.start.length).to.equal(1) | ||
expect(result.end.length).to.equal(1) | ||
expect(result.nodes.length).to.equal(1) | ||
expect(result.nodes[0].nodeName).to.equal('A') | ||
}) | ||
*/ | ||
}) |