-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu-spec.js
331 lines (277 loc) · 11.7 KB
/
menu-spec.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
323
324
325
326
327
328
329
330
331
'use strict';
/*eslint-env mocha*/
/*jshint expr: true*/
/*jshint multistr: true*/
var path = require('path');
var chai = require('chai');
var expect = chai.expect;
var Menu = require('../lib/menu.js');
var MenuItem = require('../lib/menuitem.js');
var assemble = require('assemble');
var app, menu;
describe('Menu', function () {
it('should exist', function () {
expect(Menu).to.be.an('function');
});
it('should return an object', function () {
var menu = new Menu({});
expect(menu).to.be.an('object');
});
describe('Menu instance', function () {
var menu = new Menu({});
it('should have an `items` array', function () {
expect(menu.items).to.exist;
expect(menu.items).to.be.an('array');
});
});
describe('addItem method', function () {
beforeEach(function () {
// setup assemble
app = assemble();
if (!app.pages) {
app.create('pages');
}
menu = new Menu({
cwd: '/',
base: '/'
});
});
it('should accept a menuItem and add it to its own', function () {
var page = app.page('index.hbs', {path: 'index.hbs', contents: new Buffer('a')});
var mi = new MenuItem(page);
expect(menu.items.length).to.equal(0);
menu.addItem(mi);
expect(menu.items.length).to.equal(1);
//console.log('menu items', menu.items);
});
it('should place index pages at front of array', function () {
/* create menu items */
var aboutPage = app.page('about.hbs', {path: 'about.hbs', contents: new Buffer('a')});
var aMi = new MenuItem(aboutPage);
// console.log('about', aMi);
var indexPage = app.page('index.hbs', {path: 'index.hbs', contents: new Buffer('a')});
var iMi = new MenuItem(indexPage);
// console.log('index', iMi);
/* add them to menu */
menu.addItem(aMi);
// console.log('about page only', menu.items);
menu.addItem(iMi);
// console.log('index first?', menu.items);
expect(menu.items[0].title).to.equal('index');
expect(menu.items.length).to.equal(2);
});
it('should make index pages into directories', function () {
var page = app.page('index.hbs', {path: 'index.hbs', contents: new Buffer('a')});
var mi = new MenuItem(page);
menu.addItem(mi);
var productsPage = app.page('products/index.hbs', {path: 'products/index.hbs', contents: new Buffer('a')});
var pMi = new MenuItem(productsPage);
menu.addItem(pMi);
var computersPage = app.page('products/computers.hbs', {path: 'products/computers.hbs', contents: new Buffer('a')});
var cMi = new MenuItem(computersPage);
menu.addItem(cMi);
//console.log(menu.items);
expect(menu.items[1].items[0].title).to.equal('computers');
});
it('should create a parent item when one does not exist', function () {
// var productsPage = app.page('products/index.hbs', {path: 'products/index.hbs', contents: new Buffer('a')});
// var pMi = new MenuItem(productsPage);
//menu.addItem(pMi);
var computersPage = app.page('products/computers.hbs', {cwd: '/', base: '/', path: 'products/computers.hbs', contents: new Buffer('a')});
var cMi = new MenuItem(computersPage);
menu.addItem(cMi);
//console.log(JSON.stringify(menu.items[0], null, '\t'));
expect(menu.items.length).to.equal(1);
});
it('should work with real files', function () {
app.set('cwd', path.join(app.get('cwd'), 'test/mocks/src'));
//console.log('cwd', app.get('cwd'));
var page = app.page('media/news/releases/july-press-release.hbs');
var mi = new MenuItem(page);
menu.addItem(mi);
var page2 = app.page('media/news/releases/june-press-release.hbs');
var mi2 = new MenuItem(page2);
menu.addItem(mi2);
// console.log(JSON.stringify(menu.items[0], null, '\t'));
expect(menu.items[0].title).to.equal('media');
expect(menu.items[0].items[0].title).to.equal('news');
expect(menu.items[0].items[0].items[0].title).to.equal('releases');
expect(menu.items[0].items[0].items[0].items.length).to.equal(2);
expect(menu.items[0].items[0].items[0].items[0].title).to.equal('july-press-release');
});
it('should use a custom menuPath when one is specified', function () {
var homePage = app.page('index.hbs', {path: 'index.hbs',
contents: new Buffer('---\ntitle: home\nmenu-title: default menu home\n--- a')
});
var hMi = new MenuItem(homePage);
var mediaPage = app.page('index.hbs', {path: 'media/news/latest-news.hbs',
contents: new Buffer('---\nmenu-path: news/latest \n---\na')
});
var mMi = new MenuItem(mediaPage);
/* add items to menu */
menu.addItem(hMi);
menu.addItem(mMi);
// console.log(JSON.stringify(menu.items, null, '\t'));
/* test the menu */
expect(menu.items[1].title).to.equal('news');
expect(menu.items[1].items[0].title).to.equal('latest-news');
expect(menu.items[1].items[0].url).to.equal('/media/news/latest-news.html');
});
it('should handle items that arrive out of order, and merge in stubs', function () {
var homePage = app.page('index.hbs', {path: 'index.hbs',
contents: new Buffer('---\ntitle: home\nmenu-title: default menu home\n--- a')
});
var hMi = new MenuItem(homePage);
var newsPage = app.page('index.hbs', {path: 'media/news/latest-news.hbs',
contents: new Buffer('---\nmenu-title: latest \n---\na')
});
var nMi = new MenuItem(newsPage);
/* add items to menu */
menu.addItem(hMi);
menu.addItem(nMi);
// console.log(JSON.stringify(menu.items, null, '\t'));
expect(menu.items[1].title).to.equal('media');
/* create media item and add to menu */
var mediaPage = app.page('index.hbs', {path: 'media/index.hbs',
contents: new Buffer('---\nmenu-title: Media Content \n---\na')
});
var mMi = new MenuItem(mediaPage);
menu.addItem(mMi);
/* test the menu */
// console.log(JSON.stringify(menu.items, null, '\t'));
expect(menu.items[1].title).to.equal('Media Content');
expect(menu.items[1].items[0].title).to.equal('news');
expect(menu.items[1].items[0].items[0].title).to.equal('latest');
});
it('should merge in details of menuItems with identical locations', function () {
var homePage = app.page('index.hbs', {path: 'index.hbs',
contents: new Buffer('---\ntitle: home\nmenu-title: first menu home\n--- a')
});
var hMi = new MenuItem(homePage);
menu.addItem(hMi);
expect(menu.items[0].title).to.equal('first menu home');
var secondPage = app.page('index.hbs', {path: 'index.hbs',
contents: new Buffer('---\ntitle: home\nmenu-title: second menu home\n--- a')
});
var sMi = new MenuItem(secondPage);
menu.addItem(sMi);
expect(menu.items[0].title).to.equal('second menu home');
});
it('should highlight path when menuItem.isCurrentPage is set to true', function () {
/* setup initial menu */
var newsPage = app.page('index.hbs', {path: 'media/news/latest-news.hbs',
contents: new Buffer('---\nmenu-title: latest \n---\na')
});
var nMi = new MenuItem(newsPage);
menu.addItem(nMi);
/* page not in activePath */
var aboutPage = app.page('about.hbs', {path: 'about.hbs',
contents: new Buffer('---\nmenu-title: about \n---\na')
});
var aMi = new MenuItem(aboutPage);
menu.addItem(aMi);
expect(menu.items[0].items[0].items[0].isCurrentPage).to.be.false;
/* now set isActive to highlight path */
var newsView = app.page('index.hbs', {path: 'media/news/latest-news.hbs',
contents: new Buffer('---\nmenu-title: latest \n---\na')
});
var miActive = new MenuItem(newsView);
miActive.isCurrentPage = true;
menu.addItem(miActive);
// console.log(JSON.stringify(menu.items, null, '\t'));
expect(menu.items[0].isActive, 'media.isActive').to.be.true;
expect(menu.items[0].items[0].isActive, 'news.isActive').to.be.true;
expect(menu.items[0].items[0].items[0].isActive, 'latest-news.isActive').to.be.false;
expect(menu.items[0].items[0].items[0].isCurrentPage, 'latest-news.isCurrentPage').to.be.true;
expect(menu.items[1].isActive, 'about.isActive').to.be.false;
});
it('should highlight and merge-in root elements too', function () {
// setup nav
var aboutPage = app.page('about.hbs', {path: 'about.hbs',
contents: new Buffer('---\nmenu-title: about \n---\na')
});
var aMi = new MenuItem(aboutPage);
menu.addItem(aMi);
var homePage = app.page('index.hbs', {path: 'index.hbs',
contents: new Buffer('---\ntitle: home\nmenu-title: first menu home\n--- a')
});
var hMi = new MenuItem(homePage);
menu.addItem(hMi);
// create highlighted home page
var indexHomePage = app.page('index.hbs', {path: 'index.hbs',
contents: new Buffer('---\ntitle: home\nmenu-title: first menu home\n--- a')
});
var iMi = new MenuItem(indexHomePage);
iMi.isCurrentPage = true;
menu.addItem(iMi);
// examine menuItems
//console.log(JSON.stringify(menu.items, null, '\t'));
expect(menu.items[0].isCurrentPage, 'media.isCurrentPage').to.be.true;
});
it('should group siblings under same parent item', function () {
var formerPage = app.page('artists/former.hbs', {path: 'artists/former.hbs',
contents: new Buffer('---\nmenu-title: The Artist Formerly Known \n---\na')
});
var fMi = new MenuItem(formerPage);
menu.addItem(fMi);
var dodgerPage = app.page('artists/dodger.hbs', {path: 'artists/dodger.hbs',
contents: new Buffer('---\nmenu-title: Artfull Dodger \n---\na')
});
var dMi = new MenuItem(dodgerPage);
menu.addItem(dMi);
var artistsPage = app.page('artists/index.hbs', {path: 'artists/index.hbs',
contents: new Buffer('---\nmenu-title: Artists \n---\na')
});
var aMi = new MenuItem(artistsPage);
menu.addItem(aMi);
//console.log(JSON.stringify(menu.items, null, '\t'));
expect(menu.items.length).to.equal(1);
expect(menu.items[0].items.length).to.equal(2);
});
});
describe('Clear Menu', function () {
var menu = new Menu({});
it('should exist', function () {
expect(menu.clearMenu).to.be.a('function');
});
it('should delete all menuItems when called', function () {
var page = app.page('index.hbs', {path: 'index.hbs', contents: new Buffer('a')});
var mi = new MenuItem(page);
menu.addItem(mi);
expect(menu.items.length).to.equal(1);
menu.clearMenu();
expect(menu.items.length).to.equal(0);
});
});
// describe('Menu configuration', function () {
// beforeEach(function () {
// // setup assemble
// app = assemble();
// if (!app.pages) {
// app.create('pages');
// }
// });
// it('should have configurable link ordering', function () {
// // configure menu
// menu = new Menu({
// cwd: '/',
// base: '/',
// items: ['About Us', 'Products']
// });
// // insert menu items out of order
// var productsPage = app.page('products.hbs', {path: 'products.hbs',
// contents: new Buffer('')
// });
// var pMi = new MenuItem(productsPage);
// menu.addItem(pMi);
// var aboutPage = app.page('about.hbs', {path: 'about.hbs',
// contents: new Buffer('')
// });
// var aMi = new MenuItem(aboutPage);
// menu.addItem(aMi);
// // test order
// console.log(JSON.stringify(menu.items, null, '\t'));
// expect(menu.items[0].title).to.equal('about');
// });
// });
});