UNPKG

4.67 kBJavaScriptView Raw
1"use strict";
2
3var assert = require('assert');
4var fs = require('fs');
5var File = require('vinyl');
6var inline = require('../index');
7var join = require('path').join;
8var viewFunction = function (path) {
9 return path;
10};
11
12
13describe('gulp-inline-ng2-template', function () {
14 it('should work with default config', function (done) {
15 var paths = {
16 TEST_FILE : './test/fixtures/templates.js',
17 RESULT_EXPECTED: './test/fixtures/result_expected.js',
18 RESULT_ACTUAL : './test/fixtures/result_actual.js'
19 };
20
21 runTest(paths, { base: 'test/fixtures' }, done);
22 });
23
24 it('should work with relative paths', function (done) {
25 var paths = {
26 TEST_FILE : './test/fixtures/templates_relative.js',
27 RESULT_EXPECTED: './test/fixtures/result_expected_relative.js',
28 RESULT_ACTUAL : './test/fixtures/result_actual_relative.js'
29 };
30
31 runTest(paths, { jade: true, useRelativePaths: true }, done);
32 });
33
34 it('should work with default config and one line options', function (done) {
35 var paths = {
36 TEST_FILE : './test/fixtures/templates.js',
37 RESULT_EXPECTED: './test/fixtures/result_expected_one_line.js',
38 RESULT_ACTUAL : './test/fixtures/result_actual_one_line.js'
39 };
40
41 runTest(paths, { base: 'test/fixtures', removeLineBreaks: true }, done);
42 });
43
44 it('should work with default config and templateUrl as a function', function (done) {
45 var paths = {
46 TEST_FILE : './test/fixtures/templates_function.js',
47 RESULT_EXPECTED: './test/fixtures/result_expected_function.js',
48 RESULT_ACTUAL : './test/fixtures/result_actual_function.js'
49 };
50
51 var OPTIONS = {
52 base: 'test/fixtures',
53 templateFunction: viewFunction
54 };
55
56 runTest(paths, OPTIONS, done);
57 });
58
59 it('should work with templates and styles processors', function (done) {
60 var paths = {
61 TEST_FILE : './test/fixtures/templates_processors.js',
62 RESULT_EXPECTED: './test/fixtures/result_expected_processors.js',
63 RESULT_ACTUAL : './test/fixtures/result_actual_processors.js'
64 };
65
66 var OPTIONS = {
67 base: 'test/fixtures',
68 useRelative: true,
69 templateExtension: 'jade',
70 templateProcessor: function (path, file, cb) {
71 return cb(null, require('jade').render(file));
72 },
73 styleProcessor: function (path, file, cb) {
74 return cb(null, require('stylus').render(file));
75 }
76 };
77
78 runTest(paths, OPTIONS, done);
79 });
80
81 it('should work with templates and styles with backticks in them', function(done) {
82 var paths = {
83 TEST_FILE : './test/fixtures/templates_unescaped.js',
84 RESULT_EXPECTED: './test/fixtures/result_expected_unescaped.js',
85 RESULT_ACTUAL : './test/fixtures/result_actual_unescaped.js'
86 };
87
88 runTest(paths, { base: 'test/fixtures' }, done);
89 });
90
91 it('should work when templateUrl and styleUrl files do not exist', function(done) {
92 var paths = {
93 TEST_FILE : './test/fixtures/templates_nonexistent_files.js',
94 RESULT_EXPECTED: './test/fixtures/result_expected_nonexistent_files.js',
95 RESULT_ACTUAL : './test/fixtures/result_actual_nonexistent_files.js'
96 };
97
98 runTest(paths, { supportNonExistentFiles: true }, done);
99 });
100
101 it('should work with different quote marks', function (done) {
102 var paths = {
103 TEST_FILE : './test/fixtures/templates_quotemark.js',
104 RESULT_EXPECTED: './test/fixtures/result_expected_quotemark.js',
105 RESULT_ACTUAL : './test/fixtures/result_actual_quotemark.js'
106 };
107
108 runTest(paths, { base: 'test/fixtures' }, done);
109 });
110
111 it('should work with template function and different quote marks', function (done) {
112 var paths = {
113 TEST_FILE : './test/fixtures/templates_quotemark_function.js',
114 RESULT_EXPECTED: './test/fixtures/result_expected_quotemark_function.js',
115 RESULT_ACTUAL : './test/fixtures/result_actual_quotemark_function.js'
116 };
117
118 var OPTIONS = {
119 base: 'test/fixtures',
120 templateFunction: viewFunction
121 };
122
123 runTest(paths, OPTIONS, done);
124 });
125});
126
127
128
129function runTest(paths, pluginOptions, done) {
130 var jsFile = new File({
131 contents: new Buffer(fs.readFileSync(paths.TEST_FILE).toString()),
132 path: join(process.cwd(), 'test/fixtures/someSrcFile')
133 });
134
135 var stream = inline(pluginOptions);
136 stream.write(jsFile);
137
138 stream.once('data', function(file) {
139 var result = file.contents.toString();
140 // Save the result in a file.
141 fs.writeFileSync(paths.RESULT_ACTUAL, result, 'utf8');
142
143 assert.equal(
144 result,
145 fs.readFileSync(paths.RESULT_EXPECTED).toString()
146 );
147 done();
148 });
149}