UNPKG

5.23 kBJavaScriptView Raw
1var should = require('should');
2var fs = require('fs');
3var rimraf = require('rimraf');
4var path = require('path');
5
6var mason = require('../index');
7
8var COMPILED = __dirname + '/compiled';
9
10function emptyCompiled() {
11 rimraf.sync(COMPILED);
12 fs.mkdirSync(COMPILED);
13}
14
15function startsEmpty() {
16 it('starts empty', function() {
17 var files = fs.readdirSync(COMPILED);
18 files.should.be.empty;
19 });
20}
21
22describe('Building', function() {
23
24 describe('JS with uglify', function() {
25 var A1_FILE = __dirname + '/fixtures/public/vendor/a1.js';
26 var A2_FILE = __dirname + '/fixtures/public/vendor/a2.js';
27 var MASON_FILE = __dirname + '/fixtures/mason_js.json';
28 var DEST_FILE = __dirname + '/compiled/a.js';
29 var initialSize, aJs;
30
31 before(emptyCompiled);
32 before(function getInitialSize() {
33 var a1 = fs.statSync(A1_FILE);
34 var a2 = fs.statSync(A2_FILE);
35 initialSize = a1.size + a2.size;
36 });
37
38 describe('/compiled directory', startsEmpty);
39 describe('build', function() {
40 it('should run without error', function() {
41 mason(MASON_FILE).build();
42 });
43 });
44 describe('target a.js', function() {
45 it('should exist', function() {
46 aJs = fs.statSync(DEST_FILE);
47 aJs.isFile().should.equal(true);
48 });
49 it('should have working content', function() {
50 var compiledA = require(DEST_FILE);
51 should.exist(compiledA.a1);
52 should.exist(compiledA.a2);
53 compiledA.a1.should.equal('This is a1');
54 compiledA.a2().should.equal('This is a2');
55 should.not.exist(compiledA.b1);
56 });
57 it('should have been compressed', function() {
58 aJs.size.should.be.lessThan(initialSize);
59 });
60 });
61 describe('target b.js', function() {
62 it('should exist', function() {
63 var bJs = fs.statSync(__dirname + '/compiled/b/index.js');
64 bJs.isFile().should.equal(true);
65 });
66 it('should have uglified content', function() {
67 var compiledB = require(__dirname + '/compiled/b/index.js');
68 should.exist(compiledB.b1);
69 should.exist(compiledB.b2);
70 compiledB.b1.should.equal('This is b1');
71 compiledB.b2().should.equal('This is b2');
72 should.not.exist(compiledB.a1);
73 });
74 });
75 });
76 describe('stylus', function() {
77 var MASON_FILE = __dirname + '/fixtures/mason_stylus.json';
78 var DEST_FILE = __dirname + '/compiled/a.css';
79 var REF_FILE = __dirname + '/fixtures/reference/a.css';
80
81 before(emptyCompiled);
82
83 describe('/compiled directory', startsEmpty);
84 describe('build', function() {
85 it('should run without error', function() {
86 mason(MASON_FILE).build();
87 });
88 });
89 describe('target a.css', function() {
90 it('should exist', function() {
91 var aCss = fs.statSync(DEST_FILE);
92 aCss.isFile().should.equal(true);
93 });
94 it('should have compiled css content', function() {
95 var css = fs.readFileSync(DEST_FILE, 'utf-8');
96 var ref = fs.readFileSync(REF_FILE, 'utf-8');
97 css.should.equal(ref);
98 });
99 });
100 });
101 describe('simple jade file', function() {
102 var MASON_FILE = __dirname + '/fixtures/mason_jade_simple.json';
103 var DEST_FILE = __dirname + '/compiled/a.html';
104 var REF_FILE = __dirname + '/fixtures/reference/a.html';
105
106 before(emptyCompiled);
107
108 describe('/compiled directory', startsEmpty);
109 describe('build', function() {
110 it('should run without error', function() {
111 mason(MASON_FILE).build();
112 });
113 });
114 describe('target viewA', function() {
115 it('should exist', function() {
116 var aHtml = fs.statSync(DEST_FILE);
117 aHtml.isFile().should.equal(true);
118 });
119 it('should have compiled html content', function() {
120 var html = fs.readFileSync(DEST_FILE, 'utf-8');
121 var ref = fs.readFileSync(REF_FILE, 'utf-8');
122 html.should.equal(ref);
123 });
124 });
125 });
126 describe('complex jade (locals, mode, only)', function() {
127 var MASON_FILE = __dirname + '/fixtures/mason_jade_complex.json';
128 var DEST_DIR = __dirname + '/compiled';
129 var DEST_FILE = __dirname + '/compiled/b.html';
130 var REF_FILE = __dirname + '/fixtures/reference/b.html';
131
132 before(emptyCompiled);
133
134 describe('/compiled directory', startsEmpty);
135 describe('build', function() {
136 it('should run without error', function() {
137 mason({
138 file: MASON_FILE,
139 modes: {
140 'a.js': 'debug',
141 'b.js': 'uglify'
142 },
143 only: ['viewB', 'a.js', 'b.js']
144 }).build();
145 });
146 });
147 describe('target viewB', function() {
148 it('should exist', function() {
149 var aHtml = fs.statSync(DEST_FILE);
150 aHtml.isFile().should.equal(true);
151 });
152 it('should have compiled html content', function() {
153 var html = fs.readFileSync(DEST_FILE, 'utf-8');
154 var ref = fs.readFileSync(REF_FILE, 'utf-8');
155 html.should.equal(ref);
156 });
157 });
158 describe('omitted targets', function() {
159 it('should not exist', function() {
160 var aCss = fs.readdirSync(DEST_DIR);
161 aCss.should.eql(['a.js', 'b', 'b.html']);
162 });
163 });
164 });
165});
\No newline at end of file