overleaf/services/spelling/test/unit/coffee/LearnedWordsManagerTests.coffee

98 lines
3 KiB
CoffeeScript
Raw Normal View History

2014-08-15 07:13:35 -04:00
sinon = require('sinon')
chai = require 'chai'
expect = chai.expect
SandboxedModule = require('sandboxed-module')
modulePath = require('path').join __dirname, '../../../app/js/LearnedWordsManager'
2015-03-03 09:28:47 -05:00
assert = require("chai").assert
should = require("chai").should()
2014-08-15 07:13:35 -04:00
describe "LearnedWordsManager", ->
beforeEach ->
@token = "a6b3cd919ge"
@callback = sinon.stub()
@db =
spellingPreferences:
update: sinon.stub().callsArg(3)
2015-03-03 09:28:47 -05:00
@cache =
get:sinon.stub()
set:sinon.stub()
del:sinon.stub()
2014-08-15 07:13:35 -04:00
@LearnedWordsManager = SandboxedModule.require modulePath, requires:
"./DB" : @db
2015-03-03 09:28:47 -05:00
"./MongoCache":@cache
2015-05-20 06:44:54 -04:00
"logger-sharelatex":
log:->
err:->
info:->
2017-03-16 12:35:51 -04:00
'metrics-sharelatex': {timeAsyncMethod: sinon.stub(), inc: sinon.stub()}
2014-08-15 07:13:35 -04:00
describe "learnWord", ->
beforeEach ->
@word = "instanton"
@LearnedWordsManager.learnWord @token, @word, @callback
it "should insert the word in the word list in the database", ->
expect(
@db.spellingPreferences.update.calledWith({
token: @token
}, {
$push : learnedWords: @word
}, {
upsert: true
})
).to.equal true
it "should call the callback", ->
expect(@callback.called).to.equal true
describe "getLearnedWords", ->
beforeEach ->
@wordList = ["apples", "bananas", "pears"]
@db.spellingPreferences.findOne = (conditions, callback) =>
callback null, learnedWords: @wordList
sinon.spy @db.spellingPreferences, "findOne"
@LearnedWordsManager.getLearnedWords @token, @callback
it "should get the word list for the given user", ->
expect(
@db.spellingPreferences.findOne.calledWith token: @token
).to.equal true
it "should return the word list in the callback", ->
expect(@callback.calledWith null, @wordList).to.equal true
2015-03-03 09:28:47 -05:00
2014-08-15 07:13:35 -04:00
describe "caching the result", ->
it 'should use the cache first if it is primed', (done)->
@wordList = ["apples", "bananas", "pears"]
2015-03-03 09:28:47 -05:00
@cache.get.returns(@wordList)
2014-08-15 07:13:35 -04:00
@db.spellingPreferences.findOne = sinon.stub()
@LearnedWordsManager.getLearnedWords @token, (err, spellings)=>
2015-03-03 09:28:47 -05:00
@db.spellingPreferences.findOne.called.should.equal false
assert.deepEqual @wordList, spellings
2014-08-15 07:13:35 -04:00
done()
it 'should set the cache after hitting the db', (done)->
@wordList = ["apples", "bananas", "pears"]
@db.spellingPreferences.findOne = sinon.stub().callsArgWith(1, null, learnedWords: @wordList)
@LearnedWordsManager.getLearnedWords @token, (err, spellings)=>
2015-03-03 09:28:47 -05:00
@cache.set.calledWith(@token, @wordList).should.equal true
2014-08-15 07:13:35 -04:00
done()
it 'should break cache when update is called', (done)->
@word = "instanton"
@LearnedWordsManager.learnWord @token, @word, =>
2015-03-03 09:28:47 -05:00
@cache.del.calledWith(@token).should.equal true
2014-08-15 07:13:35 -04:00
done()
2017-10-30 12:57:34 -04:00
describe "deleteUsersLearnedWords", ->
beforeEach ->
2017-11-02 07:24:01 -04:00
@db.spellingPreferences.remove = sinon.stub().callsArgWith(1)
2017-10-30 12:57:34 -04:00
it "should get the word list for the given user", (done)->
@LearnedWordsManager.deleteUsersLearnedWords @token, =>
2017-11-02 07:24:01 -04:00
@db.spellingPreferences.remove.calledWith(token: @token).should.equal true
2017-10-30 12:57:34 -04:00
done()