render.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. var koa = require('koa');
  2. var hbs = require('..');
  3. var assert = require('assert');
  4. var request = require('supertest');
  5. var testApp = require('./app');
  6. describe('without required options', function () {
  7. it('should throw an error when viewPath is not set', function () {
  8. assert.throws(function () { hbs.create().middleware({}); });
  9. });
  10. });
  11. describe('rendering', function() {
  12. var app;
  13. it('should render into the response body', function (done) {
  14. app = testApp.create({
  15. viewPath: __dirname + '/app/assets'
  16. });
  17. request(app.listen())
  18. .get('/')
  19. .expect(200)
  20. .end(function (err, content) {
  21. if(err) return done(err);
  22. assert.ok(/<title>test<\/title>/.test(content.text));
  23. assert.ok(/html/.test(content.text));
  24. done();
  25. });
  26. });
  27. describe('with an empty template', function () {
  28. it('should render a blank page', function (done) {
  29. app = testApp.create({
  30. viewPath: __dirname + '/app/assets'
  31. });
  32. request(app.listen())
  33. .get('/empty')
  34. .expect(200)
  35. .end(function (err, page) {
  36. if(err) return done(err);
  37. assert.deepEqual(page.text, '');
  38. done();
  39. });
  40. });
  41. });
  42. describe('with partials', function () {
  43. it('should render into the response body', function (done) {
  44. app = testApp.create({
  45. viewPath: __dirname + '/app/assets',
  46. partialsPath: __dirname + '/app/assets/partials'
  47. });
  48. request(app.listen())
  49. .get('/partials')
  50. .expect(200)
  51. .end(function (err, content) {
  52. if(err) return done(err);
  53. assert.ok(/google\.com/.test(content.text));
  54. done();
  55. });
  56. });
  57. it('should work also for nested partials', function (done) {
  58. request(app.listen())
  59. .get('/nestedPartials')
  60. .expect(200)
  61. .end(function (err, content) {
  62. assert.ok(/NESTED/.test(content.text));
  63. done();
  64. });
  65. });
  66. });
  67. describe('when using layouts', function () {
  68. var app;
  69. before(function () {
  70. // Create app which specifies layouts
  71. app = testApp.create({
  72. viewPath: __dirname + '/app/assets',
  73. partialsPath: __dirname + '/app/assets/partials',
  74. layoutsPath: __dirname + '/app/assets/layouts',
  75. defaultLayout: 'default'
  76. });
  77. });
  78. describe('with the default layout', function () {
  79. it('should insert rendered content', function (done) {
  80. request(app.listen())
  81. .get('/layout')
  82. .expect(200)
  83. .end(function (err, content) {
  84. if(err) return done(err);
  85. assert.ok(/DEFAULT LAYOUT/.test(content.text));
  86. assert.ok(/DEFAULT CONTENT/.test(content.text));
  87. done();
  88. });
  89. });
  90. it('should support alternative layouts', function (done) {
  91. request(app.listen())
  92. .get('/altLayout')
  93. .expect(200)
  94. .end(function (err, content) {
  95. if(err) return done(err);
  96. assert.ok(/ALTERNATIVE LAYOUT/.test(content.text));
  97. assert.ok(/ALTERNATIVE CONTENT/.test(content.text));
  98. done();
  99. });
  100. });
  101. });
  102. describe('with block content', function() {
  103. it('should show default without content for', function (done) {
  104. request(app.listen())
  105. .get('/blockNoReplace')
  106. .expect(200)
  107. .end(function (err, content) {
  108. assert.ok(/DEFAULT BLOCK CONTENT/.test(content.text));
  109. assert.ok(/NO BLOCK/.test(content.text));
  110. done();
  111. });
  112. });
  113. it('should replace block content with contentFor', function (done) {
  114. request(app.listen())
  115. .get('/block')
  116. .expect(200)
  117. .end(function (err, content) {
  118. assert.ok(/CONTENT FOR SIDEBAR/.test(content.text));
  119. assert.ok(/CONTENT IN THE BODY/.test(content.text));
  120. done();
  121. });
  122. });
  123. });
  124. });
  125. describe('when using locals', function () {
  126. var app;
  127. before(function () {
  128. // Create app which specifies layouts
  129. app = testApp.create({
  130. viewPath: __dirname + '/app/assets',
  131. partialsPath: __dirname + '/app/assets/partials',
  132. layoutsPath: __dirname + '/app/assets/layouts',
  133. locals: {
  134. title: 'Foo'
  135. }
  136. });
  137. });
  138. it('should not overflow the call stack when recursive', function (done) {
  139. request(app.listen())
  140. .get('/localsRecursive')
  141. .expect(200)
  142. .end(function (err, content) {
  143. if(err) {
  144. return done(err);
  145. }
  146. done();
  147. });
  148. });
  149. it('should render "Foo"', function (done) {
  150. request(app.listen())
  151. .get('/locals')
  152. .expect(200)
  153. .end(function (err, content) {
  154. assert.ok(/Foo/.test(content.text));
  155. done();
  156. });
  157. });
  158. it('should render "Bar"', function (done) {
  159. request(app.listen())
  160. .get('/localsOverride')
  161. .expect(200)
  162. .end(function (err, content) {
  163. assert.ok(/Bar/.test(content.text));
  164. done();
  165. });
  166. });
  167. });
  168. });
  169. describe('var conflict', function () {
  170. var app = koa();
  171. app.use(hbs.middleware({
  172. viewPath: __dirname + '/app/assets'
  173. }));
  174. app.use(function * () {
  175. if (this.url === '/first') {
  176. yield this.render('locals', {
  177. title: 'hbs'
  178. });
  179. return;
  180. }
  181. if (this.url === '/second') {
  182. yield this.render('locals', {
  183. name: 'hbs'
  184. });
  185. return;
  186. }
  187. });
  188. var server;
  189. before(function () {
  190. server = app.listen();
  191. });
  192. it('should render title', function (done) {
  193. request(server)
  194. .get('/first')
  195. .expect(200)
  196. .end(function (err, content) {
  197. assert.equal(content.text, '<h1>hbs</h1>');
  198. done();
  199. });
  200. });
  201. it('should not have title', function (done) {
  202. request(server)
  203. .get('/second')
  204. .expect(200)
  205. .end(function (err, content) {
  206. assert.equal(content.text, '<h1></h1>');
  207. done();
  208. });
  209. });
  210. });