Skip to content

Commit

Permalink
Clear spell-checker if new lang has no dictionary (#3185)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Aug 13, 2024
1 parent 9feb899 commit 5d7eee8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/utilities/spellChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
});
}
Expand Down
49 changes: 48 additions & 1 deletion src/utilities/tests/spellChecker.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
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`;
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) => {
Expand Down Expand Up @@ -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("");
Expand Down

0 comments on commit 5d7eee8

Please sign in to comment.