Skip to content

Commit 828436c

Browse files
author
Mikolaj Dadela
committed
Added tests for examples loader.
1 parent e4af933 commit 828436c

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

loaders/examples.loader.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ function findRequires(codeString) {
5252
return Object.keys(requires);
5353
}
5454

55-
56-
module.exports = function(source, map) {
55+
function examplesLoader(source, map) {
5756
this.cacheable && this.cacheable();
5857

5958
var examples = readExamples(source);
@@ -86,4 +85,13 @@ module.exports = function(source, map) {
8685
'function(code) {var require=requireInRuntime; return eval(code);}'
8786
) + ';',
8887
].join('\n');
89-
};
88+
}
89+
90+
_.assign(examplesLoader, {
91+
requireAnythingRegex: requireAnythingRegex,
92+
simpleStringRegex: simpleStringRegex,
93+
readExamples: readExamples,
94+
findRequires: findRequires,
95+
});
96+
97+
module.exports = examplesLoader;

test/examples.loader.spec.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { expect } from 'chai';
2+
3+
import examplesLoader from '../loaders/examples.loader';
4+
5+
/* eslint max-nested-callbacks: [1, 5] */
6+
7+
describe('examples loader', () => {
8+
9+
describe('requireAnythingRegex', () => {
10+
11+
let regex;
12+
beforeEach(() => {
13+
expect(examplesLoader.requireAnythingRegex).to.be.an.instanceof(RegExp);
14+
// we make a version without the /g flag
15+
regex = new RegExp(examplesLoader.requireAnythingRegex, '');
16+
});
17+
18+
it('should match require invocations', () => {
19+
expect(`require("foo")`).to.match(regex);
20+
expect(`require ( "foo" )`).to.match(regex);
21+
expect(`require('foo')`).to.match(regex);
22+
expect(`require(foo)`).to.match(regex);
23+
expect(`require("f" + "o" + "o")`).to.match(regex);
24+
expect(`require("f" + ("o" + "o"))`).to.match(regex);
25+
expect(`function f() { require("foo"); }`).to.match(regex);
26+
});
27+
28+
it('should not match other occurences of require', () => {
29+
expect(`"required field"`).not.to.match(regex);
30+
expect(`var f = require;`).not.to.match(regex);
31+
expect(`require.call(module, "foo")`).not.to.match(regex);
32+
});
33+
});
34+
35+
describe('simpleStringRegex', () => {
36+
it('should match simple strings and nothing else', () => {
37+
let regex = examplesLoader.simpleStringRegex;
38+
39+
expect(`"foo"`).to.match(regex);
40+
expect(`'foo'`).to.match(regex);
41+
expect(`"fo'o"`).to.match(regex);
42+
expect(`'fo"o'`).to.match(regex);
43+
expect(`'.,:;!§$&/()=@^12345'`).to.match(regex);
44+
45+
expect(`foo`).not.to.match(regex);
46+
expect(`'foo"`).not.to.match(regex);
47+
expect(`"foo'`).not.to.match(regex);
48+
49+
// these 2 are actually valid in JS, but don't work with this regex.
50+
// But you shouldn't be using these in your requires anyway.
51+
expect(`"fo\\"o"`).not.to.match(regex);
52+
expect(`'fo\\'o'`).not.to.match(regex);
53+
54+
expect(`"foo" + "bar"`).not.to.match(regex);
55+
});
56+
});
57+
58+
describe('findRequires', () => {
59+
it('should find calls to require in code', () => {
60+
let findRequires = examplesLoader.findRequires;
61+
expect(findRequires(`require('foo')`)).to.deep.equal(['foo']);
62+
});
63+
});
64+
65+
describe('readExamples', () => {
66+
it('should separate code and html chunks', () => {
67+
let examplesMarkdown = '# header\n\n <div />\n\ntext';
68+
let examples = examplesLoader.readExamples(examplesMarkdown);
69+
expect(examples).to.have.length(3);
70+
expect(examples[0].type).to.equal('html');
71+
expect(examples[1].type).to.equal('code');
72+
expect(examples[2].type).to.equal('html');
73+
});
74+
});
75+
76+
describe('loader', () => {
77+
it('should return valid, parsable js', () => {
78+
let exampleMarkdown = '# header\n\n <div />\n\ntext';
79+
let output = examplesLoader.call({}, exampleMarkdown);
80+
expect(() => new Function(output)).not.to.throw(SyntaxError); // eslint-disable-line no-new-func
81+
});
82+
});
83+
84+
});

0 commit comments

Comments
 (0)