UNPKG

3 kBJavaScriptView Raw
1var fs = require('fs');
2var path = require('path');
3var should = require('should');
4var gutil = require('gulp-util');
5var twig = require('../');
6
7require('mocha');
8
9describe('gulp-twig', function () {
10
11 it('should compile twig templates to html files', function (done) {
12 var twg = twig({
13 data: {
14 title: 'twig'
15 }
16 });
17
18 var fakeFile = new gutil.File({
19 base: 'test/',
20 cwd: 'test/',
21 path: path.join(__dirname, '/templates/file.twig'),
22 contents: fs.readFileSync(__dirname + '/templates/file.twig')
23 });
24
25 twg.on('data', function (newFile) {
26 should.exist(newFile);
27 should.exist(newFile.contents);
28 should.exist(newFile.path);
29 String(newFile.contents).should.equal(fs.readFileSync(__dirname + '/expected/file.html', 'utf8'));
30 done();
31 });
32 twg.write(fakeFile);
33 });
34
35 it('should compile twig templates to html files without options', function (done) {
36 var twg = twig();
37
38 var fakeFile = new gutil.File({
39 base: 'test/',
40 cwd: 'test/',
41 path: path.join(__dirname, '/templates/file.twig'),
42 contents: fs.readFileSync(__dirname + '/templates/file.twig')
43 });
44
45 twg.on('data', function (newFile) {
46 should.exist(newFile);
47 should.exist(newFile.contents);
48 should.exist(newFile.path);
49 String(newFile.contents).should.equal(fs.readFileSync(__dirname + '/expected/file-noopts.html', 'utf8'));
50 done();
51 });
52 twg.write(fakeFile);
53 });
54
55 it('should return \'null\' file when no file put in', function (done) {
56 var twg = twig();
57
58 var fakeFile = new gutil.File({
59 base: 'test/',
60 cwd: 'test/'
61 });
62
63 twg.on('data', function (newFile) {
64 should.exist(newFile);
65 should.not.exist(newFile.contents);
66 should.not.exist(newFile.path);
67 String(newFile.contents).should.equal('null');
68 done();
69 });
70 twg.write(fakeFile);
71 });
72
73 it('should accept data from file.data', function (done) {
74 var twg = twig();
75
76 var fakeFile = new gutil.File({
77 base: 'test/',
78 cwd: 'test/',
79 path: path.join(__dirname, '/templates/file.twig'),
80 contents: fs.readFileSync(__dirname + '/templates/file.twig'),
81 });
82
83 // simulate data attribute being added by gulp-data plugin
84 fakeFile['data'] = {
85 title: 'twig'
86 };
87
88 twg.on('data', function (newFile) {
89 should.exist(newFile);
90 should.exist(newFile.contents);
91 should.exist(newFile.path);
92 String(newFile.contents).should.equal(fs.readFileSync(__dirname + '/expected/file.html', 'utf8'));
93 done();
94 });
95 twg.write(fakeFile);
96 });
97
98});