LearnedWordsManagerTests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const SandboxedModule = require('sandboxed-module')
  4. const modulePath = require('path').join(
  5. __dirname,
  6. '/../../../../app/src/Features/Spelling/LearnedWordsManager'
  7. )
  8. const { InvalidError } = require('../../../../app/src/Features/Errors/Errors')
  9. describe('LearnedWordsManager', function () {
  10. beforeEach(function () {
  11. this.token = 'a6b3cd919ge'
  12. this.callback = sinon.stub()
  13. this.db = {
  14. spellingPreferences: {
  15. updateOne: sinon.stub().yields(),
  16. findOne: sinon.stub().yields(null, ['pear']),
  17. },
  18. }
  19. this.LearnedWordsManager = SandboxedModule.require(modulePath, {
  20. requires: {
  21. '../../infrastructure/mongodb': { db: this.db },
  22. '@overleaf/metrics': {
  23. timeAsyncMethod: sinon.stub(),
  24. inc: sinon.stub(),
  25. },
  26. '@overleaf/settings': {
  27. maxDictionarySize: 20,
  28. },
  29. },
  30. })
  31. })
  32. describe('learnWord', function () {
  33. describe('under size limit', function () {
  34. beforeEach(function () {
  35. this.word = 'instanton'
  36. this.LearnedWordsManager.learnWord(this.token, this.word, this.callback)
  37. })
  38. it('should insert the word in the word list in the database', function () {
  39. expect(
  40. this.db.spellingPreferences.updateOne.calledWith(
  41. {
  42. token: this.token,
  43. },
  44. {
  45. $addToSet: { learnedWords: this.word },
  46. },
  47. {
  48. upsert: true,
  49. }
  50. )
  51. ).to.equal(true)
  52. })
  53. it('should call the callback without error', function () {
  54. sinon.assert.called(this.callback)
  55. expect(this.callback.lastCall.args.length).to.equal(0)
  56. })
  57. })
  58. describe('over size limit', function () {
  59. beforeEach(function () {
  60. this.word = 'superlongwordthatwillgobeyondthelimit'
  61. this.LearnedWordsManager.learnWord(this.token, this.word, this.callback)
  62. })
  63. it('should not insert the word in the word list in the database', function () {
  64. expect(this.db.spellingPreferences.updateOne.notCalled).to.equal(true)
  65. })
  66. it('should call the callback with error', function () {
  67. sinon.assert.called(this.callback)
  68. expect(this.callback.lastCall.args[0]).to.be.instanceof(InvalidError)
  69. })
  70. })
  71. })
  72. describe('unlearnWord', function () {
  73. beforeEach(function () {
  74. this.word = 'instanton'
  75. this.LearnedWordsManager.unlearnWord(this.token, this.word, this.callback)
  76. })
  77. it('should remove the word from the word list in the database', function () {
  78. expect(
  79. this.db.spellingPreferences.updateOne.calledWith(
  80. {
  81. token: this.token,
  82. },
  83. {
  84. $pull: { learnedWords: this.word },
  85. }
  86. )
  87. ).to.equal(true)
  88. })
  89. it('should call the callback', function () {
  90. expect(this.callback.called).to.equal(true)
  91. })
  92. })
  93. describe('getLearnedWords', function () {
  94. beforeEach(function () {
  95. this.wordList = ['apples', 'bananas', 'pears']
  96. this.wordListWithDuplicates = this.wordList.slice()
  97. this.wordListWithDuplicates.push('bananas')
  98. this.db.spellingPreferences.findOne = (conditions, callback) => {
  99. callback(null, { learnedWords: this.wordListWithDuplicates })
  100. }
  101. sinon.spy(this.db.spellingPreferences, 'findOne')
  102. this.LearnedWordsManager.getLearnedWords(this.token, this.callback)
  103. })
  104. it('should get the word list for the given user', function () {
  105. expect(
  106. this.db.spellingPreferences.findOne.calledWith({ token: this.token })
  107. ).to.equal(true)
  108. })
  109. it('should return the word list in the callback without duplicates', function () {
  110. expect(this.callback.calledWith(null, this.wordList)).to.equal(true)
  111. })
  112. })
  113. describe('getLearnedWordsSize', function () {
  114. it('should return the word list size in the callback', function () {
  115. this.db.spellingPreferences.findOne = (conditions, callback) => {
  116. callback(null, {
  117. learnedWords: ['apples', 'bananas', 'pears', 'bananas'],
  118. })
  119. }
  120. this.LearnedWordsManager.getLearnedWordsSize(this.token, this.callback)
  121. sinon.assert.calledWith(this.callback, null, 38)
  122. })
  123. })
  124. describe('deleteUsersLearnedWords', function () {
  125. beforeEach(function () {
  126. this.db.spellingPreferences.deleteOne = sinon.stub().callsArgWith(1)
  127. })
  128. it('should get the word list for the given user', function (done) {
  129. this.LearnedWordsManager.deleteUsersLearnedWords(this.token, () => {
  130. this.db.spellingPreferences.deleteOne
  131. .calledWith({ token: this.token })
  132. .should.equal(true)
  133. done()
  134. })
  135. })
  136. })
  137. })