UNPKG

2.36 kBJavaScriptView Raw
1var assert = require('assert'),
2 path = require('path');
3
4var cjs = require('../index');
5
6describe('connect-jade-static', function() {
7
8 it('should raise error when insufficient params provided', function() {
9 assert.throws(function() {
10 cjs({ baseUrl: '/' });
11 });
12
13 assert.throws(function() {
14 cjs({ baseDir: '/' });
15 });
16
17 });
18
19 it('should serve jade file', function(done) {
20 var mw = cjs({ baseUrl: '/views', baseDir: path.join(__dirname, 'views') });
21
22 var req = { originalUrl: '/views/tpl.html' };
23 var res = { send: function(html) {
24 // otherwise jade tries to catch this error :/
25 process.nextTick(function() {
26 assert.equal(html, '<h1>Hello</h1><ul><li>aaa</li><li>bbb</li><li>ccc</li></ul>');
27 done();
28 });
29 }};
30 var next = function(err) {
31 throw new Error('Code shouldn\'t reach here');
32 };
33
34 mw(req, res, next);
35 });
36
37 it('should support jade options', function(done) {
38 var mw = cjs({ baseUrl: '/views', baseDir: path.join(__dirname, 'views'), jade: { pretty: true } });
39
40 var req = { originalUrl: '/views/tpl.html' };
41 var res = { send: function(html) {
42 // otherwise jade tries to catch this error :/
43 process.nextTick(function() {
44 assert.equal(html,
45 '\n<h1>Hello</h1>\n' +
46 '<ul>\n' +
47 ' <li>aaa</li>\n' +
48 ' <li>bbb</li>\n' +
49 ' <li>ccc</li>\n' +
50 '</ul>');
51 done();
52 });
53 }};
54 var next = function(err) {
55 throw new Error('Code shouldn\'t reach here');
56 };
57
58 mw(req, res, next);
59 });
60
61 it('should call next for unknown file', function(done) {
62 var mw = cjs({ baseUrl: '/views', baseDir: path.join(__dirname, 'views') });
63
64 var req = { originalUrl: '/views/blah.html' };
65 var res = { send: function(html) {
66 throw new Error('Code shouldn\'t reach here');
67 }};
68
69 mw(req, res, done);
70 });
71
72 it('should raise error if jade template invalid', function(done) {
73 var mw = cjs({ baseUrl: '/views', baseDir: path.join(__dirname, 'views') });
74
75 var req = { originalUrl: '/views/tpl_err.html' };
76 var res = { send: function(html) {
77 throw new Error('Code shouldn\'t reach here');
78 }};
79 var next = function(err) {
80 assert.ok(err instanceof TypeError);
81 done();
82 };
83
84 mw(req, res, next);
85 });
86
87});