overleaf/services/filestore/test/unit/coffee/ImageOptimiserTests.js

85 lines
2.3 KiB
JavaScript
Raw Normal View History

/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const {
assert
} = require("chai");
const sinon = require('sinon');
const chai = require('chai');
const should = chai.should();
const {
expect
} = chai;
const modulePath = "../../../app/js/ImageOptimiser.js";
const SandboxedModule = require('sandboxed-module');
2014-02-14 11:39:05 -05:00
describe("ImageOptimiser", function() {
2014-02-14 11:39:05 -05:00
beforeEach(function() {
this.child_process =
{exec : sinon.stub()};
this.settings =
{enableConversions:true};
this.optimiser = SandboxedModule.require(modulePath, { requires: {
'child_process': this.child_process,
"logger-sharelatex": {
log() {},
err() {},
warn() {}
},
"settings-sharelatex": this.settings
}
}
);
2014-02-14 11:39:05 -05:00
this.sourcePath = "/this/path/here.eps";
return this.error = "Error";
});
2014-02-14 11:39:05 -05:00
describe("compressPng", function() {
2014-02-14 11:39:05 -05:00
it("convert the file", function(done){
this.child_process.exec.callsArgWith(2);
return this.optimiser.compressPng(this.sourcePath, err=> {
const args = this.child_process.exec.args[0][0];
args.should.equal(`optipng ${this.sourcePath}`);
return done();
});
});
2014-02-14 11:39:05 -05:00
return it("should return the error", function(done){
this.child_process.exec.callsArgWith(2, this.error);
return this.optimiser.compressPng(this.sourcePath, err=> {
err.should.equal(this.error);
return done();
});
});
});
describe('when enableConversions is disabled', () => it('should produce an error', function(done) {
this.settings.enableConversions = false;
this.child_process.exec.callsArgWith(2);
return this.optimiser.compressPng(this.sourcePath, err=> {
this.child_process.exec.called.should.equal(false);
expect(err).to.exist;
return done();
});
}));
return describe('when optimiser is sigkilled', () => it('should not produce an error', function(done) {
this.error = new Error('woops');
this.error.signal = 'SIGKILL';
this.child_process.exec.callsArgWith(2, this.error);
return this.optimiser.compressPng(this.sourcePath, err=> {
expect(err).to.equal(null);
return done();
});
}));
});