1 | var t = require('../test-lib/test.js');
|
2 | var assert = require('assert');
|
3 | var async = require('async');
|
4 | var request = require('request');
|
5 |
|
6 | describe('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 |
|
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 |
|
81 |
|
82 |
|
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 |
|
126 |
|
127 |
|
128 |
|
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 |
|
139 |
|
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 |
|
166 | assert.equal(response.statusCode, 200);
|
167 |
|
168 |
|
169 | assert(body.match(/Event 005/));
|
170 | assert(body.match(/Event 006/));
|
171 | assert(body.match(/Event 007/));
|
172 |
|
173 |
|
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 |
|
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 |
|
187 | assert(!body.match(/Event 056/));
|
188 |
|
189 |
|
190 | assert(!body.match(/Event 001/));
|
191 | assert(!body.match(/Event 030/));
|
192 | done();
|
193 | });
|
194 | });
|
195 |
|
196 | });
|
197 |
|
198 | describe('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 |
|
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 |
|
283 |
|
284 |
|
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 |
|
328 |
|
329 |
|
330 |
|
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 |
|
341 |
|
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 |
|
368 | assert.equal(response.statusCode, 200);
|
369 |
|
370 |
|
371 | assert(body.match(/Event 005/));
|
372 | assert(body.match(/Event 006/));
|
373 | assert(body.match(/Event 007/));
|
374 |
|
375 |
|
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 |
|
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 |
|
389 | assert(!body.match(/Event 056/));
|
390 |
|
391 |
|
392 | assert(!body.match(/Event 001/));
|
393 | assert(!body.match(/Event 030/));
|
394 |
|
395 |
|
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 |
|
401 | assert(i3 < i7);
|
402 | assert(i4 < i7);
|
403 | assert(i3 < i4);
|
404 |
|
405 | done();
|
406 | });
|
407 | });
|
408 |
|
409 | });
|