-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathview.context.js
227 lines (191 loc) · 6.75 KB
/
view.context.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
'use strict';
/* deps: mocha */
var assert = require('assert');
var should = require('should');
var extend = require('extend-shallow');
var matter = require('parser-front-matter');
var App = require('..');
var app;
describe('context', function () {
beforeEach(function () {
app = new App();
app.engine(['tmpl', 'md'], require('engine-lodash'));
app.create('page');
});
describe('global data', function () {
it('should use global data:', function (done) {
app.data({ title: 'foo'});
app.page('aaa.md', 'a<%= title %>c');
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('afooc');
done();
});
});
it('should prefer "matched" global data over global `data`:', function (done) {
app.data({ title: 'foo'});
app.data({ 'aaa\\.md': {title: 'bar'}});
app.page('aaa.md', 'a<%= title %>c');
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('abarc');
done();
});
});
});
describe('method locals', function () {
it('should pass data to templates in the `.render()` method:', function (done) {
app.data({ abc: 'xyz'});
app.page('aaa.md', '<%= abc %>');
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('xyz');
done();
});
});
it('should pass data to templates in the `.render()` method:', function (done) {
app.data({ title: 'b'});
app.page('aaa.md', 'a<%= title %>c');
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('abc');
done();
});
});
});
describe('view properties', function () {
it('should use view locals:', function (done) {
app.page('aaa.md', 'a<%= title %>c', {locals: {title: 'zzz'}});
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('azzzc');
done();
});
});
it('should use view data:', function (done) {
app.page('aaa.md', 'a<%= title %>c', {title: 'zzz'});
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('azzzc');
done();
});
});
it('should use view data from front-matter:', function (done) {
app.onLoad(/\.md$/, function (view, next) {
matter.parse(view, next);
});
app.page('aaa.md', '---\ntitle: Front Matter\n---\na<%= title %>c');
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('aFront Matterc');
done();
});
});
it('should prefer front-matter over explicitly defined data:', function (done) {
app.onLoad(/\.md$/, function (view, next) {
matter.parse(view, next);
});
app.page('aaa.md', '---\ntitle: Front Matter\n---\na<%= title %>c', { title: 'Data'});
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('aFront Matterc');
done();
});
});
it('should prefer front-matter over explicitly defined locals:', function (done) {
app.onLoad(/\.md$/, function (view, next) {
matter.parse(view, next);
});
app.page('aaa.md', '---\ntitle: Front Matter\n---\na<%= title %>c', {locals: { title: 'Data'}});
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('aFront Matterc');
done();
});
});
it('should prefer front-matter over explicitly defined locals:', function (done) {
app.onLoad(/\.md$/, function (view, next) {
matter.parse(view, next);
});
app.page('bbb.md', {content: '---\ntitle: Front Matter\n---\na<%= title %>c', locals: { title: 'Data'}});
app.render('bbb.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('aFront Matterc');
done();
});
});
it('should prefer view data over view locals:', function (done) {
app.page('aaa.md', {content: 'a<%= title %>c', title: 'aaa', locals: {title: 'zzz'}});
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('aaaac');
done();
});
});
it('should prefer `data` over `locals`:', function (done) {
app.data({ title: 'b'});
app.data({ 'aaa\\.md': {title: 'zzz'}});
app.page('aaa.md', 'a<%= title %>c', { title: 'bbb'});
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('abbbc');
done();
});
});
it('should give preference to front matter when `prefer locals` is disabled:', function (done) {
app.data({ title: 'b'});
app.disable('prefer locals');
app.onLoad(/\.md$/, function (view, next) {
matter.parse(view, next);
});
var page = app.page('aaa.md', '---\ntitle: zzz\n---\na<%= title %>c', { title: 'bbb'});
app.render('aaa.md', function (err, res) {
if (err) return console.log(err);
res.content.should.equal('azzzc');
done();
});
});
});
describe('helpers', function () {
});
describe('context method', function () {
it('should expose `.context` for calculating the context of a view:', function (done) {
app.pages('foo.tmpl', {path: 'foo.tmpl', content: '<%= a %>'});
var page = app.pages.get('foo.tmpl');
assert.equal(typeof page.context, 'function');
done();
});
it('should build the context for a view:', function (done) {
app.data({title: 'home'});
app.page('a.tmpl', {
path: 'a.tmpl',
content: '<%= a %><%= title %>',
a: 'this is '
});
app.render(app.pages.get('a.tmpl'), function (err, res) {
if (err) return done(err);
res.content.should.equal('this is home');
done();
});
});
});
describe('custom', function () {
it('should use a custom `context` function:', function (done) {
app.data({ title: 'b'});
app.onLoad(/\.md$/, function (view, next) {
matter.parse(view, next);
});
app.page('aaa.md', '---\ntitle: zzz\n---\na<%= title %>c', { title: 'bbb'});
var page = app.pages.get('aaa.md');
page.context(function (data, contexts) {
contexts.locals.title = contexts.data.title;
return this;
});
app.render(page, function (err, res) {
if (err) return console.log(err);
res.content.should.equal('azzzc');
done();
});
});
});
});