1 |
|
2 |
|
3 | 'use strict';
|
4 | var should = require('should'),
|
5 | stream = require('stream'),
|
6 | tools = require('browserify-transform-tools'),
|
7 | stringify = require('../index');
|
8 |
|
9 | describe('the main function called', function () {
|
10 | var input = '<p style="color: grey;" > should be minified </p>',
|
11 | outputTransformed = 'module.exports = \"<p style=\\"color: grey;\\" > should be minified </p>";\n',
|
12 | outputMinified = 'module.exports = \"<p style=\\"color: grey\\">should be minified</p>";\n';
|
13 |
|
14 | function assertFactoryFunctionReturnsOneArgument () {
|
15 | it('should return a factory function that expects one argument', function () {
|
16 | should(this.transformerFactory.length).be.equal(1);
|
17 | });
|
18 | }
|
19 |
|
20 | function assertFactoryFunctionReturnsStreamWhenSuppliedValidFile () {
|
21 | describe('when the returned function is called with a valid file path', function () {
|
22 | before(function () {
|
23 | this.transformer = this.transformerFactory('a_file.xxx');
|
24 | });
|
25 |
|
26 | it('should return a Stream object', function () {
|
27 | should(this.transformer).be.instanceOf(stream.Stream);
|
28 | should(this.transformer.writable).ok;
|
29 | should(this.transformer.readable).ok;
|
30 | this.transformer.write.should.be.a.Function;
|
31 | this.transformer.end.should.be.a.Function;
|
32 | });
|
33 | });
|
34 | }
|
35 |
|
36 | describe('with no options', function () {
|
37 | before(function () {
|
38 | this.transformConfig = {
|
39 | content: input
|
40 | };
|
41 | this.transformerFactory = stringify();
|
42 | });
|
43 |
|
44 | assertFactoryFunctionReturnsOneArgument();
|
45 | assertFactoryFunctionReturnsStreamWhenSuppliedValidFile();
|
46 |
|
47 | it('should respond to input with the default options', function (done) {
|
48 | tools.runTransform(this.transformerFactory, 'a_file.txt', this.transformConfig, function(err, result) {
|
49 | should(err).be.null;
|
50 | should(result).be.equal(outputTransformed);
|
51 | done();
|
52 | });
|
53 | });
|
54 |
|
55 | it('should respond without transformation when should be file skipped', function (done) {
|
56 | tools.runTransform(this.transformerFactory, 'a_file.foo', this.transformConfig, function(err, result) {
|
57 | should(err).be.null;
|
58 | should(result).be.equal(input);
|
59 | done();
|
60 | });
|
61 | });
|
62 | });
|
63 |
|
64 | describe('with options as first argument', function () {
|
65 | before(function () {
|
66 | this.transformConfig = {
|
67 | content: input
|
68 | };
|
69 | this.transformerFactory = stringify({
|
70 | appliesTo: { includeExtensions: ['.xxx'] },
|
71 | minify: true,
|
72 | minifyAppliesTo: {
|
73 | includeExtensions: ['.xxx']
|
74 | }
|
75 | });
|
76 | });
|
77 |
|
78 | assertFactoryFunctionReturnsOneArgument();
|
79 | assertFactoryFunctionReturnsStreamWhenSuppliedValidFile();
|
80 |
|
81 | it('should respond to input with the given options', function (done) {
|
82 | tools.runTransform(this.transformerFactory, 'a_file.xxx', this.transformConfig, function(err, result) {
|
83 | should(err).be.null;
|
84 | should(result).be.equal(outputMinified);
|
85 | done();
|
86 | });
|
87 | });
|
88 |
|
89 | it('should respond without transformation when should be file skipped', function (done) {
|
90 | tools.runTransform(this.transformerFactory, 'a_file.foo', this.transformConfig, function(err, result) {
|
91 | should(err).be.null;
|
92 | should(result).be.equal(input);
|
93 | done();
|
94 | });
|
95 | });
|
96 | });
|
97 |
|
98 | describe('with file as first argument', function () {
|
99 | before(function () {
|
100 | this.transformConfig = {
|
101 | content: input,
|
102 | config: {
|
103 | appliesTo: { includeExtensions: ['.xxx'] }
|
104 | }
|
105 | };
|
106 | });
|
107 |
|
108 | describe('when called with a valid file path and options', function () {
|
109 | before(function () {
|
110 | this.transformer = stringify('a_file', this.transformConfig.config);
|
111 | });
|
112 |
|
113 | it('should return a Stream object', function () {
|
114 | should(this.transformer).be.instanceOf(stream.Stream);
|
115 | should(this.transformer.writable).ok;
|
116 | should(this.transformer.readable).ok;
|
117 | this.transformer.write.should.be.a.Function;
|
118 | this.transformer.end.should.be.a.Function;
|
119 | });
|
120 | });
|
121 |
|
122 | it('should respond to input with the given options', function (done) {
|
123 | tools.runTransform(stringify, 'a_file.xxx', this.transformConfig, function(err, result) {
|
124 | should(err).be.null;
|
125 | should(result).be.equal(outputTransformed);
|
126 | done();
|
127 | });
|
128 | });
|
129 |
|
130 | it('should respond without transformation when should be file skipped', function (done) {
|
131 | tools.runTransform(stringify, 'a_file.foo', this.transformConfig, function(err, result) {
|
132 | should(err).be.null;
|
133 | should(result).be.equal(input);
|
134 | done();
|
135 | });
|
136 | });
|
137 | });
|
138 | });
|