ImageOptimiserTests.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const modulePath = '../../../app/js/ImageOptimiser.js'
  5. const { FailedCommandError } = require('../../../app/js/Errors')
  6. const SandboxedModule = require('sandboxed-module')
  7. describe('ImageOptimiser', function() {
  8. let ImageOptimiser, SafeExec
  9. const sourcePath = '/wombat/potato.eps'
  10. beforeEach(function() {
  11. SafeExec = {
  12. promises: sinon.stub().resolves()
  13. }
  14. ImageOptimiser = SandboxedModule.require(modulePath, {
  15. requires: {
  16. './SafeExec': SafeExec,
  17. 'logger-sharelatex': {
  18. log() {},
  19. err() {},
  20. warn() {}
  21. }
  22. }
  23. })
  24. })
  25. describe('compressPng', function() {
  26. it('should convert the file', function(done) {
  27. ImageOptimiser.compressPng(sourcePath, err => {
  28. expect(err).not.to.exist
  29. expect(SafeExec.promises).to.have.been.calledWith([
  30. 'optipng',
  31. sourcePath
  32. ])
  33. done()
  34. })
  35. })
  36. it('should return the error', function(done) {
  37. SafeExec.promises.rejects('wombat herding failure')
  38. ImageOptimiser.compressPng(sourcePath, err => {
  39. expect(err.toString()).to.equal('wombat herding failure')
  40. done()
  41. })
  42. })
  43. })
  44. describe('when optimiser is sigkilled', function() {
  45. it('should not produce an error', function(done) {
  46. const error = new FailedCommandError('', 'SIGKILL', '', '')
  47. SafeExec.promises.rejects(error)
  48. ImageOptimiser.compressPng(sourcePath, err => {
  49. expect(err).not.to.exist
  50. done()
  51. })
  52. })
  53. })
  54. })