summaryrefslogtreecommitdiffstats
path: root/test/template.js
blob: 6eb227869fe9ff384fa3b59141694287108c5e15 (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
var mock = require('./mock');
var Output = require('../lib/output/base');

describe('Template', function() {
    var output;

    before(function() {
        return mock.outputDefaultBook(Output, {})
        .then(function(_output) {
            output = _output;
        });
    });

    describe('.renderString', function() {
        it('should render a simple string', function() {
            return output.template.renderString('Hello World')
                .should.be.fulfilledWith('Hello World');
        });

        it('should render with variable', function() {
            return output.template.renderString('Hello {{ world }}', { world: 'World'})
                .should.be.fulfilledWith('Hello World');
        });
    });

    describe('Blocks', function() {
        it('should correctly add a block', function() {
            output.template.addBlock('sayhello', function(blk) {
                return 'Hello ' + blk.body + '!';
            });

            return output.template.renderString('{% sayhello %}World{% endsayhello %}')
                .should.be.fulfilledWith('Hello World!');
        });

        it('should correctly add a block with args', function() {
            output.template.addBlock('sayhello_args', function(blk) {
                return 'Hello ' + blk.args[0] + '!';
            });

            return output.template.renderString('{% sayhello_args "World" %}{% endsayhello_args %}')
                .should.be.fulfilledWith('Hello World!');
        });

        it('should correctly add a block with kwargs', function() {
            output.template.addBlock('sayhello_kwargs', function(blk) {
                return 'Hello ' + blk.kwargs.name + '!';
            });

            return output.template.renderString('{% sayhello_kwargs name="World" %}{% endsayhello_kwargs %}')
                .should.be.fulfilledWith('Hello World!');
        });
    });
});