handlebars-layouts.e2e.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* eslint-env mocha */
  2. 'use strict';
  3. var handlebarsLayouts = require('../index'),
  4. handlebars = require('handlebars'),
  5. expect = require('expect'),
  6. fs = require('fs'),
  7. path = require('path'),
  8. through = require('through2'),
  9. vinylFs = require('vinyl-fs'),
  10. config = {
  11. partials: path.join(__dirname, 'fixtures/partials/'),
  12. fixtures: path.join(__dirname, 'fixtures/templates/'),
  13. expected: path.join(__dirname, 'expected/templates/'),
  14. actual: path.join(__dirname, 'actual/templates/')
  15. };
  16. describe('handlebars-layouts e2e', function () {
  17. var hbs;
  18. function read() {
  19. return fs.readFileSync(path.join.apply(path, arguments), 'utf8');
  20. }
  21. function testWithFile(filename, data, done) {
  22. var fixture = config.fixtures + filename,
  23. expected = config.expected + filename;
  24. function compileFile(file, enc, cb) {
  25. var template;
  26. try {
  27. template = hbs.compile(String(file.contents));
  28. file.contents = new Buffer(template(data));
  29. this.push(file);
  30. cb();
  31. }
  32. catch (err) {
  33. cb(err);
  34. }
  35. }
  36. function expectFile(file) {
  37. expect(String(file.contents)).toBe(read(expected));
  38. done();
  39. }
  40. function expectError(err) {
  41. expect(err.message).toContain('derp');
  42. done();
  43. }
  44. vinylFs
  45. .src(fixture)
  46. .pipe(through.obj(compileFile))
  47. // .pipe(vinylFs.dest(config.actual))
  48. .on('data', expectFile)
  49. .on('error', expectError);
  50. }
  51. beforeEach(function () {
  52. hbs = handlebars.create();
  53. handlebarsLayouts.register(hbs);
  54. // Register partials
  55. hbs.registerPartial({
  56. 'deep-a': read(config.partials, 'deep-a.hbs'),
  57. 'deep-b': read(config.partials, 'deep-b.hbs'),
  58. 'deep-c': read(config.partials, 'deep-c.hbs'),
  59. 'parent-context': read(config.partials, 'parent-context.hbs'),
  60. context: read(config.partials, 'context.hbs'),
  61. layout2col: read(config.partials, 'layout2col.hbs'),
  62. layout: read(config.partials, 'layout.hbs'),
  63. media: read(config.partials, 'media.hbs'),
  64. user: read(config.partials, 'user.hbs')
  65. });
  66. });
  67. it('should extend layouts', function (done) {
  68. var data = require('./fixtures/data/users.json');
  69. testWithFile('extend.html', data, done);
  70. });
  71. it('should deeply extend layouts', function (done) {
  72. testWithFile('deep-extend.html', {}, done);
  73. });
  74. it('should embed layouts', function (done) {
  75. var data = require('./fixtures/data/users.json');
  76. testWithFile('embed.html', data, done);
  77. });
  78. it('should preserve context', function (done) {
  79. testWithFile('context.html', {root: 'root'}, done);
  80. });
  81. it('should append content', function (done) {
  82. testWithFile('append.html', { title: 'append' }, done);
  83. });
  84. it('should prepend content', function (done) {
  85. testWithFile('prepend.html', { title: 'prepend' }, done);
  86. });
  87. it('should replace content', function (done) {
  88. testWithFile('replace.html', { title: 'replace' }, done);
  89. });
  90. it('should ignore bogus content', function (done) {
  91. testWithFile('bogus.html', { title: 'bogus' }, done);
  92. });
  93. it('should pass through hash values', function (done) {
  94. var data = require('./fixtures/data/users.json');
  95. testWithFile('hash.html', data, done);
  96. });
  97. it('should pass through non-object values', function (done) {
  98. var data = Object.create(null);
  99. data.key = 'value';
  100. testWithFile('non-object.html', data, done);
  101. });
  102. it('should throw an error if partial is not registered', function (done) {
  103. testWithFile('error.html', {}, done);
  104. });
  105. });