| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- const sinon = require('sinon')
- const { assert, expect } = require('chai')
- const SandboxedModule = require('sandboxed-module')
- const modulePath = require('path').join(
- __dirname,
- '../../../app/js/LearnedWordsManager'
- )
- describe('LearnedWordsManager', function () {
- beforeEach(function () {
- this.token = 'a6b3cd919ge'
- this.callback = sinon.stub()
- this.db = {
- spellingPreferences: {
- updateOne: sinon.stub().yields()
- }
- }
- this.cache = {
- get: sinon.stub(),
- set: sinon.stub(),
- del: sinon.stub()
- }
- this.LearnedWordsManager = SandboxedModule.require(modulePath, {
- requires: {
- './mongodb': { db: this.db },
- './MongoCache': this.cache,
- '@overleaf/metrics': {
- timeAsyncMethod: sinon.stub(),
- inc: sinon.stub()
- }
- }
- })
- })
- describe('learnWord', function () {
- beforeEach(function () {
- this.word = 'instanton'
- this.LearnedWordsManager.learnWord(this.token, this.word, this.callback)
- })
- it('should insert the word in the word list in the database', function () {
- expect(
- this.db.spellingPreferences.updateOne.calledWith(
- {
- token: this.token
- },
- {
- $addToSet: { learnedWords: this.word }
- },
- {
- upsert: true
- }
- )
- ).to.equal(true)
- })
- it('should call the callback', function () {
- expect(this.callback.called).to.equal(true)
- })
- })
- describe('unlearnWord', function () {
- beforeEach(function () {
- this.word = 'instanton'
- this.LearnedWordsManager.unlearnWord(this.token, this.word, this.callback)
- })
- it('should remove the word from the word list in the database', function () {
- expect(
- this.db.spellingPreferences.updateOne.calledWith(
- {
- token: this.token
- },
- {
- $pull: { learnedWords: this.word }
- }
- )
- ).to.equal(true)
- })
- it('should call the callback', function () {
- expect(this.callback.called).to.equal(true)
- })
- })
- describe('getLearnedWords', function () {
- beforeEach(function () {
- this.wordList = ['apples', 'bananas', 'pears']
- this.wordListWithDuplicates = this.wordList.slice()
- this.wordListWithDuplicates.push('bananas')
- this.db.spellingPreferences.findOne = (conditions, callback) => {
- callback(null, { learnedWords: this.wordListWithDuplicates })
- }
- sinon.spy(this.db.spellingPreferences, 'findOne')
- this.LearnedWordsManager.getLearnedWords(this.token, this.callback)
- })
- it('should get the word list for the given user', function () {
- expect(
- this.db.spellingPreferences.findOne.calledWith({ token: this.token })
- ).to.equal(true)
- })
- it('should return the word list in the callback without duplicates', function () {
- expect(this.callback.calledWith(null, this.wordList)).to.equal(true)
- })
- })
- describe('caching the result', function () {
- it('should use the cache first if it is primed', function (done) {
- this.wordList = ['apples', 'bananas', 'pears']
- this.cache.get.returns(this.wordList)
- this.db.spellingPreferences.findOne = sinon.stub()
- this.LearnedWordsManager.getLearnedWords(this.token, (err, spellings) => {
- expect(err).not.to.exist
- this.db.spellingPreferences.findOne.called.should.equal(false)
- assert.deepEqual(this.wordList, spellings)
- done()
- })
- })
- it('should set the cache after hitting the db', function (done) {
- this.wordList = ['apples', 'bananas', 'pears']
- this.db.spellingPreferences.findOne = sinon
- .stub()
- .callsArgWith(1, null, { learnedWords: this.wordList })
- this.LearnedWordsManager.getLearnedWords(this.token, () => {
- this.cache.set.calledWith(this.token, this.wordList).should.equal(true)
- done()
- })
- })
- it('should break cache when update is called', function (done) {
- this.word = 'instanton'
- this.LearnedWordsManager.learnWord(this.token, this.word, () => {
- this.cache.del.calledWith(this.token).should.equal(true)
- done()
- })
- })
- })
- describe('deleteUsersLearnedWords', function () {
- beforeEach(function () {
- this.db.spellingPreferences.deleteOne = sinon.stub().callsArgWith(1)
- })
- it('should get the word list for the given user', function (done) {
- this.LearnedWordsManager.deleteUsersLearnedWords(this.token, () => {
- this.db.spellingPreferences.deleteOne
- .calledWith({ token: this.token })
- .should.equal(true)
- done()
- })
- })
- })
- })
|