-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
87 lines (72 loc) · 2.77 KB
/
test.js
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
/*!
* async-helper-base <https://github.com/jonschlinkert/async-helper-base>
*
* Copyright (c) 2015 Jon Schlinkert.
* Licensed under the MIT license.
*/
'use strict';
/* deps:mocha */
var assert = require('assert');
var should = require('should');
var Template = require('template');
var helper = require('./');
var template = new Template();
describe('helper', function () {
beforeEach(function (done) {
var orig = process.cwd();
before(function () {
process.chdir(__dirname + '/fixtures');
});
after(function () {
process.chdir(orig);
});
template.engine('*', require('engine-lodash'));
done();
});
describe('errors', function () {
it('should throw an error when `name` is missing:', function () {
(function () {
helper(template);
}).should.throw('async-helper-base expects `name` to be a string.');
});
});
describe('helpers', function () {
it('should create an async helper:', function (done) {
template.create('badge');
template.badge('travis', {content: '[![Build Status](http://img.shields.io/travis/<%= name %>.svg)](https://travis-ci.org/<%= name %>)'})
template.asyncHelper('badge', helper('badge'));
template.render('<%= badge("travis") %>', {name: 'verb'}, function (err, res) {
if (err) console.log(err);
res.should.equal('[![Build Status](http://img.shields.io/travis/verb.svg)](https://travis-ci.org/verb)');
done();
});
});
it('should use globally defined options in the helper:', function (done) {
template.option('helper', {
badge: {cwd: 'fixtures'}
});
// create a custom template type
template.create('badge');
// override the auto-generated `badge` helper with a custom helper
template.asyncHelper('badge', helper('badge'));
// call the badge helper
template.render('<%= badge("aaa") %>', {name: 'verb'}, function (err, res) {
if (err) console.log(err);
res.should.equal('Fixtures!!! [![Build Status](http://img.shields.io/travis/verb.svg)](https://travis-ci.org/verb)');
done();
});
});
it('should take a callback to modify the rendered string:', function (done) {
template.create('badge');
template.badge('travis', {content: '[![Build Status](http://img.shields.io/travis/<%= name %>.svg)](https://travis-ci.org/<%= name %>)'});
template.asyncHelper('badge', helper('badge', function (str, name) {
return str.split('verb').join(name);
}));
template.render('<%= badge("travis", "foo") %>', {name: 'verb'}, function (err, res) {
if (err) console.log(err);
res.should.equal('[![Build Status](http://img.shields.io/travis/foo.svg)](https://travis-ci.org/foo)');
done();
});
});
});
});