summaryrefslogtreecommitdiffstats
path: root/lib/models
diff options
context:
space:
mode:
authorJohan Preynat <johan.preynat@gmail.com>2016-04-30 17:03:19 +0200
committerJohan Preynat <johan.preynat@gmail.com>2016-04-30 17:05:30 +0200
commitd9d7ab4ee59fad5c20e33cbf6125420fee5efffd (patch)
treea8667b957ef3cb909600c7a31425a3129b2654fa /lib/models
parent30bce5f9bb1f8ceee867770386fa6f7fdffd27ee (diff)
downloadgitbook-d9d7ab4ee59fad5c20e33cbf6125420fee5efffd.zip
gitbook-d9d7ab4ee59fad5c20e33cbf6125420fee5efffd.tar.gz
gitbook-d9d7ab4ee59fad5c20e33cbf6125420fee5efffd.tar.bz2
lib/models/templateBlock.js: Add test file lib/models/__tests__/templateBlock.js
Diffstat (limited to 'lib/models')
-rw-r--r--lib/models/__tests__/templateBlock.js107
1 files changed, 107 insertions, 0 deletions
diff --git a/lib/models/__tests__/templateBlock.js b/lib/models/__tests__/templateBlock.js
new file mode 100644
index 0000000..9f0e9b7
--- /dev/null
+++ b/lib/models/__tests__/templateBlock.js
@@ -0,0 +1,107 @@
+var nunjucks = require('nunjucks');
+var Immutable = require('immutable');
+var Promise = require('../../utils/promise');
+
+describe('TemplateBlock', function() {
+ var TemplateBlock = require('../templateBlock');
+ var TemplateEngine = require('../templateEngine');
+
+ describe('create', function() {
+ pit('must initialize a simple TemplateBlock from a function', function() {
+ var templateBlock = TemplateBlock.create('sayhello', function(block) {
+ return '<p>Hello, World!</p>';
+ });
+
+ // Check basic templateBlock properties
+ expect(templateBlock.getName()).toBe('sayhello');
+ expect(templateBlock.getPost()).toBeNull();
+ expect(templateBlock.getParse()).toBeTruthy();
+ expect(templateBlock.getEndTag()).toBe('endsayhello');
+ expect(templateBlock.getBlocks().size).toBe(0);
+ expect(templateBlock.getShortcuts().size).toBe(0);
+ expect(templateBlock.getExtensionName()).toBe('BlocksayhelloExtension');
+
+ // Check result of applying block
+ return Promise()
+ .then(function() {
+ return templateBlock.applyBlock();
+ })
+ .then(function(result) {
+ expect(result.name).toBe('sayhello');
+ expect(result.body).toBe('<p>Hello, World!</p>');
+ });
+ });
+ });
+
+ describe('toNunjucksExt()', function() {
+ pit('must create a valid nunjucks extension', function() {
+ var templateBlock = TemplateBlock.create('sayhello', function(block) {
+ return '<p>Hello, World!</p>';
+ });
+
+ // Create a fresh Nunjucks environment
+ var env = new nunjucks.Environment(null, { autoescape: false });
+
+ // Add template block to environement
+ var Ext = templateBlock.toNunjucksExt();
+ env.addExtension(templateBlock.getExtensionName(), new Ext());
+
+ // Render a template using the block
+ var src = '{% sayhello %}{% endsayhello %}';
+ return Promise.nfcall(env.renderString.bind(env), src)
+ .then(function(res) {
+ expect(res).toBe('<p>Hello, World!</p>');
+ });
+ });
+
+ pit('must apply block arguments correctly', function() {
+ var templateBlock = TemplateBlock.create('sayhello', function(block) {
+ return '<'+block.kwargs.tag+'>Hello, '+block.kwargs.name+'!</'+block.kwargs.tag+'>';
+ });
+
+ // Create a fresh Nunjucks environment
+ var env = new nunjucks.Environment(null, { autoescape: false });
+
+ // Add template block to environement
+ var Ext = templateBlock.toNunjucksExt();
+ env.addExtension(templateBlock.getExtensionName(), new Ext());
+
+ // Render a template using the block
+ var src = '{% sayhello name="Samy", tag="p" %}{% endsayhello %}';
+ return Promise.nfcall(env.renderString.bind(env), src)
+ .then(function(res) {
+ expect(res).toBe('<p>Hello, Samy!</p>');
+ });
+ });
+
+ pit('must handle nested blocks', function() {
+ var templateBlock = new TemplateBlock({
+ name: 'yoda',
+ blocks: Immutable.List(['start', 'end']),
+ process: function(block) {
+ var nested = {};
+
+ block.blocks.forEach(function(blk) {
+ nested[blk.name] = blk.body.trim();
+ });
+
+ return '<p class="yoda">'+nested.end+' '+nested.start+'</p>';
+ }
+ });
+
+ // Create a fresh Nunjucks environment
+ var env = new nunjucks.Environment(null, { autoescape: false });
+
+ // Add template block to environement
+ var Ext = templateBlock.toNunjucksExt();
+ env.addExtension(templateBlock.getExtensionName(), new Ext());
+
+ // Render a template using the block
+ var src = '{% yoda %}{% start %}this sentence should be{% end %}inverted{% endyoda %}';
+ return Promise.nfcall(env.renderString.bind(env), src)
+ .then(function(res) {
+ expect(res).toBe('<p class="yoda">inverted this sentence should be</p>');
+ });
+ });
+ });
+}); \ No newline at end of file