SpellingAPIManagerTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* eslint-disable
  2. handle-callback-err
  3. */
  4. const sinon = require('sinon')
  5. const { expect } = require('chai')
  6. const SandboxedModule = require('sandboxed-module')
  7. const modulePath = require('path').join(
  8. __dirname,
  9. '../../../app/js/SpellingAPIManager'
  10. )
  11. const promiseStub = val => new Promise(resolve => resolve(val))
  12. describe('SpellingAPIManager', function () {
  13. beforeEach(function () {
  14. this.token = 'user-id-123'
  15. this.ASpell = {}
  16. this.SpellingAPIManager = SandboxedModule.require(modulePath, {
  17. requires: {
  18. './ASpell': this.ASpell,
  19. '@overleaf/settings': { ignoredMisspellings: ['ShareLaTeX'] },
  20. },
  21. })
  22. })
  23. describe('runRequest', function () {
  24. beforeEach(function () {
  25. this.nonLearnedWords = ['some', 'words', 'htat', 'are', 'speled', 'rong']
  26. this.allWords = this.nonLearnedWords
  27. this.misspellings = [
  28. { index: 2, suggestions: ['that'] },
  29. { index: 4, suggestions: ['spelled'] },
  30. { index: 5, suggestions: ['wrong', 'ring'] },
  31. ]
  32. this.misspellingsWithoutLearnedWords = this.misspellings.slice(0, 3)
  33. this.ASpell.checkWords = (lang, word, callback) => {
  34. callback(null, this.misspellings)
  35. }
  36. this.ASpell.promises = {
  37. checkWords: sinon.stub().returns(promiseStub(this.misspellings)),
  38. }
  39. sinon.spy(this.ASpell, 'checkWords')
  40. })
  41. describe('with sensible JSON', function () {
  42. beforeEach(function (done) {
  43. this.SpellingAPIManager.runRequest(
  44. this.token,
  45. { words: this.allWords },
  46. (error, result) => {
  47. if (error) return done(error)
  48. this.result = result
  49. done()
  50. }
  51. )
  52. })
  53. it('should return the words that are spelled incorrectly and not learned', function () {
  54. expect(this.result.misspellings).to.deep.equal(
  55. this.misspellingsWithoutLearnedWords
  56. )
  57. })
  58. })
  59. describe('with a missing words array', function () {
  60. beforeEach(function (done) {
  61. this.SpellingAPIManager.runRequest(this.token, {}, (error, result) => {
  62. this.error = error
  63. this.result = result
  64. done()
  65. })
  66. })
  67. it('should return an error', function () {
  68. expect(this.error).to.exist
  69. expect(this.error).to.be.instanceof(Error)
  70. expect(this.error.message).to.equal('malformed JSON')
  71. })
  72. })
  73. describe('without a language', function () {
  74. beforeEach(function (done) {
  75. this.SpellingAPIManager.runRequest(
  76. this.token,
  77. { words: this.allWords },
  78. (error, result) => {
  79. if (error) return done(error)
  80. this.result = result
  81. done()
  82. }
  83. )
  84. })
  85. it('should use en as the default', function () {
  86. this.ASpell.promises.checkWords.calledWith('en').should.equal(true)
  87. })
  88. })
  89. describe('with a language', function () {
  90. beforeEach(function (done) {
  91. this.language = 'fr'
  92. this.SpellingAPIManager.runRequest(
  93. this.token,
  94. {
  95. words: this.allWords,
  96. language: this.language,
  97. },
  98. (error, result) => {
  99. if (error) return done(error)
  100. this.result = result
  101. done()
  102. }
  103. )
  104. })
  105. it('should use the language', function () {
  106. this.ASpell.promises.checkWords
  107. .calledWith(this.language)
  108. .should.equal(true)
  109. })
  110. })
  111. })
  112. })