UNPKG

6.34 kBJavaScriptView Raw
1var t = require('../test-lib/test.js');
2var assert = require('assert');
3var request = require('request');
4
5var apos;
6
7describe('custom-pages', function() {
8
9 this.timeout(t.timeout);
10
11 after(function(done) {
12 return t.destroy(apos, done);
13 });
14
15 /// ///
16 // EXISTENCE
17 /// ///
18
19 it('should initialize', function(done) {
20 apos = require('../index.js')({
21 root: module,
22 shortName: 'test',
23
24 modules: {
25 'apostrophe-express': {
26 secret: 'xxx',
27 port: 7900
28 },
29 'nifty-pages': {
30 extend: 'apostrophe-custom-pages'
31 }
32 },
33 afterInit: function(callback) {
34 // In tests this will be the name of the test file,
35 // so override that in order to get apostrophe to
36 // listen normally and not try to run a task. -Tom
37 apos.argv._ = [];
38 return callback(null);
39 },
40 afterListen: function(err) {
41 assert(!err);
42 done();
43 }
44 });
45 });
46
47 it('should fire a dispatch route for its homepage', function(done) {
48 var niftyPages = apos.modules['nifty-pages'];
49 niftyPages.dispatch('/', function(req, callback) {
50 req.handlerInvoked = true;
51 req.template = function(req, args) {
52 return 'niftyPages-index';
53 };
54 return setImmediate(callback);
55 });
56 // Simulate a page request
57 var req = {
58 data: {
59 bestPage: {
60 type: 'nifty-page'
61 }
62 },
63 remainder: '/'
64 };
65 // pageServe method normally invoked via callAll in
66 // the pages module
67 niftyPages.pageServe(req, function(err) {
68 assert(!err);
69 assert(req.handlerInvoked);
70 done();
71 });
72 });
73
74 it('should fire a dispatch route matching a second, longer URL', function(done) {
75 var niftyPages = apos.modules['nifty-pages'];
76 niftyPages.dispatch('/foo', function(req, callback) {
77 req.fooInvoked = true;
78 return setImmediate(callback);
79 });
80 // Simulate a page request
81 var req = {
82 data: {
83 bestPage: {
84 type: 'nifty-page'
85 }
86 },
87 remainder: '/foo'
88 };
89 // pageServe method normally invoked via callAll in
90 // the pages module
91 niftyPages.pageServe(req, function(err) {
92 assert(!err);
93 assert(req.fooInvoked);
94 done();
95 });
96 });
97
98 it('should fire a dispatch route with parameters', function(done) {
99 var niftyPages = apos.modules['nifty-pages'];
100 niftyPages.dispatch('/bar/:bizzle/:kapow/*', function(req, callback) {
101 req.barInvoked = true;
102 return setImmediate(callback);
103 });
104 // Simulate a page request
105 var req = {
106 data: {
107 bestPage: {
108 type: 'nifty-page'
109 }
110 },
111 remainder: '/bar/wacky/wonky/wibble/skip'
112 };
113 // pageServe method normally invoked via callAll in
114 // the pages module
115 niftyPages.pageServe(req, function(err) {
116 assert(!err);
117 assert(req.barInvoked);
118 assert(req.params.bizzle === 'wacky');
119 assert(req.params.kapow === 'wonky');
120 assert(req.params[0] === 'wibble/skip');
121 done();
122 });
123 });
124
125 it('should allow a later call to dispatch to override an earlier dispatch route', function(done) {
126 var niftyPages = apos.modules['nifty-pages'];
127 niftyPages.dispatch('/foo', function(req, callback) {
128 req.foo2Invoked = true;
129 req.template = function(req, args) {
130 return 'niftyPages-foo';
131 };
132 return setImmediate(callback);
133 });
134 // Simulate a page request
135 var req = {
136 data: {
137 bestPage: {
138 type: 'nifty-page'
139 }
140 },
141 remainder: '/foo'
142 };
143 // pageServe method normally invoked via callAll in
144 // the pages module
145 niftyPages.pageServe(req, function(err) {
146 assert(!err);
147 assert(req.foo2Invoked);
148 assert(!req.fooInvoked);
149 done();
150 });
151 });
152
153 it('should not match when page type is wrong', function(done) {
154 var niftyPages = apos.modules['nifty-pages'];
155 // Simulate a page request for the wrong page type
156 var req = {
157 data: {
158 bestPage: {
159 type: 'wibble-page'
160 }
161 },
162 remainder: '/foo'
163 };
164 // pageServe method normally invoked via callAll in
165 // the pages module
166 niftyPages.pageServe(req, function(err) {
167 assert(!err);
168 assert(!req.foo2Invoked);
169 done();
170 });
171 });
172
173 it('should not match when there is no bestPage', function(done) {
174 var niftyPages = apos.modules['nifty-pages'];
175 // Simulate a page request for the wrong page type
176 var req = {
177 data: {
178 bestPage: null
179 },
180 remainder: '/foo'
181 };
182 // pageServe method normally invoked via callAll in
183 // the pages module
184 niftyPages.pageServe(req, function(err) {
185 assert(!err);
186 assert(!req.foo2Invoked);
187 done();
188 });
189 });
190
191 it('should be able to insert a test page manually into the db', function(done) {
192 var testItem =
193 { _id: 'niftyPages1',
194 type: 'nifty-page',
195 slug: '/niftyPages',
196 published: true,
197 path: '/niftyPages',
198 level: 1,
199 rank: 5
200 };
201 return apos.docs.db.insert(testItem, done);
202 });
203
204 it('should match a dispatch route on a real live page request', function(done) {
205 return request('http://localhost:7900/niftyPages', function(err, response, body) {
206 console.error(err);
207 console.error(body);
208 assert(!err);
209 // Is our status code good?
210 assert.equal(response.statusCode, 200);
211 // Did we get the index output?
212 assert(body.match(/niftyPages-index/));
213 return done();
214 });
215 });
216
217 it('runs foo route with /foo remainder', function(done) {
218 return request('http://localhost:7900/niftyPages/foo', function(err, response, body) {
219 assert(!err);
220 // Is our status code good?
221 assert.equal(response.statusCode, 200);
222 // Did we get the index output?
223 assert(body.match(/niftyPages-foo/));
224 return done();
225 });
226 });
227
228 it('yields 404 with bad remainder (not matching any dispatch routes)', function(done) {
229 return request('http://localhost:7900/niftyPages/tututu', function(err, response, body) {
230 assert(!err);
231 // Is our status code good?
232 assert.equal(response.statusCode, 404);
233 return done();
234 });
235 });
236
237});