-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
322 lines (262 loc) · 9.16 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'use strict';
require('mocha');
require('should');
var path = require('path');
var assert = require('assert');
var App = require('templates');
var Views = App.Views;
var loader = require('./');
var collection;
var app;
function res(fp) {
return path.resolve(fp);
}
describe('loader', function() {
describe('app.load()', function() {
beforeEach(function() {
app = new App();
});
it('should return a function:', function() {
assert.equal(typeof loader(), 'function');
});
it('should decorate `.load` onto the app instance', function() {
app.use(loader());
var view = app.view({path: 'index.js'});
var collection = app.load('*.js', {cwd: __dirname});
assert(collection.hasOwnProperty(res('index.js')));
});
it('should take options on loader', function() {
app.use(loader({cwd: 'fixtures'}));
var collection = app.load('*.txt');
assert(collection.hasOwnProperty(res('fixtures/a.txt')));
});
it('should take options on load', function() {
app.use(loader());
var collection = app.load('*.txt', {cwd: 'fixtures'});
assert(collection.hasOwnProperty(res('fixtures/a.txt')));
});
it('should use the renameKey options on load', function() {
app.use(loader());
var collection = app.load('*.txt', {
cwd: 'fixtures',
renameKey: function(file) {
return file.path;
}
});
assert(collection.hasOwnProperty(res('fixtures/a.txt')));
});
});
describe('collection.loadViews()', function() {
beforeEach(function() {
app = new App();
app.on('error', function(err) {
console.log(err);
process.exit(1);
});
});
it('should decorate `loadViews` onto collections', function() {
app.use(loader());
app.create('pages');
app.pages.loadViews('*.js');
assert(app.views.pages.hasOwnProperty(res('index.js')));
});
it('should use a cwd defined on the collection options:', function() {
app.use(loader());
app.create('pages')
.option('cwd', 'fixtures')
.loadViews('*.tmpl');
assert(app.views.pages.hasOwnProperty(res('fixtures/a.tmpl')));
});
it('should use a cwd defined on create:', function() {
app.use(loader());
app.create('pages', {cwd: 'fixtures'})
.loadViews('*.tmpl');
assert(app.views.pages.hasOwnProperty(res('fixtures/a.tmpl')));
});
it('should use a cwd defined on create with collection loader', function() {
app.use(loader());
app.create('pages', {cwd: 'fixtures'});
app.pages('*.tmpl');
assert(app.views.pages.hasOwnProperty(res('fixtures/a.tmpl')));
});
});
describe('collection loader function', function() {
beforeEach(function() {
app = new App();
});
it('should work as a collection plugin:', function() {
app.create('pages')
.use(loader('*.json'));
assert(app.views.pages.hasOwnProperty(res('package.json')));
});
it('should decorate `load` on the collection:', function() {
app.create('files')
.use(loader());
app.files.load('*.json');
assert(app.views.files.hasOwnProperty(res('package.json')));
});
it('should be chainable:', function() {
app.create('pages')
.use(loader())
.loadViews('*.json')
.loadViews('*.js');
assert(app.views.pages.hasOwnProperty(res('index.js')));
assert(app.views.pages.hasOwnProperty(res('package.json')));
});
it('should add the `loadViews` method to a collection:', function() {
app.create('pages')
.use(loader());
app.pages.loadViews('fixtures/*.txt');
assert(app.views.pages.hasOwnProperty(res('fixtures/a.txt')));
});
it('collection.loadViews should be chainable', function() {
app.create('pages')
.use(loader())
.loadViews('*.json')
.loadViews('*.js');
assert(app.views.pages.hasOwnProperty(res('index.js')));
assert(app.views.pages.hasOwnProperty(res('package.json')));
});
it('should patch `addViews` to support globs:', function() {
app.create('pages')
.use(loader());
app.pages.addViews('fixtures/*.txt');
assert(app.views.pages.hasOwnProperty(res('fixtures/a.txt')));
});
it('should load globs with app collection methods:', function() {
app.create('pages')
.use(loader());
app.pages('fixtures/*.txt');
assert(app.views.pages.hasOwnProperty(res('fixtures/a.txt')));
});
it('should load glob arrays with app collection methods:', function() {
app.create('pages')
.use(loader());
app.pages(['fixtures/*.txt']);
assert(app.views.pages.hasOwnProperty(res('fixtures/a.txt')));
});
it('should not change native behavior with addView:', function() {
app.create('pages')
.use(loader());
app.page('a', {content: '...'});
app.page('b', {content: '...'});
app.page('c', {content: '...'});
assert(app.views.pages.hasOwnProperty('a'));
assert(app.views.pages.hasOwnProperty('b'));
assert(app.views.pages.hasOwnProperty('c'));
});
it('should maintain backwards compatibility with addViews:', function() {
app.create('pages')
.use(loader());
app.pages({
a: {contents: '...'},
b: {contents: '...'},
c: {contents: '...'}
});
assert(app.views.pages.hasOwnProperty('a'));
assert(app.views.pages.hasOwnProperty('b'));
assert(app.views.pages.hasOwnProperty('c'));
});
it('should get the contents for a view:', function() {
app.create('pages')
.use(loader());
app.pages(['fixtures/*.txt']);
var page = app.pages.getView('fixtures/a.txt');
assert.equal(page.contents.toString(), 'This is AAA');
});
it('should take a custom renameKey function on the options:', function() {
app.create('pages')
.use(loader());
app.pages.option('renameKey', function(key, file) {
return path.basename(key);
});
app.pages.loadViews('fixtures/*.txt');
assert(app.views.pages.hasOwnProperty('a.txt'));
});
it('should keep collection options separate:', function() {
app.use(loader());
app.create('pages');
app.create('partials', {viewType: 'partial'});
app.create('layouts', {viewType: 'layout'});
app.on('page', function(view, type) {
assert.equal(view.options.viewType.length, 1);
assert.equal(view.options.viewType[0], 'renderable');
});
app.on('layout', function(view, type) {
assert.equal(view.options.viewType.length, 1);
assert.equal(view.options.viewType[0], 'layout');
});
app.on('partial', function(view, type) {
assert.equal(view.options.viewType.length, 1);
assert.equal(view.options.viewType[0], 'partial');
});
app.pages.option('renameKey', function(key, file) {
return path.basename(key);
});
app.layouts.option('renameKey', function(key, file) {
return path.basename(key);
});
app.partials.option('renameKey', function(key, file) {
return path.basename(key);
});
app.pages('fixtures/*.txt');
app.layouts('fixtures/*.hbs');
app.partials('fixtures/*.tmpl');
assert(app.views.pages.hasOwnProperty('a.txt'));
});
});
describe('loadView', function() {
beforeEach(function() {
collection = new Views({
renameKey: function(key) {
return path.basename(key);
}
});
collection.use(loader());
});
it('should load a file and add it to `views`:', function() {
collection.loadView('fixtures/a.tmpl');
collection.views.should.have.property('a.tmpl');
});
it('should use a cwd defined on the collection:', function() {
collection.option('cwd', 'fixtures');
collection.loadView('a.tmpl');
collection.views.should.have.property('a.tmpl');
});
it('should handle files with no extension:', function() {
collection.loadView('LICENSE');
collection.views.should.have.property('LICENSE');
});
});
describe('loadViews', function() {
beforeEach(function() {
collection = new Views({
renameKey: function(key) {
return path.basename(key);
}
});
collection.use(loader());
});
it('should load a view from a filepath:', function() {
collection.loadViews('fixtures/a.tmpl');
collection.views.should.have.property('a.tmpl');
});
it('should load views from a glob:', function() {
collection.loadViews('fixtures/*.tmpl');
collection.views.should.have.property('a.tmpl');
});
it('should load views an array globs:', function() {
collection.loadViews(['fixtures/*.tmpl']);
collection.views.should.have.property('a.tmpl');
});
it('should use a cwd defined on the collection:', function() {
collection.option('cwd', 'fixtures');
collection.loadViews('*.tmpl');
collection.views.should.have.property('a.tmpl');
});
it('should handle files with no extension:', function() {
collection.loadViews('LICENSE');
collection.views.should.have.property('LICENSE');
});
});
});