ASpellTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* eslint-disable
  2. no-undef
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Sanity-check the conversion and remove this comment.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. import { expect, assert } from 'chai'
  12. import esmock from 'esmock'
  13. describe('ASpell', function () {
  14. beforeEach(async function () {
  15. this.ASpell = await esmock('../../../app/js/ASpell', {
  16. '@overleaf/metrics': {
  17. gauge() {},
  18. inc() {},
  19. },
  20. })
  21. })
  22. describe('a correctly spelled word', function () {
  23. beforeEach(function (done) {
  24. return this.ASpell.checkWords('en', ['word'], (error, result) => {
  25. if (error) return done(error)
  26. this.result = result
  27. return done()
  28. })
  29. })
  30. return it('should not correct the word', function () {
  31. return this.result.length.should.equal(0)
  32. })
  33. })
  34. describe('a misspelled word', function () {
  35. beforeEach(function (done) {
  36. return this.ASpell.checkWords('en', ['bussines'], (error, result) => {
  37. if (error) return done(error)
  38. this.result = result
  39. return done()
  40. })
  41. })
  42. return it('should correct the word', function () {
  43. this.result.length.should.equal(1)
  44. return this.result[0].suggestions.indexOf('business').should.not.equal(-1)
  45. })
  46. })
  47. describe('multiple words', function () {
  48. beforeEach(function (done) {
  49. return this.ASpell.checkWords(
  50. 'en',
  51. ['bussines', 'word', 'neccesary'],
  52. (error, result) => {
  53. if (error) return done(error)
  54. this.result = result
  55. return done()
  56. }
  57. )
  58. })
  59. return it('should correct the incorrect words', function () {
  60. this.result[0].index.should.equal(0)
  61. this.result[0].suggestions.indexOf('business').should.not.equal(-1)
  62. this.result[1].index.should.equal(2)
  63. return this.result[1].suggestions
  64. .indexOf('necessary')
  65. .should.not.equal(-1)
  66. })
  67. })
  68. describe('without a valid language', function () {
  69. beforeEach(function (done) {
  70. return this.ASpell.checkWords('notALang', ['banana'], (error, result) => {
  71. this.error = error
  72. this.result = result
  73. return done()
  74. })
  75. })
  76. return it('should return an error', function () {
  77. return expect(this.error).to.exist
  78. })
  79. })
  80. describe('when there are no suggestions', function () {
  81. beforeEach(function (done) {
  82. return this.ASpell.checkWords(
  83. 'en',
  84. ['asdkfjalkdjfadhfkajsdhfashdfjhadflkjadhflajsd'],
  85. (error, result) => {
  86. this.error = error
  87. this.result = result
  88. return done()
  89. }
  90. )
  91. })
  92. return it('should return a blank array', function () {
  93. this.result.length.should.equal(1)
  94. return assert.deepEqual(this.result[0].suggestions, [])
  95. })
  96. })
  97. return describe('when the request times out', function () {
  98. beforeEach(function (done) {
  99. const words = __range__(0, 1000, true).map(i => 'abcdefg' + i)
  100. this.ASpell.setTimeout(1)
  101. this.start = Date.now()
  102. return this.ASpell.checkWords('en', words, (error, result) => {
  103. expect(error).to.exist
  104. this.result = result
  105. return done()
  106. })
  107. })
  108. // Note that this test fails on OS X, due to differing pipe behaviour
  109. // on killing the child process. It can be tested successfully on Travis
  110. // or the CI server.
  111. return it('should return in reasonable time', function () {
  112. const delta = Date.now() - this.start
  113. return delta.should.be.below(1000)
  114. })
  115. })
  116. })
  117. function __range__(left, right, inclusive) {
  118. const range = []
  119. const ascending = left < right
  120. const end = !inclusive ? right : ascending ? right + 1 : right - 1
  121. for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
  122. range.push(i)
  123. }
  124. return range
  125. }