LearnedWordsManagerTests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. describe('LearnedWordsManager', function () {
  9. beforeEach(function () {
  10. this.token = 'a6b3cd919ge'
  11. this.callback = sinon.stub()
  12. this.db = {
  13. spellingPreferences: {
  14. updateOne: sinon.stub().yields(),
  15. },
  16. }
  17. this.LearnedWordsManager = SandboxedModule.require(modulePath, {
  18. requires: {
  19. '../../infrastructure/mongodb': { db: this.db },
  20. '@overleaf/metrics': {
  21. timeAsyncMethod: sinon.stub(),
  22. inc: sinon.stub(),
  23. },
  24. },
  25. })
  26. })
  27. describe('learnWord', function () {
  28. beforeEach(function () {
  29. this.word = 'instanton'
  30. this.LearnedWordsManager.learnWord(this.token, this.word, this.callback)
  31. })
  32. it('should insert the word in the word list in the database', function () {
  33. expect(
  34. this.db.spellingPreferences.updateOne.calledWith(
  35. {
  36. token: this.token,
  37. },
  38. {
  39. $addToSet: { learnedWords: this.word },
  40. },
  41. {
  42. upsert: true,
  43. }
  44. )
  45. ).to.equal(true)
  46. })
  47. it('should call the callback', function () {
  48. expect(this.callback.called).to.equal(true)
  49. })
  50. })
  51. describe('unlearnWord', function () {
  52. beforeEach(function () {
  53. this.word = 'instanton'
  54. this.LearnedWordsManager.unlearnWord(this.token, this.word, this.callback)
  55. })
  56. it('should remove the word from the word list in the database', function () {
  57. expect(
  58. this.db.spellingPreferences.updateOne.calledWith(
  59. {
  60. token: this.token,
  61. },
  62. {
  63. $pull: { learnedWords: this.word },
  64. }
  65. )
  66. ).to.equal(true)
  67. })
  68. it('should call the callback', function () {
  69. expect(this.callback.called).to.equal(true)
  70. })
  71. })
  72. describe('getLearnedWords', function () {
  73. beforeEach(function () {
  74. this.wordList = ['apples', 'bananas', 'pears']
  75. this.wordListWithDuplicates = this.wordList.slice()
  76. this.wordListWithDuplicates.push('bananas')
  77. this.db.spellingPreferences.findOne = (conditions, callback) => {
  78. callback(null, { learnedWords: this.wordListWithDuplicates })
  79. }
  80. sinon.spy(this.db.spellingPreferences, 'findOne')
  81. this.LearnedWordsManager.getLearnedWords(this.token, this.callback)
  82. })
  83. it('should get the word list for the given user', function () {
  84. expect(
  85. this.db.spellingPreferences.findOne.calledWith({ token: this.token })
  86. ).to.equal(true)
  87. })
  88. it('should return the word list in the callback without duplicates', function () {
  89. expect(this.callback.calledWith(null, this.wordList)).to.equal(true)
  90. })
  91. })
  92. describe('deleteUsersLearnedWords', function () {
  93. beforeEach(function () {
  94. this.db.spellingPreferences.deleteOne = sinon.stub().callsArgWith(1)
  95. })
  96. it('should get the word list for the given user', function (done) {
  97. this.LearnedWordsManager.deleteUsersLearnedWords(this.token, () => {
  98. this.db.spellingPreferences.deleteOne
  99. .calledWith({ token: this.token })
  100. .should.equal(true)
  101. done()
  102. })
  103. })
  104. })
  105. })