ASpellWorkerTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-undef
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Sanity-check the conversion and remove this comment.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon')
  13. const chai = require('chai')
  14. const { expect } = chai
  15. const SandboxedModule = require('sandboxed-module')
  16. const EventEmitter = require('events')
  17. describe('ASpellWorker', function () {
  18. beforeEach(function () {
  19. this.child_process = {}
  20. return (this.ASpellWorker = SandboxedModule.require('../../../app/js/ASpellWorker', {
  21. requires: {
  22. 'logger-sharelatex': {
  23. log() { },
  24. info() { },
  25. err() { }
  26. },
  27. 'metrics-sharelatex': {
  28. gauge() { },
  29. inc() { }
  30. },
  31. 'child_process': this.child_process
  32. }
  33. }))
  34. })
  35. describe("creating a worker", function () {
  36. beforeEach(function () {
  37. this.pipe = {
  38. 'stdout': new EventEmitter(),
  39. 'stderr': { on: sinon.stub() },
  40. 'stdin': {on: sinon.stub() },
  41. 'on': sinon.stub(),
  42. 'pid': 12345
  43. }
  44. this.child_process.spawn = sinon.stub().returns(this.pipe)
  45. this.pipe.stdout.setEncoding = sinon.stub()
  46. worker = new this.ASpellWorker('en')
  47. })
  48. describe("with normal aspell output", function () {
  49. beforeEach(function () {
  50. this.callback = worker.callback = sinon.stub()
  51. this.pipe.stdout.emit('data', '& hello\n')
  52. this.pipe.stdout.emit('data', '& world\n')
  53. this.pipe.stdout.emit('data', 'en\n')
  54. this.pipe.stdout.emit('data', '& goodbye')
  55. })
  56. it('should call the callback', function() {
  57. expect(this.callback.called).to.equal(true)
  58. expect(this.callback.calledWith(null, "& hello\n& world\nen\n")).to.equal(true)
  59. })
  60. })
  61. describe("with the aspell end marker split across chunks", function () {
  62. beforeEach(function () {
  63. this.callback = worker.callback = sinon.stub()
  64. this.pipe.stdout.emit('data', '& hello\n')
  65. this.pipe.stdout.emit('data', '& world\ne')
  66. this.pipe.stdout.emit('data', 'n\n')
  67. this.pipe.stdout.emit('data', '& goodbye')
  68. })
  69. it('should call the callback', function() {
  70. expect(this.callback.called).to.equal(true)
  71. expect(this.callback.calledWith(null, "& hello\n& world\nen\n")).to.equal(true)
  72. })
  73. })
  74. describe("with the aspell end marker newline split across chunks", function () {
  75. beforeEach(function () {
  76. this.callback = worker.callback = sinon.stub()
  77. this.pipe.stdout.emit('data', '& hello\n')
  78. this.pipe.stdout.emit('data', '& world\n')
  79. this.pipe.stdout.emit('data', 'en')
  80. this.pipe.stdout.emit('data', '\n& goodbye')
  81. })
  82. it('should call the callback', function() {
  83. expect(this.callback.called).to.equal(true)
  84. expect(this.callback.calledWith(null, "& hello\n& world\nen")).to.equal(true)
  85. })
  86. })
  87. describe("with everything split across chunks", function () {
  88. beforeEach(function () {
  89. this.callback = worker.callback = sinon.stub()
  90. '& hello\n& world\nen\n& goodbye'.split('').forEach(x => {
  91. this.pipe.stdout.emit('data', x)
  92. })
  93. })
  94. it('should call the callback', function() {
  95. expect(this.callback.called).to.equal(true)
  96. expect(this.callback.calledWith(null, "& hello\n& world\nen")).to.equal(true)
  97. })
  98. })
  99. })
  100. })