From 1d405914fdcc906119f4b64619415c20ad6d0cf4 Mon Sep 17 00:00:00 2001 From: Tim Alby Date: Tue, 18 Feb 2020 09:03:16 -0500 Subject: [PATCH] don't learn the same word twice --- .../spelling/app/js/LearnedWordsManager.js | 2 +- .../spelling/test/acceptance/js/LearnTest.js | 29 +++++++++++++++---- .../test/unit/js/LearnedWordsManagerTests.js | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/services/spelling/app/js/LearnedWordsManager.js b/services/spelling/app/js/LearnedWordsManager.js index bbf7651d87..bfb9bcc91f 100644 --- a/services/spelling/app/js/LearnedWordsManager.js +++ b/services/spelling/app/js/LearnedWordsManager.js @@ -15,7 +15,7 @@ const LearnedWordsManager = { token: userToken }, { - $push: { learnedWords: word } + $addToSet: { learnedWords: word } }, { upsert: true diff --git a/services/spelling/test/acceptance/js/LearnTest.js b/services/spelling/test/acceptance/js/LearnTest.js index c516de3162..a3e8bed07a 100644 --- a/services/spelling/test/acceptance/js/LearnTest.js +++ b/services/spelling/test/acceptance/js/LearnTest.js @@ -27,18 +27,37 @@ const unlearnWord = word => }) }) +const getDict = () => + request.get({ + url: `/user/${USER_ID}` + }) + const deleteDict = () => request.del({ url: `/user/${USER_ID}` }) describe('learning words', function() { - it('should return status 204 when posting a word successfully', async function () { + afterEach(async function() { + await deleteDict() + }) + + it('should return status 204 when posting a word successfully', async function() { const response = await learnWord('abcd') expect(response.statusCode).to.equal(204) }) - it('should return no misspellings after a word is learnt', async function () { + it('should not learn the same word twice', async function() { + await learnWord('foobar') + const learnResponse = await learnWord('foobar') + expect(learnResponse.statusCode).to.equal(204) + + const dictResponse = await getDict() + const responseBody = JSON.parse(dictResponse.body) + expect(responseBody.length).to.equals(1) + }) + + it('should return no misspellings after a word is learnt', async function() { const response = await checkWord(['abv']) const responseBody = JSON.parse(response.body) expect(responseBody.misspellings.length).to.equals(1) @@ -50,7 +69,7 @@ describe('learning words', function() { expect(responseBody2.misspellings.length).to.equals(0) }) - it('should return misspellings again after a personal dictionary is deleted', async function () { + it('should return misspellings again after a personal dictionary is deleted', async function() { await learnWord('bvc') await deleteDict() @@ -61,12 +80,12 @@ describe('learning words', function() { }) describe('unlearning words', function() { - it('should return status 204 when posting a word successfully', async function () { + it('should return status 204 when posting a word successfully', async function() { const response = await unlearnWord('anything') expect(response.statusCode).to.equal(204) }) - it('should return misspellings after a word is unlearnt', async function () { + it('should return misspellings after a word is unlearnt', async function() { await learnWord('abv') const response = await checkWord(['abv']) diff --git a/services/spelling/test/unit/js/LearnedWordsManagerTests.js b/services/spelling/test/unit/js/LearnedWordsManagerTests.js index 32ea14a18a..c617b1fb81 100644 --- a/services/spelling/test/unit/js/LearnedWordsManagerTests.js +++ b/services/spelling/test/unit/js/LearnedWordsManagerTests.js @@ -51,7 +51,7 @@ describe('LearnedWordsManager', function() { token: this.token }, { - $push: { learnedWords: this.word } + $addToSet: { learnedWords: this.word } }, { upsert: true