spellchecker-spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { expect } from 'chai';
  2. import { BrowserWindow, Session, session } from 'electron';
  3. import { ifdescribe } from './spec-helpers';
  4. import { closeWindow } from './window-helpers';
  5. const features = process.electronBinding('features');
  6. ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
  7. let w: BrowserWindow;
  8. beforeEach(async () => {
  9. w = new BrowserWindow({
  10. show: false
  11. });
  12. });
  13. afterEach(async () => {
  14. await closeWindow(w);
  15. });
  16. describe('custom dictionary word list API', () => {
  17. let ses: Session;
  18. beforeEach(async () => {
  19. // ensure a new session runs on each test run
  20. ses = session.fromPartition(`persist:customdictionary-test-${Date.now()}`);
  21. });
  22. afterEach(async () => {
  23. if (ses) {
  24. await ses.clearStorageData();
  25. ses.destroy();
  26. }
  27. });
  28. describe('ses.listWordsFromSpellCheckerDictionary', () => {
  29. it('should successfully list words in custom dictionary', async () => {
  30. const words = ['foo', 'bar', 'baz'];
  31. const results = words.map(word => ses.addWordToSpellCheckerDictionary(word));
  32. expect(results).to.eql([true, true, true]);
  33. const wordList = await ses.listWordsInSpellCheckerDictionary();
  34. expect(wordList).to.have.deep.members(words);
  35. });
  36. it('should return an empty array if no words are added', async () => {
  37. const wordList = await ses.listWordsInSpellCheckerDictionary();
  38. expect(wordList).to.have.length(0);
  39. });
  40. });
  41. describe('ses.addWordToSpellCheckerDictionary', () => {
  42. it('should successfully add word to custom dictionary', async () => {
  43. const result = ses.addWordToSpellCheckerDictionary('foobar');
  44. expect(result).to.equal(true);
  45. const wordList = await ses.listWordsInSpellCheckerDictionary();
  46. expect(wordList).to.eql(['foobar']);
  47. });
  48. it('should fail for an empty string', async () => {
  49. const result = ses.addWordToSpellCheckerDictionary('');
  50. expect(result).to.equal(false);
  51. const wordList = await ses.listWordsInSpellCheckerDictionary;
  52. expect(wordList).to.have.length(0);
  53. });
  54. // remove API will always return false because we can't add words
  55. it('should fail for non-persistent sessions', async () => {
  56. const tempSes = session.fromPartition('temporary');
  57. const result = tempSes.addWordToSpellCheckerDictionary('foobar');
  58. expect(result).to.equal(false);
  59. });
  60. });
  61. describe('ses.removeWordFromSpellCheckerDictionary', () => {
  62. it('should successfully remove words to custom dictionary', async () => {
  63. const result1 = ses.addWordToSpellCheckerDictionary('foobar');
  64. expect(result1).to.equal(true);
  65. const wordList1 = await ses.listWordsInSpellCheckerDictionary();
  66. expect(wordList1).to.eql(['foobar']);
  67. const result2 = ses.removeWordFromSpellCheckerDictionary('foobar');
  68. expect(result2).to.equal(true);
  69. const wordList2 = await ses.listWordsInSpellCheckerDictionary();
  70. expect(wordList2).to.have.length(0);
  71. });
  72. it('should fail for words not in custom dictionary', () => {
  73. const result2 = ses.removeWordFromSpellCheckerDictionary('foobar');
  74. expect(result2).to.equal(false);
  75. });
  76. });
  77. });
  78. });