From 3a49bfe448dfbfcdc9061dd1d397392ed8bbf3fb Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Fri, 10 Feb 2017 12:26:01 -0800 Subject: [PATCH] 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 --- spec/n1-spec-runner/n1-spec-runner.es6 | 2 - spec/spellchecker-spec.es6 | 67 ++++++++------------------ 2 files changed, 20 insertions(+), 49 deletions(-) diff --git a/spec/n1-spec-runner/n1-spec-runner.es6 b/spec/n1-spec-runner/n1-spec-runner.es6 index 6157d6ddc..53e4a14d7 100644 --- a/spec/n1-spec-runner/n1-spec-runner.es6 +++ b/spec/n1-spec-runner/n1-spec-runner.es6 @@ -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); diff --git a/spec/spellchecker-spec.es6 b/spec/spellchecker-spec.es6 index 812e06211..c078ffb97 100644 --- a/spec/spellchecker-spec.es6 +++ b/spec/spellchecker-spec.es6 @@ -1,52 +1,32 @@ /* 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 + // 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 = {} }); - [ - {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('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() }); - 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'); - }) - 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);