2019-07-03 08:41:01 -04:00
|
|
|
/* eslint-disable
|
2019-07-11 06:29:00 -04:00
|
|
|
handle-callback-err
|
2019-07-03 08:41:01 -04:00
|
|
|
*/
|
2022-12-20 07:11:25 -05:00
|
|
|
import sinon from 'sinon'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import esmock from 'esmock'
|
|
|
|
|
|
|
|
const MODULE_PATH = '../../../app/js/SpellingAPIManager'
|
2019-07-03 08:41:01 -04:00
|
|
|
|
2021-07-13 07:04:47 -04:00
|
|
|
const promiseStub = val => new Promise(resolve => resolve(val))
|
2019-07-11 06:29:00 -04:00
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
describe('SpellingAPIManager', function () {
|
2022-12-20 07:11:25 -05:00
|
|
|
beforeEach(async function () {
|
2019-07-03 08:41:01 -04:00
|
|
|
this.token = 'user-id-123'
|
2022-12-20 07:11:25 -05:00
|
|
|
this.nonLearnedWords = ['some', 'words', 'htat', 'are', 'speled', 'rong']
|
|
|
|
this.allWords = this.nonLearnedWords
|
|
|
|
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: sinon.stub().yields(null, this.misspellings),
|
|
|
|
promises: {
|
|
|
|
checkWords: sinon.stub().returns(promiseStub(this.misspellings)),
|
2021-07-13 07:04:47 -04:00
|
|
|
},
|
2022-12-20 07:11:25 -05:00
|
|
|
}
|
|
|
|
this.SpellingAPIManager = await esmock(MODULE_PATH, {
|
|
|
|
'../../../app/js/ASpell.js': this.ASpell,
|
2024-02-06 04:34:15 -05:00
|
|
|
'@overleaf/settings': { ignoredMisspellings: ['Overleaf'] },
|
2019-07-11 06:29:00 -04:00
|
|
|
})
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
describe('runRequest', function () {
|
|
|
|
describe('with sensible JSON', function () {
|
|
|
|
beforeEach(function (done) {
|
2019-07-11 06:29:00 -04:00
|
|
|
this.SpellingAPIManager.runRequest(
|
2019-07-03 08:41:01 -04:00
|
|
|
this.token,
|
|
|
|
{ words: this.allWords },
|
|
|
|
(error, result) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2019-07-03 08:41:01 -04:00
|
|
|
this.result = result
|
2019-07-11 06:29:00 -04:00
|
|
|
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 () {
|
2019-07-11 06:29:00 -04:00
|
|
|
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) {
|
2019-07-11 06:29:00 -04:00
|
|
|
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)
|
2019-07-11 06:29:00 -04:00
|
|
|
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) {
|
2019-07-11 06:29:00 -04:00
|
|
|
this.SpellingAPIManager.runRequest(
|
2019-07-03 08:41:01 -04:00
|
|
|
this.token,
|
|
|
|
{ words: this.allWords },
|
|
|
|
(error, result) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2019-07-03 08:41:01 -04:00
|
|
|
this.result = result
|
2019-07-11 06:29:00 -04:00
|
|
|
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 () {
|
2019-07-11 06:29:00 -04:00
|
|
|
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) {
|
2019-07-11 06:29:00 -04:00
|
|
|
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) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2019-07-03 08:41:01 -04:00
|
|
|
this.result = result
|
2019-07-11 06:29:00 -04:00
|
|
|
done()
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
it('should use the language', function () {
|
2019-07-11 06:29:00 -04:00
|
|
|
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
|
|
|
})
|