pp-parachute
Version:
Airdrop JS Applications
273 lines (248 loc) • 11.2 kB
JavaScript
var Environment = require('../../application/environment');
var Application = require('../../application/application');
var Bundler = require('../../bundlers/bundler');
var Promise = require('bluebird');
var fs = require('fs');
var path = require('path');
describe('Environment', function () {
var environment, outputPath1, outputPath2, outputPath3, bundler1, bundler2, bundler3;
beforeEach(function () {
environment = new Environment('test');
environment.rootPath = __dirname;
outputPath1 = path.resolve(environment.rootPath + '/test_dist/test.txt');
outputPath2 = path.resolve(environment.rootPath + '/test_dist/test.js');
outputPath3 = path.resolve(environment.rootPath + '/test_dist/test.css');
bundler1 = new Bundler('./test_src/**/*.txt');
bundler2 = new Bundler('./test_src/**/*.js');
bundler3 = new Bundler('./test_src/**/*.css');
environment.useBundler(bundler1, 'test.txt');
environment.useBundler(bundler2, 'test.js');
environment.setOutputDirectory('./test_dist');
});
afterEach(function () {
environment.shutdown();
});
//this spec is having issues with unlink... check for file then unlink and suddenly file cant be unliked
//but is still in file system
xdescribe('General', function () {
xit('should output bundled files relative to the output directory', function (done) {
environment.useBundler(bundler1, '/../../test.js');
environment.build().then(function () {
var out = path.resolve(__dirname + '/../test.js');
//expect(fs.existsSync(out)).toBe(true);
fs.exists(out, function(exists) {
expect(exists).toBe(true);
exists && fs.unlink(out);
done();
});
});
});
it('should inherit a bundler from its application', function(done) {
var application = new Application('App1');
application.useEnvironment(environment);
application.useBundler(bundler3, 'test.css');
var random = "" + Math.random();
fs.writeFileSync(__dirname + "/test_src/file1.css", random);
expect(environment.getActiveBundlers()[bundler3.bundlerId]).toBe(bundler3);
environment.build().then(function() {
expect(fs.readFileSync(outputPath3).toString()).toBe(random);
done();
});
});
});
describe('Build', function () {
it('should build a set of bundlers', function (done) {
fs.existsSync(outputPath1) && fs.unlinkSync(outputPath1);
fs.existsSync(outputPath2) && fs.unlinkSync(outputPath2);
environment.build().then(function () {
//timeout for file system not to suck
setTimeout(function() {
var fileBundler1 = fs.readFileSync(outputPath1).toString();
var fileBundler2 = fs.readFileSync(outputPath2).toString();
expect(fileBundler1.indexOf('file1')).not.toBe(-1);
expect(fileBundler1.indexOf('file2') !== -1).toBe(true);
expect(fileBundler2.indexOf('x = 5') !== -1).toBe(true);
expect(fileBundler2.indexOf('y = 10') !== -1).toBe(true);
done();
}, 250);
});
});
it('should emit build start event', function (done) {
var handler = {
buildStarted: function (bundlers) {
expect(bundlers.length).toBe(2);
expect(bundlers[0]).toBe(bundler1);
expect(bundlers[1]).toBe(bundler2);
}
};
spyOn(handler, 'buildStarted').and.callThrough();
environment.handle('buildStarted', handler.buildStarted);
environment.build().then(function () {
expect(handler.buildStarted.calls.count()).toBe(1);
done();
});
});
it('should emit buildCompleted event', function (done) {
bundler2.bundle = function () {
return Promise.reject('error');
};
var handler = {
buildCompleted: function (passedBundlers, failedBundlers) {
expect(passedBundlers.length).toBe(1);
expect(passedBundlers[0]).toBe(bundler1);
expect(passedBundlers.length).toBe(1);
expect(failedBundlers.length).toBe(1);
expect(failedBundlers[0]).toBeDefined();
expect(failedBundlers[0]).toBe(bundler2);
expect(failedBundlers[0].bundleError).toBe('error');
}
};
spyOn(handler, 'buildCompleted').and.callThrough();
environment.handle('buildCompleted', handler.buildCompleted);
environment.build().then(function () {
expect(handler.buildCompleted.calls.count()).toBe(1);
done();
});
});
it('should emit buildSucceeded event', function (done) {
var handler = {
buildSucceeded: function (bundlers) {
expect(bundlers.length).toBe(2);
expect(bundlers[0]).toBe(bundler1);
expect(bundlers[1]).toBe(bundler2);
}
};
spyOn(handler, 'buildSucceeded').and.callThrough();
environment.handle('buildSucceeded', handler.buildSucceeded);
environment.build().then(function () {
expect(handler.buildSucceeded.calls.count()).toBe(1);
done();
});
});
it('should emit buildFailed event', function (done) {
bundler2.bundle = function () {
return Promise.reject('error');
};
var handler = {
buildFailed: function (failedBundlers) {
expect(failedBundlers.length).toBe(1);
expect(failedBundlers[0]).toBeDefined();
expect(failedBundlers[0]).toBe(bundler2);
expect(failedBundlers[0].bundleError).toBe('error');
}
};
spyOn(handler, 'buildFailed').and.callThrough();
environment.handle('buildFailed', handler.buildFailed);
environment.build().then(function () {
expect(handler.buildFailed.calls.count()).toBe(1);
done();
});
});
it('should not build while a build is in progress', function () {
var promise1 = environment.build();
var promise2 = environment.build();
expect(promise1).toBe(promise2);
});
it('should start a second build after the first one finishes', function (done) {
var promise1 = environment.build();
var promise2 = null;
promise1.then(function () {
promise2 = environment.build();
return promise2;
}).then(function () {
expect(promise1).not.toBe(promise2);
done();
})
})
});
describe('Watch', function () {
var tempFilePath = __dirname + '/test_src/file3.txt';
afterEach(function () {
if (fs.existsSync(tempFilePath)) {
try {
fs.unlinkSync(tempFilePath);
}
catch (e) {
}
}
});
it('should emit buildStarted event', function (done) {
var handler = {
buildStarted: function (bundlers) {
expect(bundlers.length).toBe(2);
expect(bundlers[0]).toBe(bundler1);
expect(bundlers[1]).toBe(bundler2);
}
};
spyOn(handler, 'buildStarted').and.callThrough();
environment.handle('buildStarted', handler.buildStarted);
environment.watch().then(function () {
expect(handler.buildStarted.calls.count()).toBe(1);
done();
});
});
it('should emit buildCompleted when initial watch is built', function (done) {
var handler = {
buildCompleted: function (passed, failed) {
expect(passed.length).toBe(2);
expect(passed[0]).toBe(bundler1);
expect(passed[1]).toBe(bundler2);
}
};
spyOn(handler, 'buildCompleted').and.callThrough();
environment.handle('buildCompleted', handler.buildCompleted);
environment.watch().then(function () {
expect(handler.buildCompleted.calls.count()).toBe(1);
done();
});
});
it('should emit buildStarted when a bundle starts to build after initial build', function (done) {
var handler = {
buildStarted: function (bundlers) {
expect(bundlers.length).toBe(2);
expect(bundlers[0]).toBe(bundler1);
expect(bundlers[1]).toBe(bundler2);
}
};
spyOn(handler, 'buildStarted').and.callThrough();
environment.handle('buildStarted', handler.buildStarted);
environment.watch().then(function() {
expect(handler.buildStarted.calls.count()).toBe(1);
done();
})
});
it('should emit buildFailed when a bundle fails after the initial build', function (done) {
var x = {
buildFailed: function (failedBundles) {
expect(failedBundles.length).toBe(1);
expect(failedBundles[0]).toBe(bundler1);
done();
}
};
spyOn(x, 'buildFailed').and.callThrough();
environment.handle('buildFailed', x.buildFailed);
environment.watch().then(function () {
bundler1.bundle = function () {
return Promise.reject('error');
};
expect(x.buildFailed.calls.count()).toBe(0);
fs.writeFileSync(tempFilePath, "file3");
});
});
it('should emit buildSucceeded when subsequent builds succeed', function (done) {
var x = {
buildSucceeded: function (passedBundles) {
expect(passedBundles.length).toBe(2);
expect(passedBundles[0]).toBe(bundler1);
expect(passedBundles[1]).toBe(bundler2);
done();
}
};
spyOn(x, 'buildSucceeded').and.callThrough();
environment.watch().then(function () {
environment.handle('buildSucceeded', x.buildSucceeded);
fs.writeFileSync(tempFilePath, "file3");
});
});
});
});