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