forked from assemble/assemble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.collection.js
179 lines (152 loc) · 5.05 KB
/
app.collection.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
'use strict';
require('mocha');
require('should');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var define = require('define-property');
var support = require('./support');
var App = support.resolve();
var Collection = App.Collection;
var app;
describe('app.collection', function() {
describe('method', function() {
beforeEach(function() {
app = new App();
});
it('should expose the collection method', function() {
assert(typeof app.collection === 'function');
});
it('should return a new collection', function() {
var collection = app.collection();
assert(typeof collection === 'object');
});
it('should have isCollection property', function() {
var collection = app.collection();
assert(collection.isCollection === true);
});
});
describe('adding views', function() {
beforeEach(function() {
app = new App()
.use(function() {
return function() {
define(this, 'count', {
get: function() {
return Object.keys(this.views).length;
},
set: function() {
throw new Error('count is a read-only getter and cannot be defined.');
}
});
};
});
app.engine('tmpl', require('engine-base'));
app.create('pages');
});
it('should load a view onto the respective collection:', function() {
app.pages('test/fixtures/pages/a.hbs');
app.views.pages.should.have.property(path.resolve('test/fixtures/pages/a.hbs'));
});
it('should allow collection methods to be chained:', function() {
app
.pages('test/fixtures/pages/a.hbs')
.pages('test/fixtures/pages/b.hbs')
.pages('test/fixtures/pages/c.hbs');
app.views.pages.should.have.properties([
path.resolve('test/fixtures/pages/a.hbs'),
path.resolve('test/fixtures/pages/b.hbs'),
path.resolve('test/fixtures/pages/c.hbs')
]);
});
it('should expose the `option` method:', function() {
app.pages.option('foo', 'bar')
.pages('test/fixtures/pages/a.hbs')
.pages('test/fixtures/pages/b.hbs')
.pages('test/fixtures/pages/c.hbs');
app.pages.options.should.have.property('foo', 'bar');
app.views.pages.should.have.properties([
path.resolve('test/fixtures/pages/a.hbs'),
path.resolve('test/fixtures/pages/b.hbs'),
path.resolve('test/fixtures/pages/c.hbs')
]);
});
it('should expose the `option` method:', function() {
app.pages.option('foo', 'bar')
.pages('test/fixtures/pages/a.hbs')
.pages('test/fixtures/pages/b.hbs')
.pages('test/fixtures/pages/c.hbs');
assert(app.pages.count === 3);
});
});
describe('addItem', function() {
beforeEach(function() {
app = new App();
});
it('should add items to a collection', function() {
var pages = app.collection({Collection: Collection});
pages.addItem('foo');
pages.addItem('bar');
pages.addItem('baz');
pages.items.hasOwnProperty('foo');
pages.items.hasOwnProperty('bar');
pages.items.hasOwnProperty('baz');
});
it('should create a collection from an existing collection:', function() {
var pages = app.collection({Collection: Collection});
pages.addItem('foo');
pages.addItem('bar');
pages.addItem('baz');
var posts = app.collection(pages);
posts.items.hasOwnProperty('foo');
posts.items.hasOwnProperty('bar');
posts.items.hasOwnProperty('baz');
});
});
describe('rendering views', function() {
beforeEach(function() {
app = new App();
app.engine('tmpl', require('engine-base'));
app.create('pages');
app.cache.data = {};
});
it('should render a view with inherited app.render', function(cb) {
app.page('test/fixtures/templates/a.tmpl')
.use(function(view) {
view.contents = fs.readFileSync(view.path);
})
.set('data.name', 'Brian')
.render(function(err, res) {
if (err) return cb(err);
assert(res.content === 'Brian');
cb();
});
});
});
});
describe('collection singular method', function() {
describe('create', function() {
beforeEach(function() {
app = new App();
});
it('should add a pluralized collection from singular name', function() {
app.create('page');
assert(typeof app.views.pages === 'object');
});
});
describe('adding views', function() {
beforeEach(function() {
app = new App();
app.engine('tmpl', require('engine-base'));
app.create('page');
});
it('should add a view to the created collection:', function() {
app.page('test/fixtures/pages/a.hbs');
assert(typeof app.views.pages[path.resolve('test/fixtures/pages/a.hbs')] === 'object');
});
it('should expose the `option` method:', function() {
app.pages.option('foo', 'bar');
app.pages.options.should.have.property('foo', 'bar');
});
});
});