SpellingAPIManagerTests.js 3.2 KB

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