Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear spell-checker if new lang has no dictionary #3185

Merged
merged 7 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading