LearnedWordsManagerTests.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-undef
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Sanity-check the conversion and remove this comment.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon')
  13. const chai = require('chai')
  14. const { expect } = chai
  15. const SandboxedModule = require('sandboxed-module')
  16. const modulePath = require('path').join(
  17. __dirname,
  18. '../../../app/js/LearnedWordsManager'
  19. )
  20. const { assert } = require('chai')
  21. describe('LearnedWordsManager', function() {
  22. beforeEach(function() {
  23. this.token = 'a6b3cd919ge'
  24. this.callback = sinon.stub()
  25. this.db = {
  26. spellingPreferences: {
  27. update: sinon.stub().callsArg(3)
  28. }
  29. }
  30. this.cache = {
  31. get: sinon.stub(),
  32. set: sinon.stub(),
  33. del: sinon.stub()
  34. }
  35. return (this.LearnedWordsManager = SandboxedModule.require(modulePath, {
  36. requires: {
  37. './DB': this.db,
  38. './MongoCache': this.cache,
  39. 'logger-sharelatex': {
  40. log() {},
  41. err() {},
  42. info() {}
  43. },
  44. 'metrics-sharelatex': {
  45. timeAsyncMethod: sinon.stub(),
  46. inc: sinon.stub()
  47. }
  48. }
  49. }))
  50. })
  51. describe('learnWord', function() {
  52. beforeEach(function() {
  53. this.word = 'instanton'
  54. return this.LearnedWordsManager.learnWord(
  55. this.token,
  56. this.word,
  57. this.callback
  58. )
  59. })
  60. it('should insert the word in the word list in the database', function() {
  61. return expect(
  62. this.db.spellingPreferences.update.calledWith(
  63. {
  64. token: this.token
  65. },
  66. {
  67. $push: { learnedWords: this.word }
  68. },
  69. {
  70. upsert: true
  71. }
  72. )
  73. ).to.equal(true)
  74. })
  75. return it('should call the callback', function() {
  76. return expect(this.callback.called).to.equal(true)
  77. })
  78. })
  79. describe('getLearnedWords', function() {
  80. beforeEach(function() {
  81. this.wordList = ['apples', 'bananas', 'pears']
  82. this.db.spellingPreferences.findOne = (conditions, callback) => {
  83. return callback(null, { learnedWords: this.wordList })
  84. }
  85. sinon.spy(this.db.spellingPreferences, 'findOne')
  86. return this.LearnedWordsManager.getLearnedWords(this.token, this.callback)
  87. })
  88. it('should get the word list for the given user', function() {
  89. return expect(
  90. this.db.spellingPreferences.findOne.calledWith({ token: this.token })
  91. ).to.equal(true)
  92. })
  93. return it('should return the word list in the callback', function() {
  94. return expect(this.callback.calledWith(null, this.wordList)).to.equal(
  95. true
  96. )
  97. })
  98. })
  99. describe('caching the result', function() {
  100. it('should use the cache first if it is primed', function(done) {
  101. this.wordList = ['apples', 'bananas', 'pears']
  102. this.cache.get.returns(this.wordList)
  103. this.db.spellingPreferences.findOne = sinon.stub()
  104. return this.LearnedWordsManager.getLearnedWords(
  105. this.token,
  106. (err, spellings) => {
  107. this.db.spellingPreferences.findOne.called.should.equal(false)
  108. assert.deepEqual(this.wordList, spellings)
  109. return done()
  110. }
  111. )
  112. })
  113. it('should set the cache after hitting the db', function(done) {
  114. this.wordList = ['apples', 'bananas', 'pears']
  115. this.db.spellingPreferences.findOne = sinon
  116. .stub()
  117. .callsArgWith(1, null, { learnedWords: this.wordList })
  118. return this.LearnedWordsManager.getLearnedWords(
  119. this.token,
  120. (err, spellings) => {
  121. this.cache.set
  122. .calledWith(this.token, this.wordList)
  123. .should.equal(true)
  124. return done()
  125. }
  126. )
  127. })
  128. return it('should break cache when update is called', function(done) {
  129. this.word = 'instanton'
  130. return this.LearnedWordsManager.learnWord(this.token, this.word, () => {
  131. this.cache.del.calledWith(this.token).should.equal(true)
  132. return done()
  133. })
  134. })
  135. })
  136. return describe('deleteUsersLearnedWords', function() {
  137. beforeEach(function() {
  138. return (this.db.spellingPreferences.remove = sinon.stub().callsArgWith(1))
  139. })
  140. return it('should get the word list for the given user', function(done) {
  141. return this.LearnedWordsManager.deleteUsersLearnedWords(
  142. this.token,
  143. () => {
  144. this.db.spellingPreferences.remove
  145. .calledWith({ token: this.token })
  146. .should.equal(true)
  147. return done()
  148. }
  149. )
  150. })
  151. })
  152. })