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
|
|
|
*/
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
chai.should()
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/SpellingAPIManager'
|
|
|
|
)
|
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
const promiseStub = val => new Promise(resolve => resolve(val))
|
|
|
|
|
2019-07-03 08:41:01 -04:00
|
|
|
describe('SpellingAPIManager', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.token = 'user-id-123'
|
|
|
|
this.ASpell = {}
|
|
|
|
this.learnedWords = ['lerned']
|
|
|
|
this.LearnedWordsManager = {
|
|
|
|
getLearnedWords: sinon.stub().callsArgWith(1, null, this.learnedWords),
|
2019-07-11 06:29:00 -04:00
|
|
|
learnWord: sinon.stub().callsArg(2),
|
2019-07-20 09:04:08 -04:00
|
|
|
unlearnWord: sinon.stub().callsArg(2),
|
2019-07-11 06:29:00 -04:00
|
|
|
promises: {
|
|
|
|
getLearnedWords: sinon.stub().returns(promiseStub(this.learnedWords))
|
|
|
|
}
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
this.SpellingAPIManager = SandboxedModule.require(modulePath, {
|
2019-07-03 08:41:01 -04:00
|
|
|
requires: {
|
|
|
|
'./ASpell': this.ASpell,
|
|
|
|
'./LearnedWordsManager': this.LearnedWordsManager
|
|
|
|
}
|
2019-07-11 06:29:00 -04:00
|
|
|
})
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('runRequest', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.nonLearnedWords = [
|
|
|
|
'some',
|
|
|
|
'words',
|
|
|
|
'htat',
|
|
|
|
'are',
|
|
|
|
'speled',
|
|
|
|
'rong',
|
|
|
|
'lerned'
|
|
|
|
]
|
|
|
|
this.allWords = this.nonLearnedWords.concat(this.learnedWords)
|
|
|
|
this.misspellings = [
|
|
|
|
{ index: 2, suggestions: ['that'] },
|
|
|
|
{ index: 4, suggestions: ['spelled'] },
|
|
|
|
{ index: 5, suggestions: ['wrong', 'ring'] },
|
|
|
|
{ index: 6, suggestions: ['learned'] }
|
|
|
|
]
|
|
|
|
this.misspellingsWithoutLearnedWords = this.misspellings.slice(0, 3)
|
|
|
|
|
|
|
|
this.ASpell.checkWords = (lang, word, callback) => {
|
2019-07-11 06:29:00 -04:00
|
|
|
callback(null, this.misspellings)
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
2019-07-11 06:29:00 -04:00
|
|
|
this.ASpell.promises = {
|
|
|
|
checkWords: sinon.stub().returns(promiseStub(this.misspellings))
|
|
|
|
}
|
|
|
|
sinon.spy(this.ASpell, 'checkWords')
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
this.result = result
|
2019-07-11 06:29:00 -04:00
|
|
|
done()
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -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
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a missing token', function() {
|
|
|
|
beforeEach(function(done) {
|
2019-07-11 06:29:00 -04:00
|
|
|
this.SpellingAPIManager.runRequest(
|
2019-07-03 08:41:01 -04:00
|
|
|
null,
|
|
|
|
{ words: this.allWords },
|
|
|
|
(error, result) => {
|
|
|
|
this.error = error
|
|
|
|
this.result = result
|
2019-07-11 06:29:00 -04:00
|
|
|
done()
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
it('should spell check without using any learned words', function() {
|
|
|
|
this.LearnedWordsManager.getLearnedWords.called.should.equal(false)
|
2019-07-03 08:41:01 -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) => {
|
|
|
|
this.result = result
|
2019-07-11 06:29:00 -04:00
|
|
|
done()
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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,
|
2019-07-11 06:29:00 -04:00
|
|
|
language: this.language
|
2019-07-03 08:41:01 -04:00
|
|
|
},
|
|
|
|
(error, result) => {
|
|
|
|
this.result = result
|
2019-07-11 06:29:00 -04:00
|
|
|
done()
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -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-11 06:29:00 -04:00
|
|
|
describe('with words from the whitelist', function() {
|
2019-07-03 08:41:01 -04:00
|
|
|
beforeEach(function(done) {
|
|
|
|
this.whitelistWord = this.SpellingAPIManager.whitelist[0]
|
|
|
|
this.words = ['One', 'Two', this.whitelistWord]
|
2019-07-11 06:29:00 -04:00
|
|
|
this.SpellingAPIManager.runRequest(
|
2019-07-03 08:41:01 -04:00
|
|
|
this.token,
|
|
|
|
{ words: this.words },
|
|
|
|
(error, result) => {
|
|
|
|
this.result = result
|
2019-07-11 06:29:00 -04:00
|
|
|
done()
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
it('should ignore the white-listed word', function() {
|
|
|
|
expect(this.result.misspellings.length).to.equal(
|
2019-07-03 08:41:01 -04:00
|
|
|
this.misspellings.length - 1
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
describe('learnWord', function() {
|
2019-07-03 08:41:01 -04:00
|
|
|
describe('without a token', function() {
|
|
|
|
beforeEach(function(done) {
|
2019-07-11 06:29:00 -04:00
|
|
|
this.SpellingAPIManager.learnWord(null, { word: 'banana' }, error => {
|
|
|
|
this.error = error
|
|
|
|
done()
|
|
|
|
})
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -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('no token provided')
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('without a word', function() {
|
|
|
|
beforeEach(function(done) {
|
2019-07-11 06:29:00 -04:00
|
|
|
this.SpellingAPIManager.learnWord(this.token, {}, error => {
|
2019-07-03 08:41:01 -04:00
|
|
|
this.error = error
|
2019-07-11 06:29:00 -04:00
|
|
|
done()
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
describe('with a word and a token', function() {
|
2019-07-03 08:41:01 -04:00
|
|
|
beforeEach(function(done) {
|
|
|
|
this.word = 'banana'
|
2019-07-11 06:29:00 -04:00
|
|
|
this.SpellingAPIManager.learnWord(
|
2019-07-03 08:41:01 -04:00
|
|
|
this.token,
|
|
|
|
{ word: this.word },
|
|
|
|
error => {
|
|
|
|
this.error = error
|
2019-07-11 06:29:00 -04:00
|
|
|
done()
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
it('should call LearnedWordsManager.learnWord', function() {
|
|
|
|
this.LearnedWordsManager.learnWord
|
2019-07-03 08:41:01 -04:00
|
|
|
.calledWith(this.token, this.word)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-07-20 09:04:08 -04:00
|
|
|
|
|
|
|
describe('unlearnWord', function() {
|
|
|
|
describe('without a token', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
this.SpellingAPIManager.unlearnWord(null, { word: 'banana' }, error => {
|
|
|
|
this.error = error
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return an error', function() {
|
|
|
|
expect(this.error).to.exist
|
|
|
|
expect(this.error).to.be.instanceof(Error)
|
|
|
|
expect(this.error.message).to.equal('no token provided')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('without a word', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
this.SpellingAPIManager.unlearnWord(this.token, {}, error => {
|
|
|
|
this.error = error
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return an error', function() {
|
|
|
|
expect(this.error).to.exist
|
|
|
|
expect(this.error).to.be.instanceof(Error)
|
|
|
|
expect(this.error.message).to.equal('malformed JSON')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a word and a token', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
this.word = 'banana'
|
|
|
|
this.SpellingAPIManager.unlearnWord(
|
|
|
|
this.token,
|
|
|
|
{ word: this.word },
|
|
|
|
error => {
|
|
|
|
this.error = error
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call LearnedWordsManager.unlearnWord', function() {
|
|
|
|
this.LearnedWordsManager.unlearnWord
|
|
|
|
.calledWith(this.token, this.word)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|