From 72d16c7c1fac693d44ed9f30f1df8c08452ab945 Mon Sep 17 00:00:00 2001 From: Tim Alby Date: Tue, 18 Feb 2020 10:58:03 -0500 Subject: [PATCH] filter out duplicates learned words --- services/spelling/app/js/LearnedWordsManager.js | 8 +++++++- services/spelling/test/acceptance/js/LearnTest.js | 2 ++ .../spelling/test/unit/js/LearnedWordsManagerTests.js | 9 +++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/services/spelling/app/js/LearnedWordsManager.js b/services/spelling/app/js/LearnedWordsManager.js index bfb9bcc91f..2b57697014 100644 --- a/services/spelling/app/js/LearnedWordsManager.js +++ b/services/spelling/app/js/LearnedWordsManager.js @@ -60,8 +60,14 @@ const LearnedWordsManager = { if (error != null) { return callback(error) } - const words = + let words = (preferences != null ? preferences.learnedWords : undefined) || [] + if (words) { + // remove duplicates + words = words.filter( + (value, index, self) => self.indexOf(value) === index + ) + } mongoCache.set(userToken, words) callback(null, words) }) diff --git a/services/spelling/test/acceptance/js/LearnTest.js b/services/spelling/test/acceptance/js/LearnTest.js index a3e8bed07a..7d50b22798 100644 --- a/services/spelling/test/acceptance/js/LearnTest.js +++ b/services/spelling/test/acceptance/js/LearnTest.js @@ -54,6 +54,8 @@ describe('learning words', function() { const dictResponse = await getDict() const responseBody = JSON.parse(dictResponse.body) + // the response from getlearnedwords filters out duplicates, so this test + // can succeed even if the word is stored twice in the database expect(responseBody.length).to.equals(1) }) diff --git a/services/spelling/test/unit/js/LearnedWordsManagerTests.js b/services/spelling/test/unit/js/LearnedWordsManagerTests.js index c617b1fb81..3fa0ec22b6 100644 --- a/services/spelling/test/unit/js/LearnedWordsManagerTests.js +++ b/services/spelling/test/unit/js/LearnedWordsManagerTests.js @@ -22,6 +22,9 @@ describe('LearnedWordsManager', function() { del: sinon.stub() } this.LearnedWordsManager = SandboxedModule.require(modulePath, { + globals: { + console: console + }, requires: { './DB': this.db, './MongoCache': this.cache, @@ -92,8 +95,10 @@ describe('LearnedWordsManager', function() { 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.wordList }) + callback(null, { learnedWords: this.wordListWithDuplicates }) } sinon.spy(this.db.spellingPreferences, 'findOne') this.LearnedWordsManager.getLearnedWords(this.token, this.callback) @@ -105,7 +110,7 @@ describe('LearnedWordsManager', function() { ).to.equal(true) }) - it('should return the word list in the callback', function() { + it('should return the word list in the callback without duplicates', function() { expect(this.callback.calledWith(null, this.wordList)).to.equal(true) }) })