ASpellWorkerTests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import sinon from 'sinon'
  2. import { expect } from 'chai'
  3. import esmock from 'esmock'
  4. import EventEmitter from 'events'
  5. describe('ASpellWorker', function () {
  6. beforeEach(async function () {
  7. this.pipe = {
  8. stdout: new EventEmitter(),
  9. stderr: { on: sinon.stub() },
  10. stdin: { on: sinon.stub() },
  11. on: sinon.stub(),
  12. pid: 12345,
  13. }
  14. this.pipe.stdout.setEncoding = sinon.stub()
  15. this.child_process = {
  16. spawn: sinon.stub().returns(this.pipe),
  17. }
  18. const { ASpellWorker } = await esmock('../../../app/js/ASpellWorker', {
  19. '@overleaf/metrics': {
  20. gauge() {},
  21. inc() {},
  22. },
  23. child_process: this.child_process,
  24. })
  25. this.ASpellWorker = ASpellWorker
  26. })
  27. describe('creating a worker', function () {
  28. beforeEach(function () {
  29. this.worker = new this.ASpellWorker('en')
  30. })
  31. describe('with normal aspell output', function () {
  32. beforeEach(function () {
  33. this.callback = this.worker.callback = sinon.stub()
  34. this.pipe.stdout.emit('data', '& hello\n')
  35. this.pipe.stdout.emit('data', '& world\n')
  36. this.pipe.stdout.emit('data', 'en\n')
  37. this.pipe.stdout.emit('data', '& goodbye')
  38. })
  39. it('should call the callback', function () {
  40. expect(this.callback.called).to.equal(true)
  41. expect(
  42. this.callback.calledWith(null, '& hello\n& world\nen\n')
  43. ).to.equal(true)
  44. })
  45. })
  46. describe('with the aspell end marker split across chunks', function () {
  47. beforeEach(function () {
  48. this.callback = this.worker.callback = sinon.stub()
  49. this.pipe.stdout.emit('data', '& hello\n')
  50. this.pipe.stdout.emit('data', '& world\ne')
  51. this.pipe.stdout.emit('data', 'n\n')
  52. this.pipe.stdout.emit('data', '& goodbye')
  53. })
  54. it('should call the callback', function () {
  55. expect(this.callback.called).to.equal(true)
  56. expect(
  57. this.callback.calledWith(null, '& hello\n& world\nen\n')
  58. ).to.equal(true)
  59. })
  60. })
  61. describe('with the aspell end marker newline split across chunks', function () {
  62. beforeEach(function () {
  63. this.callback = this.worker.callback = sinon.stub()
  64. this.pipe.stdout.emit('data', '& hello\n')
  65. this.pipe.stdout.emit('data', '& world\n')
  66. this.pipe.stdout.emit('data', 'en')
  67. this.pipe.stdout.emit('data', '\n& 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')).to.equal(
  72. true
  73. )
  74. })
  75. })
  76. describe('with everything split across chunks', function () {
  77. beforeEach(function () {
  78. this.callback = this.worker.callback = sinon.stub()
  79. '& hello\n& world\nen\n& goodbye'.split('').forEach(x => {
  80. this.pipe.stdout.emit('data', x)
  81. })
  82. })
  83. it('should call the callback', function () {
  84. expect(this.callback.called).to.equal(true)
  85. expect(this.callback.calledWith(null, '& hello\n& world\nen')).to.equal(
  86. true
  87. )
  88. })
  89. })
  90. })
  91. })