summaryrefslogtreecommitdiffstats
path: root/lib/models/__tests__/templateBlock.js
blob: e5f766619f8282cadafce0c72f7761ed89737b8c (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var nunjucks = require('nunjucks');
var Immutable = require('immutable');
var Promise = require('../../utils/promise');

describe('TemplateBlock', function() {
    var TemplateBlock = require('../templateBlock');

    describe('create', function() {
        it('must initialize a simple TemplateBlock from a function', function() {
            var templateBlock = TemplateBlock.create('sayhello', function(block) {
                return {
                    body: '<p>Hello, World!</p>',
                    parse: true
                };
            });

            // Check basic templateBlock properties
            expect(templateBlock.getName()).toBe('sayhello');
            expect(templateBlock.getEndTag()).toBe('endsayhello');
            expect(templateBlock.getBlocks().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('getShortcuts', function() {
        it('must return undefined if no shortcuts', function() {
            var templateBlock = TemplateBlock.create('sayhello', function(block) {
                return {
                    body: '<p>Hello, World!</p>',
                    parse: true
                };
            });

            expect(templateBlock.getShortcuts()).toNotExist();
        });

        it('must return complete shortcut', function() {
            var templateBlock = TemplateBlock.create('sayhello', {
                process: function(block) {
                    return '<p>Hello, World!</p>';
                },
                shortcuts: {
                    parsers: ['markdown'],
                    start: '$',
                    end: '-'
                }
            });

            var shortcut = templateBlock.getShortcuts();

            expect(shortcut).toBeDefined();
            expect(shortcut.getStart()).toEqual('$');
            expect(shortcut.getEnd()).toEqual('-');
            expect(shortcut.getStartTag()).toEqual('sayhello');
            expect(shortcut.getEndTag()).toEqual('endsayhello');
        });
    });

    describe('toNunjucksExt()', function() {
        it('should replace by block anchor', function() {
            var templateBlock = TemplateBlock.create('sayhello', function(block) {
                return 'Hello';
            });

            var blocks = {};

            // Create a fresh Nunjucks environment
            var env = new nunjucks.Environment(null, { autoescape: false });

            // Add template block to environement
            var Ext = templateBlock.toNunjucksExt({}, blocks);
            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) {
                blocks = Immutable.fromJS(blocks);
                expect(blocks.size).toBe(1);

                var blockId = blocks.keySeq().get(0);
                var block = blocks.get(blockId);

                expect(res).toBe('{{-%' + blockId + '%-}}');
                expect(block.get('body')).toBe('Hello');
                expect(block.get('name')).toBe('sayhello');
            });
        });

        it('must create a valid nunjucks extension', function() {
            var templateBlock = TemplateBlock.create('sayhello', function(block) {
                return {
                    body: '<p>Hello, World!</p>',
                    parse: true
                };
            });

            // 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>');
            });
        });

        it('must apply block arguments correctly', function() {
            var templateBlock = TemplateBlock.create('sayhello', function(block) {
                return {
                    body: '<'+block.kwargs.tag+'>Hello, '+block.kwargs.name+'!</'+block.kwargs.tag+'>',
                    parse: true
                };
            });

            // 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>');
            });
        });

        it('must accept an async function', function() {
            var templateBlock = TemplateBlock.create('sayhello', function(block) {
                return Promise()
                .then(function() {
                    return {
                        body: 'Hello ' + block.body,
                        parse: true
                    };
                });
            });

            // 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 %}Samy{% endsayhello %}';
            return Promise.nfcall(env.renderString.bind(env), src)
            .then(function(res) {
                expect(res).toBe('Hello Samy');
            });
        });

        it('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 {
                        body: '<p class="yoda">'+nested.end+' '+nested.start+'</p>',
                        parse: true
                    };
                }
            });

            // 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>');
            });
        });
    });
});