UNPKG

pp-parachute

Version:
77 lines (69 loc) 2.85 kB
var Bundler = require('../../bundlers/bundler'); var Promise = require('bluebird'); var path = require('path'); var fs = require('fs'); describe('Bundler', function () { var bundler; beforeEach(function () { bundler = new Bundler('./test_src/**/*.txt'); bundler.setRootPath(__dirname); }); afterEach(function () { bundler.shutdown(); }); it('should only build once', function () { var promise1 = bundler.build(); var promise2 = bundler.build(); expect(promise1).toBe(promise2); }); it('should bundle on fileAdded', function (done) { var newFilePath = __dirname + '/test_src/write_me.txt'; fs.existsSync(newFilePath) && fs.unlinkSync(newFilePath); bundler.watch().then(function () { spyOn(bundler, 'bundle').and.callThrough(); bundler.on('fileAdded', function (filePath) { expect(filePath).toBe(newFilePath); fs.existsSync(newFilePath) && fs.unlinkSync(newFilePath); }); bundler.on('bundleCompleted', function () { expect(bundler.bundle.calls.count()).toBe(1); done(); }); fs.writeFileSync(newFilePath, "content"); }); }); it('should bundle on fileChanged', function (done) { var changedFilePath = __dirname + '/test_src/change_me.txt'; !fs.existsSync(changedFilePath) && fs.writeFileSync(changedFilePath, "" + Math.random()); bundler.watch().then(function () { spyOn(bundler, 'bundle').and.callThrough(); bundler.on('fileChanged', function (filePath) { expect(filePath).toBe(changedFilePath); }); bundler.on('bundleCompleted', function () { expect(bundler.bundle.calls.count()).toBe(1); done(); }); fs.writeFileSync(changedFilePath, "" + Math.random()); }); }); it('should bundle on fileRemoved', function (done) { var removedFilePath = __dirname + '/test_src/remove_me.txt'; fs.writeFile(removedFilePath, "" + Math.random(), function () { bundler.watch().then(function () { spyOn(bundler, 'bundle').and.callThrough(); bundler.on('fileRemoved', function (filePath) { expect(filePath).toBe(removedFilePath); }); bundler.on('bundleCompleted', function () { expect(bundler.bundle.calls.count()).toBe(1); done(); }); //file system needs a break, this throws errors if the timeout isnt here setTimeout(function () { fs.unlink(removedFilePath); }, 25); }); }); }); });