UNPKG

11.1 kBJavaScriptView Raw
1var t = require('../test-lib/test.js');
2var assert = require('assert');
3var async = require('async');
4var request = require('request');
5
6describe('Pieces Widgets', function() {
7
8 var apos;
9
10 this.timeout(t.timeout);
11
12 after(function(done) {
13 return t.destroy(apos, done);
14 });
15
16 // EXISTENCE
17
18 it('should initialize', function(done) {
19 apos = require('../index.js')({
20 root: module,
21 shortName: 'test',
22
23 modules: {
24 'apostrophe-express': {
25 secret: 'xxx',
26 port: 7900
27 },
28 'events': {
29 extend: 'apostrophe-pieces',
30 name: 'event',
31 label: 'Event',
32 alias: 'events',
33 sort: { title: 1 }
34 },
35 'events-widgets': {
36 extend: 'apostrophe-pieces-widgets'
37 },
38 'apostrophe-pages': {
39 types: [
40 {
41 name: 'home',
42 label: 'Home'
43 },
44 {
45 name: 'default',
46 label: 'Default'
47 }
48 ],
49 park: [
50 {
51 title: 'Page With Events Widget',
52 type: 'default',
53 slug: '/page-with-events',
54 published: true,
55 body: {
56 type: 'area',
57 items: [
58 {
59 type: 'events',
60 by: 'id',
61 pieceIds: [
62 'wevent007', 'wevent006', 'wevent005'
63 ]
64 },
65 {
66 type: 'events',
67 by: 'tag',
68 tags: [
69 'tag2', 'madeupfaketag'
70 ],
71 limitByTag: 5
72 }
73 ]
74 }
75 }
76 ]
77 }
78 },
79 afterInit: function(callback) {
80 // In tests this will be the name of the test file,
81 // so override that in order to get apostrophe to
82 // listen normally and not try to run a task. -Tom
83 apos.argv._ = [];
84 return callback(null);
85 },
86 afterListen: function(err) {
87 assert(!err);
88 assert(apos.modules['events-widgets']);
89 done();
90 }
91 });
92 });
93
94 it('should be able to use db to insert test pieces', function(done) {
95 var testItems = [];
96 var total = 100;
97 for (var i = 1; (i <= total); i++) {
98 var paddedInt = apos.launder.padInteger(i, 3);
99 var tags;
100 if (i > 50) {
101 tags = [ 'tag2' ];
102 } else {
103 tags = [ 'tag1' ];
104 }
105 var title = 'Event ' + paddedInt;
106 testItems.push({
107 _id: 'wevent' + paddedInt,
108 slug: 'wevent-' + paddedInt,
109 published: true,
110 type: 'event',
111 title: title,
112 tags: tags,
113 body: {
114 type: 'area',
115 items: [
116 {
117 type: 'apostrophe-rich-text',
118 content: '<p>This is some content.</p>'
119 }
120 ]
121 }
122 });
123 }
124
125 // We need an event that can be distinguished by
126 // something other than a number in order to test
127 // our autocomplete, which feeds through mongo's text
128 // indexes, which don't support numbers
129 paddedInt = 'wiggly';
130 title = 'Event Wiggly';
131 testItems.push({
132 _id: 'weventwiggly' + paddedInt,
133 slug: 'wevent-wiggl' + paddedInt,
134 published: true,
135 type: 'event',
136 title: title,
137 tags: tags,
138 // fake highSearchText and highSearchWords until the
139 // search module is finished
140 highSearchText: apos.utils.sortify(title),
141 highSearchWords: apos.utils.sortify(title).split(/ /),
142 body: {
143 type: 'area',
144 items: [
145 {
146 type: 'apostrophe-rich-text',
147 content: '<p>This is some content.</p>'
148 }
149 ]
150 }
151 });
152 var req = apos.tasks.getReq();
153 return async.eachSeries(testItems, function(item, callback) {
154 return apos.docs.insert(req, item, callback);
155 }, function(err) {
156 assert(!err);
157 done();
158 });
159 });
160
161 it('should find appropriate events and not others in a page containing tag and id-based event widgets', function(done) {
162
163 return request('http://localhost:7900/page-with-events', function(err, response, body) {
164 assert(!err);
165 // Is our status code good?
166 assert.equal(response.statusCode, 200);
167 // Does it contain the right events via a widget?
168
169 assert(body.match(/Event 005/));
170 assert(body.match(/Event 006/));
171 assert(body.match(/Event 007/));
172
173 // Are they in the right order (reversed on purpose)?
174 var i5 = body.indexOf('Event 005');
175 var i6 = body.indexOf('Event 006');
176 var i7 = body.indexOf('Event 007');
177 assert((i5 > i6) && (i6 > i7));
178
179 // These are by tag
180 assert(body.match(/Event 051/));
181 assert(body.match(/Event 052/));
182 assert(body.match(/Event 053/));
183 assert(body.match(/Event 054/));
184 assert(body.match(/Event 055/));
185
186 // Respect limit by tag
187 assert(!body.match(/Event 056/));
188
189 // Does it contain events not associated with the widget?
190 assert(!body.match(/Event 001/));
191 assert(!body.match(/Event 030/));
192 done();
193 });
194 });
195
196});
197
198describe('Pieces Widget With Extra Join', function() {
199
200 var apos;
201
202 this.timeout(t.timeout);
203
204 after(function(done) {
205 return t.destroy(apos, done);
206 });
207
208 // EXISTENCE
209
210 it('should initialize', function(done) {
211 apos = require('../index.js')({
212 root: module,
213 shortName: 'test',
214
215 modules: {
216 'apostrophe-express': {
217 secret: 'xxx',
218 port: 7900
219 },
220 'events': {
221 extend: 'apostrophe-pieces',
222 name: 'event',
223 label: 'Event',
224 alias: 'events',
225 sort: { title: 1 }
226 },
227 'events-widgets': {
228 extend: 'apostrophe-pieces-widgets',
229 addFields: [
230 {
231 name: '_featured',
232 type: 'joinByArray',
233 withType: 'event'
234 }
235 ]
236 },
237 'apostrophe-pages': {
238 types: [
239 {
240 name: 'home',
241 label: 'Home'
242 },
243 {
244 name: 'default',
245 label: 'Default'
246 }
247 ],
248 park: [
249 {
250 title: 'Page With Events Widget',
251 type: 'default',
252 slug: '/page-with-events',
253 published: true,
254 body: {
255 type: 'area',
256 items: [
257 {
258 type: 'events',
259 by: 'id',
260 pieceIds: [
261 'wevent007', 'wevent006', 'wevent005'
262 ],
263 featuredIds: [
264 'wevent003', 'wevent004'
265 ]
266 },
267 {
268 type: 'events',
269 by: 'tag',
270 tags: [
271 'tag2', 'madeupfaketag'
272 ],
273 limitByTag: 5
274 }
275 ]
276 }
277 }
278 ]
279 }
280 },
281 afterInit: function(callback) {
282 // In tests this will be the name of the test file,
283 // so override that in order to get apostrophe to
284 // listen normally and not try to run a task. -Tom
285 apos.argv._ = [];
286 return callback(null);
287 },
288 afterListen: function(err) {
289 assert(!err);
290 assert(apos.modules['events-widgets']);
291 done();
292 }
293 });
294 });
295
296 it('should be able to use db to insert test pieces', function(done) {
297 var testItems = [];
298 var total = 100;
299 for (var i = 1; (i <= total); i++) {
300 var paddedInt = apos.launder.padInteger(i, 3);
301 var tags;
302 if (i > 50) {
303 tags = [ 'tag2' ];
304 } else {
305 tags = [ 'tag1' ];
306 }
307 var title = 'Event ' + paddedInt;
308 testItems.push({
309 _id: 'wevent' + paddedInt,
310 slug: 'wevent-' + paddedInt,
311 published: true,
312 type: 'event',
313 title: title,
314 tags: tags,
315 body: {
316 type: 'area',
317 items: [
318 {
319 type: 'apostrophe-rich-text',
320 content: '<p>This is some content.</p>'
321 }
322 ]
323 }
324 });
325 }
326
327 // We need an event that can be distinguished by
328 // something other than a number in order to test
329 // our autocomplete, which feeds through mongo's text
330 // indexes, which don't support numbers
331 paddedInt = 'wiggly';
332 title = 'Event Wiggly';
333 testItems.push({
334 _id: 'weventwiggly' + paddedInt,
335 slug: 'wevent-wiggl' + paddedInt,
336 published: true,
337 type: 'event',
338 title: title,
339 tags: tags,
340 // fake highSearchText and highSearchWords until the
341 // search module is finished
342 highSearchText: apos.utils.sortify(title),
343 highSearchWords: apos.utils.sortify(title).split(/ /),
344 body: {
345 type: 'area',
346 items: [
347 {
348 type: 'apostrophe-rich-text',
349 content: '<p>This is some content.</p>'
350 }
351 ]
352 }
353 });
354 var req = apos.tasks.getReq();
355 return async.eachSeries(testItems, function(item, callback) {
356 return apos.docs.insert(req, item, callback);
357 }, function(err) {
358 assert(!err);
359 done();
360 });
361 });
362
363 it('should find appropriate events and not others in a page containing tag and id-based event widgets', function(done) {
364
365 return request('http://localhost:7900/page-with-events', function(err, response, body) {
366 assert(!err);
367 // Is our status code good?
368 assert.equal(response.statusCode, 200);
369 // Does it contain the right events via a widget?
370
371 assert(body.match(/Event 005/));
372 assert(body.match(/Event 006/));
373 assert(body.match(/Event 007/));
374
375 // Are they in the right order (reversed on purpose)?
376 var i5 = body.indexOf('Event 005');
377 var i6 = body.indexOf('Event 006');
378 var i7 = body.indexOf('Event 007');
379 assert((i5 > i6) && (i6 > i7));
380
381 // These are by tag
382 assert(body.match(/Event 051/));
383 assert(body.match(/Event 052/));
384 assert(body.match(/Event 053/));
385 assert(body.match(/Event 054/));
386 assert(body.match(/Event 055/));
387
388 // Respect limit by tag
389 assert(!body.match(/Event 056/));
390
391 // Does it contain events not associated with the widget?
392 assert(!body.match(/Event 001/));
393 assert(!body.match(/Event 030/));
394
395 // Does it contain the featured events in the extra join?
396 assert(body.match(/Event 003/));
397 assert(body.match(/Event 004/));
398 var i3 = body.indexOf('Event 003');
399 var i4 = body.indexOf('Event 004');
400 // Are they in the right order and in the right place (before the regular stuff)?
401 assert(i3 < i7);
402 assert(i4 < i7);
403 assert(i3 < i4);
404
405 done();
406 });
407 });
408
409});