-
Notifications
You must be signed in to change notification settings - Fork 632
/
loader.js
115 lines (96 loc) · 3.28 KB
/
loader.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
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
(function() {
'use strict';
var expect,
Environment,
WebLoader,
FileSystemLoader,
templatesPath;
if (typeof require !== 'undefined') {
expect = require('expect.js');
Environment = require('../nunjucks/src/environment').Environment;
WebLoader = require('../nunjucks/src/web-loaders').WebLoader;
FileSystemLoader = require('../nunjucks/src/node-loaders').FileSystemLoader;
templatesPath = 'tests/templates';
} else {
expect = window.expect;
Environment = nunjucks.Environment;
WebLoader = nunjucks.WebLoader;
FileSystemLoader = nunjucks.FileSystemLoader;
templatesPath = '../templates';
}
describe('loader', function() {
it('should allow a simple loader to be created', function() {
// From Docs: http://mozilla.github.io/nunjucks/api.html#writing-a-loader
// We should be able to create a loader that only exposes getSource
var env, parent;
function MyLoader() {
// configuration
}
MyLoader.prototype.getSource = function() {
return {
src: 'Hello World',
path: '/tmp/somewhere'
};
};
env = new Environment(new MyLoader(templatesPath));
parent = env.getTemplate('fake.njk');
expect(parent.render()).to.be('Hello World');
});
it('should catch loader error', function(done) {
// From Docs: http://mozilla.github.io/nunjucks/api.html#writing-a-loader
// We should be able to create a loader that only exposes getSource
var env;
function MyLoader() {
// configuration
this.async = true;
}
MyLoader.prototype.getSource = function(s, cb) {
setTimeout(function() {
cb(new Error('test'));
}, 1);
};
env = new Environment(new MyLoader(templatesPath));
env.getTemplate('fake.njk', function(err, parent) {
expect(err).to.be.a(Error);
expect(parent).to.be(undefined);
done();
});
});
describe('WebLoader', function() {
it('should have default opts for WebLoader', function() {
var webLoader = new WebLoader(templatesPath);
expect(webLoader).to.be.a(WebLoader);
expect(webLoader.useCache).to.be(false);
expect(webLoader.async).to.be(false);
});
it('should emit a "load" event', function(done) {
var loader = new WebLoader(templatesPath);
if (typeof window === 'undefined') {
this.skip();
}
loader.on('load', function(name, source) {
expect(name).to.equal('simple-base.njk');
done();
});
loader.getSource('simple-base.njk');
});
});
if (typeof FileSystemLoader !== 'undefined') {
describe('FileSystemLoader', function() {
it('should have default opts', function() {
var loader = new FileSystemLoader(templatesPath);
expect(loader).to.be.a(FileSystemLoader);
expect(loader.noCache).to.be(false);
});
it('should emit a "load" event', function(done) {
var loader = new FileSystemLoader(templatesPath);
loader.on('load', function(name, source) {
expect(name).to.equal('simple-base.njk');
done();
});
loader.getSource('simple-base.njk');
});
});
}
});
}());