Mailspring/internal_packages/composer-templates/spec/template-store-spec.es6

181 lines
6.4 KiB
Text
Raw Normal View History

import fs from 'fs';
import { remote } from 'electron';
import {Message, DraftStore} from 'nylas-exports';
import TemplateStore from '../lib/template-store';
const { shell } = remote;
const stubTemplatesDir = '~/.nylas-mail/templates';
const stubTemplateFiles = {
'template1.html': '<p>bla1</p>',
'template2.html': '<p>bla2</p>',
};
const stubTemplates = [
{id: 'template1.html', name: 'template1', path: `${stubTemplatesDir}/template1.html`},
{id: 'template2.html', name: 'template2', path: `${stubTemplatesDir}/template2.html`},
];
fix(spec): add support for async specs and disable misbehaving ones More spec fixes replace process.nextTick with setTimeout(fn, 0) for specs Also added an unspy in the afterEach Temporarily disable specs fix(spec): start fixing specs Summary: This is the WIP fix to our spec runner. Several tests have been completely commented out that will require substantially more work to fix. These have been added to our sprint backlog. Other tests have been fixed to update to new APIs or to deal with genuine bugs that were introduced without our knowing! The most common non-trivial change relates to observing the `NylasAPI` and `NylasAPIRequest`. We used to observe the arguments to `makeRequest`. Unfortunately `NylasAPIRequest.run` is argumentless. Instead you can do: `NylasAPIRequest.prototype.run.mostRecentCall.object.options` to get the `options` passed into the object. the `.object` property grabs the context of the spy when it was last called. Fixing these tests uncovered several concerning issues with our test runner. I spent a while tracking down why our participant-text-field-spec was failling every so often. I chose that spec because it was the first spec to likely fail, thereby requiring looking at the least number of preceding files. I tried binary searching, turning on and off, several files beforehand only to realize that the failure rate was not determined by a particular preceding test, but rather the existing and quantity of preceding tests, AND the number of console.log statements I had. There is some processor-dependent race condition going on that needs further investigation. I also discovered an issue with the file-download-spec. We were getting errors about it accessing a file, which was very suspicious given the code stubs out all fs access. This was caused due to a spec that called an async function outside ot a `waitsForPromise` block or a `waitsFor` block. The test completed, the spies were cleaned up, but the downstream async chain was still running. By the time the async chain finished the runner was already working on the next spec and the spies had been restored (causing the real fs access to run). Juan had an idea to kill the specs once one fails to prevent cascading failures. I'll implement this in the next diff update Test Plan: npm test Reviewers: juan, halla, jackie Differential Revision: https://phab.nylas.com/D3501 Disable other specs Disable more broken specs All specs turned off till passing state Use async-safe versions of spec functions Add async test spec Remove unused package code Remove canary spec
2016-12-13 04:12:20 +08:00
xdescribe('TemplateStore', function templateStore() {
2016-05-06 13:30:34 +08:00
beforeEach(() => {
spyOn(fs, 'mkdir');
2016-05-06 13:30:34 +08:00
spyOn(shell, 'showItemInFolder').andCallFake(() => {});
spyOn(fs, 'writeFile').andCallFake((path, contents, callback) => {
callback(null);
});
2016-05-06 13:30:34 +08:00
spyOn(fs, 'readFile').andCallFake((path, callback) => {
const filename = path.split('/').pop();
callback(null, stubTemplateFiles[filename]);
});
});
2016-05-06 13:30:34 +08:00
it('should create the templates folder if it does not exist', () => {
2016-05-07 07:06:16 +08:00
spyOn(fs, 'exists').andCallFake((path, callback) => callback(false));
refactor(templates): major additions and refactoring for the Templates plugin. Summary: Adds several new features to the templates plugin, fixes some existing bugs, and refactors existing code. New Plugin Features/Fixes: - Changes the templates editor in preferences to allow variables to be entered with `{{brackets}}`. Handles many contenteditable complexities to implement. - Better interaction for renaming and deleting of templates in the editor. - Changes tabbing behavior when using templates. Tabbing between variables now wraps around, and typing tab from outside a variable region highlights the closest region. - Prevents "Enter" key in the composer when inside a variable region, and strips all formatting/tags from within the region - this prevents major contenteditable issues that can result in inline CSS in the style of our variable regions, which will not be removed when sending. - Shows a warning when choosing a template if it will replace existing text in a draft. - Prevents invalid characters in template names (due to filenames, esp. on Windows), and shows an error message. Strips these characters from draft titles when making a template. - Fixes a bug where TemplateStore's initialization code was being called multiple times. New N1 code: - Several new methods in `DOMUtils` useful for working with contenteditable. - Implement some missing methods in `Editor` Refactor: - Major refactor/rewrite of template composer extension to use new DOMUtils methods and simplify the logic (while adding new functionality). Remaining issues: - `preferences-tempaltes.cjsx` and `template-editor.coffee` should be rewritten in ES6 for consistency - Need tests for new DOMUtils functions and for new Templates plugin code. Test Plan: manual, need to update specs Reviewers: evan, bengotow Reviewed By: evan, bengotow Subscribers: juan Differential Revision: https://phab.nylas.com/D2382
2015-12-30 07:11:04 +08:00
TemplateStore._init(stubTemplatesDir);
expect(fs.mkdir).toHaveBeenCalled();
});
2016-05-06 13:30:34 +08:00
it('should expose templates in the templates directory', () => {
let watchCallback;
2016-05-06 13:30:34 +08:00
spyOn(fs, 'exists').andCallFake((path, callback) => { callback(true); });
2016-05-07 07:06:16 +08:00
spyOn(fs, 'watch').andCallFake((path, callback) => { watchCallback = callback });
2016-05-06 13:30:34 +08:00
spyOn(fs, 'readdir').andCallFake((path, callback) => { callback(null, Object.keys(stubTemplateFiles)); });
refactor(templates): major additions and refactoring for the Templates plugin. Summary: Adds several new features to the templates plugin, fixes some existing bugs, and refactors existing code. New Plugin Features/Fixes: - Changes the templates editor in preferences to allow variables to be entered with `{{brackets}}`. Handles many contenteditable complexities to implement. - Better interaction for renaming and deleting of templates in the editor. - Changes tabbing behavior when using templates. Tabbing between variables now wraps around, and typing tab from outside a variable region highlights the closest region. - Prevents "Enter" key in the composer when inside a variable region, and strips all formatting/tags from within the region - this prevents major contenteditable issues that can result in inline CSS in the style of our variable regions, which will not be removed when sending. - Shows a warning when choosing a template if it will replace existing text in a draft. - Prevents invalid characters in template names (due to filenames, esp. on Windows), and shows an error message. Strips these characters from draft titles when making a template. - Fixes a bug where TemplateStore's initialization code was being called multiple times. New N1 code: - Several new methods in `DOMUtils` useful for working with contenteditable. - Implement some missing methods in `Editor` Refactor: - Major refactor/rewrite of template composer extension to use new DOMUtils methods and simplify the logic (while adding new functionality). Remaining issues: - `preferences-tempaltes.cjsx` and `template-editor.coffee` should be rewritten in ES6 for consistency - Need tests for new DOMUtils functions and for new Templates plugin code. Test Plan: manual, need to update specs Reviewers: evan, bengotow Reviewed By: evan, bengotow Subscribers: juan Differential Revision: https://phab.nylas.com/D2382
2015-12-30 07:11:04 +08:00
TemplateStore._init(stubTemplatesDir);
watchCallback();
expect(TemplateStore.items()).toEqual(stubTemplates);
});
2016-05-06 13:30:34 +08:00
it('should watch the templates directory and reflect changes', () => {
let watchCallback = null;
let watchFired = false;
2016-05-06 13:30:34 +08:00
spyOn(fs, 'exists').andCallFake((path, callback) => callback(true));
2016-05-07 07:06:16 +08:00
spyOn(fs, 'watch').andCallFake((path, callback) => { watchCallback = callback });
2016-05-06 13:30:34 +08:00
spyOn(fs, 'readdir').andCallFake((path, callback) => {
if (watchFired) {
callback(null, Object.keys(stubTemplateFiles));
} else {
callback(null, []);
}
});
refactor(templates): major additions and refactoring for the Templates plugin. Summary: Adds several new features to the templates plugin, fixes some existing bugs, and refactors existing code. New Plugin Features/Fixes: - Changes the templates editor in preferences to allow variables to be entered with `{{brackets}}`. Handles many contenteditable complexities to implement. - Better interaction for renaming and deleting of templates in the editor. - Changes tabbing behavior when using templates. Tabbing between variables now wraps around, and typing tab from outside a variable region highlights the closest region. - Prevents "Enter" key in the composer when inside a variable region, and strips all formatting/tags from within the region - this prevents major contenteditable issues that can result in inline CSS in the style of our variable regions, which will not be removed when sending. - Shows a warning when choosing a template if it will replace existing text in a draft. - Prevents invalid characters in template names (due to filenames, esp. on Windows), and shows an error message. Strips these characters from draft titles when making a template. - Fixes a bug where TemplateStore's initialization code was being called multiple times. New N1 code: - Several new methods in `DOMUtils` useful for working with contenteditable. - Implement some missing methods in `Editor` Refactor: - Major refactor/rewrite of template composer extension to use new DOMUtils methods and simplify the logic (while adding new functionality). Remaining issues: - `preferences-tempaltes.cjsx` and `template-editor.coffee` should be rewritten in ES6 for consistency - Need tests for new DOMUtils functions and for new Templates plugin code. Test Plan: manual, need to update specs Reviewers: evan, bengotow Reviewed By: evan, bengotow Subscribers: juan Differential Revision: https://phab.nylas.com/D2382
2015-12-30 07:11:04 +08:00
TemplateStore._init(stubTemplatesDir);
expect(TemplateStore.items()).toEqual([]);
watchFired = true;
watchCallback();
expect(TemplateStore.items()).toEqual(stubTemplates);
});
2016-05-06 13:30:34 +08:00
describe('insertTemplateId', () => {
xit('should insert the template with the given id into the draft with the given id', () => {
let watchCallback;
2016-05-06 13:30:34 +08:00
spyOn(fs, 'exists').andCallFake((path, callback) => { callback(true); });
2016-05-07 07:06:16 +08:00
spyOn(fs, 'watch').andCallFake((path, callback) => { watchCallback = callback });
2016-05-06 13:30:34 +08:00
spyOn(fs, 'readdir').andCallFake((path, callback) => { callback(null, Object.keys(stubTemplateFiles)); });
refactor(templates): major additions and refactoring for the Templates plugin. Summary: Adds several new features to the templates plugin, fixes some existing bugs, and refactors existing code. New Plugin Features/Fixes: - Changes the templates editor in preferences to allow variables to be entered with `{{brackets}}`. Handles many contenteditable complexities to implement. - Better interaction for renaming and deleting of templates in the editor. - Changes tabbing behavior when using templates. Tabbing between variables now wraps around, and typing tab from outside a variable region highlights the closest region. - Prevents "Enter" key in the composer when inside a variable region, and strips all formatting/tags from within the region - this prevents major contenteditable issues that can result in inline CSS in the style of our variable regions, which will not be removed when sending. - Shows a warning when choosing a template if it will replace existing text in a draft. - Prevents invalid characters in template names (due to filenames, esp. on Windows), and shows an error message. Strips these characters from draft titles when making a template. - Fixes a bug where TemplateStore's initialization code was being called multiple times. New N1 code: - Several new methods in `DOMUtils` useful for working with contenteditable. - Implement some missing methods in `Editor` Refactor: - Major refactor/rewrite of template composer extension to use new DOMUtils methods and simplify the logic (while adding new functionality). Remaining issues: - `preferences-tempaltes.cjsx` and `template-editor.coffee` should be rewritten in ES6 for consistency - Need tests for new DOMUtils functions and for new Templates plugin code. Test Plan: manual, need to update specs Reviewers: evan, bengotow Reviewed By: evan, bengotow Subscribers: juan Differential Revision: https://phab.nylas.com/D2382
2015-12-30 07:11:04 +08:00
TemplateStore._init(stubTemplatesDir);
watchCallback();
const add = jasmine.createSpy('add');
2016-05-06 13:30:34 +08:00
spyOn(DraftStore, 'sessionForClientId').andCallFake(() => {
return Promise.resolve({changes: {add}});
});
2016-05-06 13:30:34 +08:00
runs(() => {
TemplateStore._onInsertTemplateId({
templateId: 'template1.html',
draftClientId: 'localid-draft',
});
});
2016-05-06 13:30:34 +08:00
waitsFor(() => add.calls.length > 0);
runs(() => {
expect(add).toHaveBeenCalledWith({
body: stubTemplateFiles['template1.html'],
});
});
});
});
2016-05-06 13:30:34 +08:00
describe('onCreateTemplate', () => {
beforeEach(() => {
let d;
2016-05-06 13:30:34 +08:00
spyOn(DraftStore, 'sessionForClientId').andCallFake((draftClientId) => {
if (draftClientId === 'localid-nosubject') {
d = new Message({subject: '', body: '<p>Body</p>'});
} else {
d = new Message({subject: 'Subject', body: '<p>Body</p>'});
}
const session = {draft() { return d; }};
return Promise.resolve(session);
});
refactor(templates): major additions and refactoring for the Templates plugin. Summary: Adds several new features to the templates plugin, fixes some existing bugs, and refactors existing code. New Plugin Features/Fixes: - Changes the templates editor in preferences to allow variables to be entered with `{{brackets}}`. Handles many contenteditable complexities to implement. - Better interaction for renaming and deleting of templates in the editor. - Changes tabbing behavior when using templates. Tabbing between variables now wraps around, and typing tab from outside a variable region highlights the closest region. - Prevents "Enter" key in the composer when inside a variable region, and strips all formatting/tags from within the region - this prevents major contenteditable issues that can result in inline CSS in the style of our variable regions, which will not be removed when sending. - Shows a warning when choosing a template if it will replace existing text in a draft. - Prevents invalid characters in template names (due to filenames, esp. on Windows), and shows an error message. Strips these characters from draft titles when making a template. - Fixes a bug where TemplateStore's initialization code was being called multiple times. New N1 code: - Several new methods in `DOMUtils` useful for working with contenteditable. - Implement some missing methods in `Editor` Refactor: - Major refactor/rewrite of template composer extension to use new DOMUtils methods and simplify the logic (while adding new functionality). Remaining issues: - `preferences-tempaltes.cjsx` and `template-editor.coffee` should be rewritten in ES6 for consistency - Need tests for new DOMUtils functions and for new Templates plugin code. Test Plan: manual, need to update specs Reviewers: evan, bengotow Reviewed By: evan, bengotow Subscribers: juan Differential Revision: https://phab.nylas.com/D2382
2015-12-30 07:11:04 +08:00
TemplateStore._init(stubTemplatesDir);
});
2016-05-06 13:30:34 +08:00
xit('should create a template with the given name and contents', () => {
const ref = TemplateStore.items();
TemplateStore._onCreateTemplate({name: '123', contents: 'bla'});
const item = (ref != null ? ref[0] : undefined);
expect(item.id).toBe('123.html');
expect(item.name).toBe('123');
expect(item.path.split('/').pop()).toBe('123.html');
});
2016-05-06 13:30:34 +08:00
xit('should display an error if no name is provided', () => {
spyOn(TemplateStore, '_displayError');
TemplateStore._onCreateTemplate({contents: 'bla'});
expect(TemplateStore._displayError).toHaveBeenCalled();
});
2016-05-06 13:30:34 +08:00
xit('should display an error if no content is provided', () => {
spyOn(TemplateStore, '_displayError');
TemplateStore._onCreateTemplate({name: 'bla'});
expect(TemplateStore._displayError).toHaveBeenCalled();
});
2016-05-06 13:30:34 +08:00
xit('should save the template file to the templates folder', () => {
TemplateStore._onCreateTemplate({name: '123', contents: 'bla'});
const path = `${stubTemplatesDir}/123.html`;
expect(fs.writeFile).toHaveBeenCalled();
expect(fs.writeFile.mostRecentCall.args[0]).toEqual(path);
expect(fs.writeFile.mostRecentCall.args[1]).toEqual('bla');
});
2016-05-06 13:30:34 +08:00
xit('should open the template so you can see it', () => {
TemplateStore._onCreateTemplate({name: '123', contents: 'bla'});
expect(shell.showItemInFolder).toHaveBeenCalled();
});
2016-05-06 13:30:34 +08:00
describe('when given a draft id', () => {
xit('should create a template from the name and contents of the given draft', () => {
spyOn(TemplateStore, 'trigger');
spyOn(TemplateStore, '_populate');
2016-05-06 13:30:34 +08:00
runs(() => {
TemplateStore._onCreateTemplate({draftClientId: 'localid-b'});
});
2016-05-07 07:06:16 +08:00
waitsFor(() => TemplateStore.trigger.callCount > 0);
2016-05-06 13:30:34 +08:00
runs(() => {
expect(TemplateStore.items().length).toEqual(1);
});
});
2016-05-06 13:30:34 +08:00
it('should display an error if the draft has no subject', () => {
spyOn(TemplateStore, '_displayError');
spyOn(fs, 'watch');
2016-05-06 13:30:34 +08:00
runs(() => {
TemplateStore._onCreateTemplate({draftClientId: 'localid-nosubject'});
});
2016-05-07 07:06:16 +08:00
waitsFor(() => TemplateStore._displayError.callCount > 0);
2016-05-06 13:30:34 +08:00
runs(() => {
expect(TemplateStore._displayError).toHaveBeenCalled();
});
});
});
});
2016-05-06 13:30:34 +08:00
describe('onShowTemplates', () => {
xit('should open the templates folder in the Finder', () => {
TemplateStore._onShowTemplates();
expect(shell.showItemInFolder).toHaveBeenCalled();
});
});
});