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:
Juan Tejada 2017-02-10 12:26:01 -08:00
parent 6d379efba1
commit 3a49bfe448
2 changed files with 20 additions and 49 deletions

View file

@ -160,8 +160,6 @@ class N1SpecRunner {
_extendJasmineMethods() { _extendJasmineMethods() {
const jasmine = jasmineExports.jasmine; const jasmine = jasmineExports.jasmine;
jasmine.getEnv().defaultTimeoutInterval = 500;
// Use underscore's definition of equality for toEqual assertions // Use underscore's definition of equality for toEqual assertions
jasmine.getEnv().addEqualityTester(_.isEqual); jasmine.getEnv().addEqualityTester(_.isEqual);

View file

@ -1,51 +1,31 @@
/* eslint global-require: 0 */ /* eslint global-require: 0 */
import fs from 'fs'
import {Spellchecker} from 'nylas-exports'; import {Spellchecker} from 'nylas-exports';
describe("Spellchecker", function spellcheckerTests() { describe("Spellchecker", function spellcheckerTests() {
beforeEach(() => { beforeEach(() => {
Spellchecker.handler.switchLanguage('en-US'); // Start with US English // electron-spellchecker is under heavy development, make sure we can still
}); // rely on this method
expect(Spellchecker.handler.handleElectronSpellCheck).toBeDefined()
[ this.customDict = '{}'
{name: "French", code: "fr", sentence: "Ceci est une phrase avec quelques mots."}, spyOn(fs, 'writeFile').andCallFake((path, customDict, cb) => {
{name: "German", code: "de", sentence: "Das ist ein Satz mit einigen Worten."}, this.customDict = customDict
{name: "Italian", code: "it", sentence: "Questa è una frase con alcune parole."}, cb()
{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');
}) })
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", () => { describe("when a custom word is added", () => {
this.customWord = "becaause" this.customWord = "becaause"
@ -64,13 +44,6 @@ describe("Spellchecker", function spellcheckerTests() {
expect(Spellchecker.isMisspelled(this.customWord)).toEqual(false) 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", () => { it("maintains it across instances", () => {
const Spellchecker2 = require("../src/spellchecker").default; const Spellchecker2 = require("../src/spellchecker").default;
expect(Spellchecker2.isMisspelled(this.customWord)).toEqual(false); expect(Spellchecker2.isMisspelled(this.customWord)).toEqual(false);