|
| 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