-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
166 lines (149 loc) · 5.23 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
'use strict';
require('mocha');
var assert = require('assert');
var assemble = require('assemble');
var collection = require('./');
var app;
describe('assemble-collection', function() {
beforeEach(function() {
app = new assemble();
app.use(collection());
});
describe('plugin', function() {
it('should export a function', function() {
assert.equal(typeof collection, 'function');
});
it('should expose a `createIndex`. method', function() {
assert.equal(typeof app.createIndex, 'function');
});
});
describe('.createIndex', function() {
it('should use default `list` and `item` templates when `list` and `item` are undefined', function(cb) {
app.page('one.hbs', {content: '', data: {tags: ['foo', 'bar']}});
var files = [];
app.toStream('pages')
.pipe(app.createIndex('tags'))
.on('data', function(file) {
files.push(file);
})
.once('error', cb)
.on('end', function() {
assert.equal(files.length, 4);
assert.equal(files[0].relative, 'one.hbs');
assert.equal(files[1].relative, 'tags.hbs');
assert.equal(files[2].relative, 'tags/foo.hbs');
assert.equal(files[3].relative, 'tags/bar.hbs');
cb();
});
});
it('should not create new files when source files are not in the collection', function(cb) {
app.page('one.hbs', {content: ''});
var files = [];
app.toStream('pages')
.pipe(app.createIndex('tags'))
.on('data', function(file) {
files.push(file);
})
.once('error', cb)
.on('end', function() {
assert.equal(files.length, 1);
assert.equal(files[0].path, 'one.hbs');
cb();
});
});
it('should create new files when source files are the collection', function(cb) {
app.page('one.hbs', {content: '', data: {tags: ['foo']}});
var files = [];
app.toStream('pages')
.pipe(app.createIndex({
pattern: ':tags/:tag.hbs',
list: app.view({path: 'list.hbs', contents: new Buffer('list')}),
item: app.view({path: 'item.hbs', contents: new Buffer('item')})
}))
.on('data', function(file) {
files.push(file);
})
.once('error', cb)
.on('end', function() {
assert.equal(files.length, 3);
assert.equal(files[0].relative, 'one.hbs');
assert.equal(files[1].relative, 'tags.hbs');
assert.equal(files[2].relative, 'tags/foo.hbs');
cb();
});
});
it('should add the collection to the `app.cache.data.collection`', function(cb) {
app.page('one.hbs', {content: '', data: {tags: ['foo']}});
var files = [];
app.toStream('pages')
.pipe(app.createIndex('tags'))
.on('data', function(file) {
assert.deepEqual(Object.keys(app.cache.data.collection.tags), ['foo']);
assert.equal(app.cache.data.collection.tags.foo[0].relative, 'one.hbs');
files.push(file);
})
.once('error', cb)
.on('end', function() {
assert.equal(files.length, 3);
assert.equal(files[0].relative, 'one.hbs');
cb();
});
});
it('should create new files with the correct source files', function(cb) {
app.page('one.hbs', {contents: new Buffer('one'), data: {tags: ['foo', 'bar']}});
app.page('two.hbs', {contents: new Buffer('two'), data: {tags: ['bar', 'baz']}});
app.page('three.hbs', {contents: new Buffer('three'), data: {tags: ['foo', 'baz']}});
var fixtures = {
'tags.hbs': {
tags: {
foo: ['one.hbs', 'three.hbs'],
bar: ['one.hbs', 'two.hbs'],
baz: ['two.hbs', 'three.hbs']
}
},
'tags/foo.hbs': {
tag: 'foo',
items: ['one.hbs', 'three.hbs']
},
'tags/bar.hbs': {
tag: 'bar',
items: ['one.hbs', 'two.hbs']
},
'tags/baz.hbs': {
tag: 'baz',
items: ['two.hbs', 'three.hbs']
}
};
var files = [];
app.toStream('pages')
.pipe(app.createIndex('tags'))
.on('data', function(file) {
files.push(file);
})
.once('error', cb)
.on('end', function() {
assert.equal(files.length, 7);
assert.equal(files[0].relative, 'one.hbs');
assert.equal(files[1].relative, 'two.hbs');
assert.equal(files[2].relative, 'three.hbs');
files.forEach(function(file) {
var expected = fixtures[file.relative];
if (!expected) return;
if (expected.tags) {
var tags = Object.keys(file.data.tags);
var actual = tags.reduce(function(acc, key) {
acc[key] = file.data.tags[key].map(function(item) { return item.relative; });
return acc;
}, {});
assert.deepEqual(actual, expected.tags);
} else {
var actual = file.data.items.map(function(item) { return item.relative; });
assert.equal(file.data.tag, expected.tag);
assert.deepEqual(actual, expected.items);
}
})
cb();
});
});
});
});