Skip to content

Commit 2a792ff

Browse files
Adds browser based CommonJS tests
1 parent b6bf336 commit 2a792ff

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

src/core.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ let brushCounter = 0;
254254

255255
export default sh;
256256
export const registerBrush = brush => sh.brushes['brush' + brushCounter++] = brush.default || brush;
257+
export const clearRegisteredBrushes = () => {
258+
sh.brushes = {};
259+
brushCounter = 0;
260+
}
257261

258262
/* an EJS hook for `gulp build --brushes` command
259263
* <%- registerBrushes %>

tests/integration/commonjs.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sizzle from 'sizzle';
2+
import {expect} from 'chai';
3+
import SyntaxHighlighter, {registerBrush, clearRegisteredBrushes} from '../..';
4+
import Brush from '../fixtures/test_brush_v4';
5+
6+
function setupSyntaxHighlighter(html) {
7+
let div;
8+
9+
before(done => {
10+
registerBrush(Brush);
11+
12+
div = document.createElement('div');
13+
div.innerHTML = html;
14+
document.body.appendChild(div);
15+
16+
SyntaxHighlighter.highlight({gutter: false});
17+
18+
function wait() {
19+
if (sizzle('.syntaxhighlighter').length) {
20+
done();
21+
} else {
22+
setTimeout(wait, 900);
23+
}
24+
}
25+
26+
wait();
27+
});
28+
29+
after(() => {
30+
clearRegisteredBrushes();
31+
document.body.removeChild(div);
32+
});
33+
}
34+
35+
describe('integration/commonjs', () => {
36+
describe('first render pass', () => {
37+
setupSyntaxHighlighter(`<pre class="brush: test_brush_v4;">first</pre>`);
38+
it('has applied the brush', () => expect(sizzle('.syntaxhighlighter')[0].innerHTML).to.contain(`<code class="test_brush_v4 plain">first</code>`));
39+
it('does not render gutter', () => expect(sizzle('.syntaxhighlighter td.gutter').length).to.equal(0));
40+
});
41+
42+
describe('second render pass', () => {
43+
setupSyntaxHighlighter(`<pre class="brush: test_brush_v4;">second</pre>`);
44+
it('has applied the brush', () => expect(sizzle('.syntaxhighlighter')[0].innerHTML).to.contain(`<code class="test_brush_v4 plain">second</code>`));
45+
});
46+
});

tests/karma/integration.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = function (config) {
88
included: false,
99
},
1010
'tests/integration/no-compat.test.js',
11+
'tests/integration/commonjs.test.js',
1112
];
1213

1314
config.set(opts);

tests/unit/commonjs.test.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import {expect} from 'chai';
22
import Brush from '../fixtures/test_brush_v4';
33

4-
describe('unit/commonjs', function() {
5-
let brush;
4+
describe('unit/commonjs', () => {
65
let html;
76

8-
before(function() {
9-
brush = new Brush();
10-
html = brush.getHtml('hello foo bar world!');
11-
});
7+
describe('using a brush', () => {
8+
let brush;
9+
10+
before(() => {
11+
brush = new Brush();
12+
html = brush.getHtml('hello foo bar world!', {gutter: false});
13+
});
1214

13-
it('returns html', () => expect(html).to.be.ok);
14-
it('renders content', () => expect(html).to.contain('<div class="line number1 index0 alt2"><code class="keyword">hello</code> <code class="plain">foo bar </code><code class="keyword">world</code><code class="plain">!</code></div>'));
15+
it('returns html', () => expect(html).to.be.ok);
16+
it('renders content', () => expect(html).to.contain('<div class="line number1 index0 alt2"><code class="keyword">hello</code> <code class="plain">foo bar </code><code class="keyword">world</code><code class="plain">!</code></div>'));
17+
it('does not render gutter', () => expect(html).to.not.contain('class="gutter'));
18+
});
1519
});

0 commit comments

Comments
 (0)