mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-09-22 16:26:08 +08:00
fix(specs) Fix Spellchecker tests
Summary: Previously, these tests were mostly testing the library itself, instead of our code. The library performed expensive operations and caused the test to time out more often than not This commit makes it so we test our code, mock out any calls to side effects, and removes a line that was overriding our jasmine timeout Test Plan: unit Reviewers: spang, evan, halla Reviewed By: evan, halla Differential Revision: https://phab.nylas.com/D3885
This commit is contained in:
parent
6d379efba1
commit
3a49bfe448
|
@ -160,8 +160,6 @@ class N1SpecRunner {
|
|||
_extendJasmineMethods() {
|
||||
const jasmine = jasmineExports.jasmine;
|
||||
|
||||
jasmine.getEnv().defaultTimeoutInterval = 500;
|
||||
|
||||
// Use underscore's definition of equality for toEqual assertions
|
||||
jasmine.getEnv().addEqualityTester(_.isEqual);
|
||||
|
||||
|
|
|
@ -1,51 +1,31 @@
|
|||
/* eslint global-require: 0 */
|
||||
import fs from 'fs'
|
||||
import {Spellchecker} from 'nylas-exports';
|
||||
|
||||
describe("Spellchecker", function spellcheckerTests() {
|
||||
beforeEach(() => {
|
||||
Spellchecker.handler.switchLanguage('en-US'); // Start with US English
|
||||
});
|
||||
|
||||
[
|
||||
{name: "French", code: "fr", sentence: "Ceci est une phrase avec quelques mots."},
|
||||
{name: "German", code: "de", sentence: "Das ist ein Satz mit einigen Worten."},
|
||||
{name: "Italian", code: "it", sentence: "Questa è una frase con alcune parole."},
|
||||
{name: "Russian", code: "ru", sentence: "Это предложение с некоторыми словами."},
|
||||
{name: "Spanish", code: "es", sentence: "Esta es una oración con algunas palabras."},
|
||||
// English shouldn't be first since we start out as English.
|
||||
{name: "English", code: "en", sentence: "This is a sentence with some words."},
|
||||
].forEach(({name, code, sentence}) => {
|
||||
it(`properly detects language when given a full sentence (${name})`, async () => {
|
||||
// Note, on Linux, calling provideHintText can result in a Hunspell
|
||||
// dictionary being downloaded. Typically this is fast but it causes
|
||||
// intermittent failures.
|
||||
if (process.env.TRAVIS && process.platform === 'linux') {
|
||||
expect(true).toEqual(true);
|
||||
return;
|
||||
}
|
||||
|
||||
process.nextTick(() => advanceClock(20));
|
||||
const lang = await Spellchecker.handler.detectLanguageForText(sentence);
|
||||
expect(lang).toBe(code)
|
||||
});
|
||||
});
|
||||
|
||||
it("knows whether a word is misspelled or not", () => {
|
||||
const correctlySpelled = ["hello", "world", "create", "goodbye", "regards"]
|
||||
const misspelled = ["mispelled", "particularily", "kelfiekd", "adlkdgiekdl"]
|
||||
for (const word of correctlySpelled) {
|
||||
expect(Spellchecker.isMisspelled(word)).toEqual(false);
|
||||
}
|
||||
for (const word of misspelled) {
|
||||
expect(Spellchecker.isMisspelled(word)).toEqual(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("provides suggestions for misspelled words", () => {
|
||||
const suggestions = Spellchecker.handler.currentSpellchecker.getCorrectionsForMisspelling("mispelled")
|
||||
expect(suggestions.length > 0).toEqual(true);
|
||||
expect(suggestions[0]).toEqual('misspelled');
|
||||
// electron-spellchecker is under heavy development, make sure we can still
|
||||
// rely on this method
|
||||
expect(Spellchecker.handler.handleElectronSpellCheck).toBeDefined()
|
||||
this.customDict = '{}'
|
||||
spyOn(fs, 'writeFile').andCallFake((path, customDict, cb) => {
|
||||
this.customDict = customDict
|
||||
cb()
|
||||
})
|
||||
spyOn(fs, 'readFile').andCallFake((path, cb) => {
|
||||
cb(null, this.customDict)
|
||||
})
|
||||
// Apparently handleElectronSpellCheck returns !misspelled
|
||||
spyOn(Spellchecker.handler, 'handleElectronSpellCheck').andReturn(false)
|
||||
Spellchecker.isMisspelledCache = {}
|
||||
});
|
||||
|
||||
it('does not call spellchecker when word has already been learned', () => {
|
||||
Spellchecker.isMisspelledCache = {mispelled: true}
|
||||
const misspelled = Spellchecker.isMisspelled('mispelled')
|
||||
expect(misspelled).toBe(true)
|
||||
expect(Spellchecker.handler.handleElectronSpellCheck).not.toHaveBeenCalled()
|
||||
});
|
||||
|
||||
describe("when a custom word is added", () => {
|
||||
this.customWord = "becaause"
|
||||
|
@ -64,13 +44,6 @@ describe("Spellchecker", function spellcheckerTests() {
|
|||
expect(Spellchecker.isMisspelled(this.customWord)).toEqual(false)
|
||||
})
|
||||
|
||||
it("maintains it when switching languages", () => {
|
||||
Spellchecker.handler.switchLanguage("de-DE")
|
||||
expect(Spellchecker.isMisspelled(this.customWord)).toEqual(false);
|
||||
Spellchecker.handler.switchLanguage("en-US")
|
||||
expect(Spellchecker.isMisspelled(this.customWord)).toEqual(false);
|
||||
})
|
||||
|
||||
it("maintains it across instances", () => {
|
||||
const Spellchecker2 = require("../src/spellchecker").default;
|
||||
expect(Spellchecker2.isMisspelled(this.customWord)).toEqual(false);
|
||||
|
|
Loading…
Reference in a new issue