1 | var t = require('../test-lib/test.js');
|
2 | var assert = require('assert');
|
3 | var Promise = require('bluebird');
|
4 |
|
5 | describe('Promisified Events: apostrophe-docs:beforeInses', function() {
|
6 |
|
7 | this.timeout(50000);
|
8 |
|
9 | after(function(done) {
|
10 | return t.destroy(apos, done);
|
11 | });
|
12 |
|
13 | var apos;
|
14 | var coreEventsWork = false;
|
15 |
|
16 | it('should implement apostrophe-docs:beforeInsert handlers properly', function(done) {
|
17 | apos = require('../index.js')({
|
18 | root: module,
|
19 | shortName: 'test2',
|
20 | modules: {
|
21 | 'test1': {
|
22 | alias: 'test1',
|
23 | construct: function(self, options) {
|
24 | self.on('apostrophe-docs:beforeInsert', 'beforeInsertReverseTitle', function(req, doc, options) {
|
25 | if (doc.type === 'default') {
|
26 | return Promise.delay(50).then(function() {
|
27 | doc.title = doc.title.split('').reverse().join('');
|
28 | });
|
29 | }
|
30 | });
|
31 | self.on('apostrophe:modulesReady', 'modulesReadyCoreEventsWork', function() {
|
32 | coreEventsWork = true;
|
33 | });
|
34 |
|
35 | self.docBeforeInsert = function(req, doc, options, callback) {
|
36 | doc.oldSchool = true;
|
37 | return setImmediate(callback);
|
38 | };
|
39 | }
|
40 | },
|
41 | 'apostrophe-pages': {
|
42 | park: [
|
43 | {
|
44 | type: 'default',
|
45 | findMeAgain: true,
|
46 | title: 'Test',
|
47 | slug: '/test',
|
48 | published: true
|
49 | }
|
50 | ]
|
51 | }
|
52 | },
|
53 | afterInit: function(callback) {
|
54 | apos.argv._ = [];
|
55 | done();
|
56 | }
|
57 | });
|
58 | });
|
59 |
|
60 | it('should find the results', function(done) {
|
61 | return apos.docs.db.findOne({ findMeAgain: true }, function(err, doc) {
|
62 | assert(!err);
|
63 | assert(doc);
|
64 | assert(doc.title === 'tseT');
|
65 | assert(doc.oldSchool);
|
66 | assert(coreEventsWork);
|
67 | done();
|
68 | });
|
69 | });
|
70 | });
|