diff --git a/src/utilities/spellChecker.ts b/src/utilities/spellChecker.ts index 847a86854c..d2c83b69bb 100644 --- a/src/utilities/spellChecker.ts +++ b/src/utilities/spellChecker.ts @@ -34,12 +34,17 @@ export default class SpellChecker { this.dictLoader = new DictionaryLoader(bcp47); this.dictLoaded = {}; await this.dictLoader.loadDictionary().then((dic) => { - if (dic !== undefined) { + if (dic) { this.spell = nspell("SET UTF-8", dic); this.addToDictLoaded(dic); if (process.env.NODE_ENV === "development") { console.log(`Loaded spell-checker: ${bcp47}`); } + } else { + this.spell = undefined; + if (process.env.NODE_ENV === "development") { + console.log(`No dictionary available: ${bcp47}`); + } } }); } diff --git a/src/utilities/tests/spellChecker.test.ts b/src/utilities/tests/spellChecker.test.ts index f208c1a6fc..6a6611595d 100644 --- a/src/utilities/tests/spellChecker.test.ts +++ b/src/utilities/tests/spellChecker.test.ts @@ -1,10 +1,12 @@ import SpellChecker from "utilities/spellChecker"; jest.mock("resources/dictionaries", () => ({ - getDict: () => Promise.resolve(`2\n${mockValidWordA}\n${mockValidWordBExt}`), + getDict: () => mockGetDict(), getKeys: () => [], })); +const mockGetDict = jest.fn(); + const invalidWord = "asdfghjkl"; const mockWord = "mockWord"; const mockValidWordA = `${mockWord}A`; @@ -12,6 +14,12 @@ const mockWordB = `${mockWord}B`; const mockWordC = `${mockWord}C`; const mockValidWordBExt = `${mockWordB}Extended`; +beforeEach(() => { + mockGetDict.mockImplementation(() => + Promise.resolve(`2\n${mockValidWordA}\n${mockValidWordBExt}`) + ); +}); + describe("SpellChecker", () => { describe("correct", () => { it("detects a correctly spelled word", (done) => { @@ -115,6 +123,45 @@ describe("SpellChecker", () => { }); }); + describe("updateLang", () => { + it("keeps dictionary when new lang code has same first part", (done) => { + const spellChecker = new SpellChecker("en-GB"); + // Give the dictionary half-a-sec to load. + setTimeout(() => { + const suggestions = spellChecker.getSpellingSuggestions(mockWordB); + expect(suggestions).toContain(mockValidWordA); + + mockGetDict.mockClear(); + spellChecker.updateLang("en-US").then(() => { + expect( + spellChecker.getSpellingSuggestions(mockWordB) + ).not.toHaveLength(0); + expect(mockGetDict).not.toHaveBeenCalled(); + done(); + }); + }, 500); + }); + + it("clears dictionary when new lang code has no dictionary", (done) => { + const spellChecker = new SpellChecker("en-GB"); + // Give the dictionary half-a-sec to load. + setTimeout(() => { + const suggestions = spellChecker.getSpellingSuggestions(mockWordB); + expect(suggestions).toContain(mockValidWordA); + + mockGetDict.mockClear(); + mockGetDict.mockResolvedValue(undefined); + spellChecker.updateLang("tpi").then(() => { + expect(mockGetDict).toHaveBeenCalled(); + expect(spellChecker.getSpellingSuggestions(mockWordB)).toHaveLength( + 0 + ); + done(); + }); + }, 500); + }); + }); + describe("replaceAllButLastWordWithEllipses", () => { it("handles empty string", () => { expect(SpellChecker.replaceAllButLastWordWithEllipses("")).toEqual("");