1 | "use strict";
|
2 |
|
3 | var assert = require('assert');
|
4 | var fs = require('fs');
|
5 | var File = require('vinyl');
|
6 | var inline = require('../index');
|
7 | var join = require('path').join;
|
8 | var viewFunction = function (path) {
|
9 | return path;
|
10 | };
|
11 |
|
12 |
|
13 | describe('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 |
|
102 |
|
103 |
|
104 | function runTest(paths, pluginOptions, done) {
|
105 | var jsFile = new File({
|
106 | contents: new Buffer(fs.readFileSync(paths.TEST_FILE).toString()),
|
107 | path: join(process.cwd(), 'test/fixtures/someSrcFile')
|
108 | });
|
109 |
|
110 | var stream = inline(pluginOptions);
|
111 | stream.write(jsFile);
|
112 |
|
113 | stream.once('data', function(file) {
|
114 | var result = file.contents.toString();
|
115 |
|
116 | fs.writeFileSync(paths.RESULT_ACTUAL, result, 'utf8');
|
117 |
|
118 | assert.equal(
|
119 | result,
|
120 | fs.readFileSync(paths.RESULT_EXPECTED).toString()
|
121 | );
|
122 | done();
|
123 | });
|
124 | }
|