2016-03-01 10:47:22 +08:00
|
|
|
import {Message} from 'nylas-exports';
|
|
|
|
import SignatureComposerExtension from '../lib/signature-composer-extension';
|
2016-03-15 08:04:40 +08:00
|
|
|
import SignatureStore from '../lib/signature-store';
|
2016-03-01 10:47:22 +08:00
|
|
|
|
2016-03-18 04:11:00 +08:00
|
|
|
const TEST_SIGNATURE = '<div class="something">This is my signature.</div>';
|
|
|
|
|
2016-03-18 08:20:30 +08:00
|
|
|
describe("SignatureComposerExtension", () => {
|
|
|
|
describe("applyTransformsToDraft", () => {
|
|
|
|
it("should unwrap the signature and remove the custom DOM element", () => {
|
2016-03-18 04:11:00 +08:00
|
|
|
const a = new Message({
|
|
|
|
draft: true,
|
|
|
|
accountId: TEST_ACCOUNT_ID,
|
|
|
|
body: `This is a test! <signature>${TEST_SIGNATURE}<br/></signature><div class="gmail_quote">Hello world</div>`,
|
|
|
|
});
|
2016-03-18 08:20:30 +08:00
|
|
|
const out = SignatureComposerExtension.applyTransformsToDraft({draft: a});
|
|
|
|
expect(out.body).toEqual(`This is a test! <!-- <signature> -->${TEST_SIGNATURE}<br/><!-- </signature> --><div class="gmail_quote">Hello world</div>`);
|
2016-03-18 04:11:00 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-18 08:20:30 +08:00
|
|
|
describe("prepareNewDraft", () => {
|
|
|
|
describe("when a signature is defined", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(NylasEnv.config, 'get').andCallFake(() => TEST_SIGNATURE);
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
|
2016-03-18 04:11:00 +08:00
|
|
|
it("should insert the signature at the end of the message or before the first quoted text block and have a newline", ()=> {
|
2016-03-01 10:47:22 +08:00
|
|
|
const a = new Message({
|
|
|
|
draft: true,
|
2016-03-15 08:04:40 +08:00
|
|
|
accountId: TEST_ACCOUNT_ID,
|
2016-03-18 04:11:00 +08:00
|
|
|
body: 'This is a test! <div class="gmail_quote">Hello world</div>',
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
const b = new Message({
|
|
|
|
draft: true,
|
2016-03-15 08:04:40 +08:00
|
|
|
accountId: TEST_ACCOUNT_ID,
|
2016-03-01 10:47:22 +08:00
|
|
|
body: 'This is a another test.',
|
|
|
|
});
|
|
|
|
|
|
|
|
SignatureComposerExtension.prepareNewDraft({draft: a});
|
2016-03-18 04:11:00 +08:00
|
|
|
expect(a.body).toEqual(`This is a test! <signature>${TEST_SIGNATURE}<br/></signature><div class="gmail_quote">Hello world</div>`);
|
2016-03-01 10:47:22 +08:00
|
|
|
SignatureComposerExtension.prepareNewDraft({draft: b});
|
2016-03-18 04:11:00 +08:00
|
|
|
expect(b.body).toEqual(`This is a another test.<signature><br/><br/>${TEST_SIGNATURE}</signature>`);
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
|
2016-03-18 04:11:00 +08:00
|
|
|
const scenarios = [
|
|
|
|
{
|
|
|
|
name: 'With blockquote',
|
|
|
|
body: `This is a test! <signature><div>SIG</div></signature><div class="gmail_quote">Hello world</div>`,
|
|
|
|
expected: `This is a test! <signature>${TEST_SIGNATURE}<br/></signature><div class="gmail_quote">Hello world</div>`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Populated signature div',
|
|
|
|
body: `This is a test! <signature><br/><br/><div>SIG</div></signature>`,
|
|
|
|
expected: `This is a test! <signature><br/><br/>${TEST_SIGNATURE}</signature>`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Empty signature div',
|
|
|
|
body: 'This is a test! <signature></signature>',
|
|
|
|
expected: `This is a test! <signature><br/><br/>${TEST_SIGNATURE}</signature>`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'With newlines',
|
|
|
|
body: 'This is a test! <signature>\n<br>\n<div>SIG</div>\n</signature>',
|
|
|
|
expected: `This is a test! <signature><br/><br/>${TEST_SIGNATURE}</signature>`,
|
|
|
|
},
|
|
|
|
]
|
2016-03-10 04:05:28 +08:00
|
|
|
|
2016-03-18 08:20:30 +08:00
|
|
|
scenarios.forEach((scenario) => {
|
|
|
|
it(`should replace the signature if a signature is already present (${scenario.name})`, () => {
|
2016-03-15 08:04:40 +08:00
|
|
|
const message = new Message({
|
|
|
|
draft: true,
|
|
|
|
body: scenario.body,
|
|
|
|
accountId: TEST_ACCOUNT_ID,
|
|
|
|
})
|
2016-03-10 04:05:28 +08:00
|
|
|
SignatureComposerExtension.prepareNewDraft({draft: message});
|
|
|
|
expect(message.body).toEqual(scenario.expected)
|
2016-03-18 04:11:00 +08:00
|
|
|
});
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-18 08:20:30 +08:00
|
|
|
describe("when no signature is present in the config file", () => {
|
2016-03-01 10:47:22 +08:00
|
|
|
beforeEach(()=> {
|
2016-03-18 08:20:30 +08:00
|
|
|
spyOn(NylasEnv.config, 'get').andCallFake(() => undefined);
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
|
2016-03-18 08:20:30 +08:00
|
|
|
it("should insert the default signature", () => {
|
2016-03-01 10:47:22 +08:00
|
|
|
const a = new Message({
|
|
|
|
draft: true,
|
2016-03-15 08:04:40 +08:00
|
|
|
accountId: TEST_ACCOUNT_ID,
|
2016-03-18 04:11:00 +08:00
|
|
|
body: 'This is a test! <div class="gmail_quote">Hello world</div>',
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
SignatureComposerExtension.prepareNewDraft({draft: a});
|
2016-03-18 04:11:00 +08:00
|
|
|
expect(a.body).toEqual(`This is a test! <signature>${SignatureStore.DefaultSignature}<br/></signature><div class="gmail_quote">Hello world</div>`);
|
2016-03-15 08:04:40 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-03-18 08:20:30 +08:00
|
|
|
describe("when a blank signature is present in the config file", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(NylasEnv.config, 'get').andCallFake(() => "");
|
2016-03-15 08:04:40 +08:00
|
|
|
});
|
|
|
|
|
2016-03-18 08:20:30 +08:00
|
|
|
it("should insert nothing", () => {
|
2016-03-15 08:04:40 +08:00
|
|
|
const a = new Message({
|
|
|
|
draft: true,
|
|
|
|
accountId: TEST_ACCOUNT_ID,
|
2016-03-18 04:11:00 +08:00
|
|
|
body: 'This is a test! <div class="gmail_quote">Hello world</div>',
|
2016-03-15 08:04:40 +08:00
|
|
|
});
|
|
|
|
SignatureComposerExtension.prepareNewDraft({draft: a});
|
2016-03-18 04:11:00 +08:00
|
|
|
expect(a.body).toEqual(`This is a test! <div class="gmail_quote">Hello world</div>`);
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|