overleaf/services/spelling/test/unit/js/SpellingAPIManagerTests.js

125 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-07-03 08:41:01 -04:00
/* eslint-disable
handle-callback-err
2019-07-03 08:41:01 -04:00
*/
const sinon = require('sinon')
const { expect } = require('chai')
2019-07-03 08:41:01 -04:00
const SandboxedModule = require('sandboxed-module')
const modulePath = require('path').join(
__dirname,
'../../../app/js/SpellingAPIManager'
)
2021-07-13 07:04:47 -04:00
const promiseStub = val => new Promise(resolve => resolve(val))
2020-08-10 12:23:15 -04:00
describe('SpellingAPIManager', function () {
beforeEach(function () {
2019-07-03 08:41:01 -04:00
this.token = 'user-id-123'
this.ASpell = {}
this.SpellingAPIManager = SandboxedModule.require(modulePath, {
2019-07-03 08:41:01 -04:00
requires: {
'./ASpell': this.ASpell,
'@overleaf/settings': { ignoredMisspellings: ['ShareLaTeX'] },
2021-07-13 07:04:47 -04:00
},
})
2019-07-03 08:41:01 -04:00
})
2020-08-10 12:23:15 -04:00
describe('runRequest', function () {
beforeEach(function () {
this.nonLearnedWords = ['some', 'words', 'htat', 'are', 'speled', 'rong']
this.allWords = this.nonLearnedWords
2019-07-03 08:41:01 -04:00
this.misspellings = [
{ index: 2, suggestions: ['that'] },
{ index: 4, suggestions: ['spelled'] },
{ index: 5, suggestions: ['wrong', 'ring'] },
]
this.misspellingsWithoutLearnedWords = this.misspellings.slice(0, 3)
this.ASpell.checkWords = (lang, word, callback) => {
callback(null, this.misspellings)
2019-07-03 08:41:01 -04:00
}
this.ASpell.promises = {
2021-07-13 07:04:47 -04:00
checkWords: sinon.stub().returns(promiseStub(this.misspellings)),
}
sinon.spy(this.ASpell, 'checkWords')
2019-07-03 08:41:01 -04:00
})
2020-08-10 12:23:15 -04:00
describe('with sensible JSON', function () {
beforeEach(function (done) {
this.SpellingAPIManager.runRequest(
2019-07-03 08:41:01 -04:00
this.token,
{ words: this.allWords },
(error, result) => {
if (error) return done(error)
2019-07-03 08:41:01 -04:00
this.result = result
done()
2019-07-03 08:41:01 -04:00
}
)
})
2020-08-10 12:23:15 -04:00
it('should return the words that are spelled incorrectly and not learned', function () {
expect(this.result.misspellings).to.deep.equal(
2019-07-03 08:41:01 -04:00
this.misspellingsWithoutLearnedWords
)
})
})
2020-08-10 12:23:15 -04:00
describe('with a missing words array', function () {
beforeEach(function (done) {
this.SpellingAPIManager.runRequest(this.token, {}, (error, result) => {
this.error = error
this.result = result
done()
})
2019-07-03 08:41:01 -04:00
})
2020-08-10 12:23:15 -04:00
it('should return an error', function () {
2019-07-03 08:41:01 -04:00
expect(this.error).to.exist
expect(this.error).to.be.instanceof(Error)
expect(this.error.message).to.equal('malformed JSON')
2019-07-03 08:41:01 -04:00
})
})
2020-08-10 12:23:15 -04:00
describe('without a language', function () {
beforeEach(function (done) {
this.SpellingAPIManager.runRequest(
2019-07-03 08:41:01 -04:00
this.token,
{ words: this.allWords },
(error, result) => {
if (error) return done(error)
2019-07-03 08:41:01 -04:00
this.result = result
done()
2019-07-03 08:41:01 -04:00
}
)
})
2020-08-10 12:23:15 -04:00
it('should use en as the default', function () {
this.ASpell.promises.checkWords.calledWith('en').should.equal(true)
2019-07-03 08:41:01 -04:00
})
})
2020-08-10 12:23:15 -04:00
describe('with a language', function () {
beforeEach(function (done) {
this.language = 'fr'
this.SpellingAPIManager.runRequest(
2019-07-03 08:41:01 -04:00
this.token,
{
words: this.allWords,
2021-07-13 07:04:47 -04:00
language: this.language,
2019-07-03 08:41:01 -04:00
},
(error, result) => {
if (error) return done(error)
2019-07-03 08:41:01 -04:00
this.result = result
done()
2019-07-03 08:41:01 -04:00
}
)
})
2020-08-10 12:23:15 -04:00
it('should use the language', function () {
this.ASpell.promises.checkWords
2019-07-03 08:41:01 -04:00
.calledWith(this.language)
.should.equal(true)
})
})
2019-07-20 09:04:08 -04:00
})
2019-07-03 08:41:01 -04:00
})