diff options
author | Johan Preynat <johan.preynat@gmail.com> | 2016-10-13 21:06:42 +0200 |
---|---|---|
committer | Johan Preynat <johan.preynat@gmail.com> | 2016-10-13 21:07:23 +0200 |
commit | bb14f48c3988597794c8304f9ef5da60db7f8446 (patch) | |
tree | 7c33ba3006d308b489e248e57df2e69684eba099 | |
parent | d56019dc18b98a32d7ec6584a9a6d31670864d61 (diff) | |
download | gitbook-bb14f48c3988597794c8304f9ef5da60db7f8446.zip gitbook-bb14f48c3988597794c8304f9ef5da60db7f8446.tar.gz gitbook-bb14f48c3988597794c8304f9ef5da60db7f8446.tar.bz2 |
Add tests for multiple inline template blocks
-rw-r--r-- | packages/gitbook/src/models/__tests__/templateBlock.js | 25 | ||||
-rw-r--r-- | packages/gitbook/src/templating/__tests__/replaceShortcuts.js | 6 |
2 files changed, 29 insertions, 2 deletions
diff --git a/packages/gitbook/src/models/__tests__/templateBlock.js b/packages/gitbook/src/models/__tests__/templateBlock.js index 408b57e..5db8a80 100644 --- a/packages/gitbook/src/models/__tests__/templateBlock.js +++ b/packages/gitbook/src/models/__tests__/templateBlock.js @@ -30,7 +30,7 @@ describe('TemplateBlock', function() { }); }); - it('must not fsil if return a string', function() { + it('must not fail if return a string', function() { const templateBlock = TemplateBlock.create('sayhello', function(block) { return 'Hello World'; }); @@ -191,5 +191,28 @@ describe('TemplateBlock', function() { expect(res).toBe('<xblock name="yoda" props="{}"><p class="yoda">inverted this sentence should be</p></xblock>'); }); }); + + it('must handle multiple inline blocks', function() { + const templateBlock = new TemplateBlock({ + name: 'math', + process(block) { + return '<math>' + block.children + '</math>'; + } + }); + + // Create a fresh Nunjucks environment + const env = new nunjucks.Environment(null, { autoescape: false }); + + // Add template block to environement + const Ext = templateBlock.toNunjucksExt(); + env.addExtension(templateBlock.getExtensionName(), new Ext()); + + // Render a template using the block after replacing shortcuts + const src = 'There should be two inline blocks as a result: {% math %}a = b{% endmath %} and {% math %}c = d{% endmath %}'; + return Promise.nfcall(env.renderString.bind(env), src) + .then(function(res) { + expect(res).toBe('There should be two inline blocks as a result: <xblock name="math" props="{}"><math>a = b</math></xblock> and <xblock name="math" props="{}"><math>c = d</math></xblock>'); + }); + }); }); }); diff --git a/packages/gitbook/src/templating/__tests__/replaceShortcuts.js b/packages/gitbook/src/templating/__tests__/replaceShortcuts.js index cfd18b5..1126f91 100644 --- a/packages/gitbook/src/templating/__tests__/replaceShortcuts.js +++ b/packages/gitbook/src/templating/__tests__/replaceShortcuts.js @@ -19,9 +19,13 @@ describe('replaceShortcuts', function() { expect(content).toBe('Hello {% math %}a = b{% endmath %}'); }); + it('should correctly replace multiple inline matches by block', function() { + const content = replaceShortcuts(blocks, 'test.md', 'Hello $$a = b$$ and $$c = d$$'); + expect(content).toBe('Hello {% math %}a = b{% endmath %} and {% math %}c = d{% endmath %}'); + }); + it('should correctly replace block matches', function() { const content = replaceShortcuts(blocks, 'test.md', 'Hello\n$$\na = b\n$$\n'); expect(content).toBe('Hello\n{% math %}\na = b\n{% endmath %}\n'); }); }); - |