import {Message} from 'nylas-exports';
import SignatureComposerExtension from '../lib/signature-composer-extension';
import SignatureStore from '../lib/signature-store';
const TEST_SIGNATURE = '
This is my signature.
';
describe("SignatureComposerExtension", ()=> {
describe("finalizeSessionBeforeSending", ()=> {
it("should unwrap the signature and remove the custom DOM element", ()=> {
const a = new Message({
draft: true,
accountId: TEST_ACCOUNT_ID,
body: `This is a test! ${TEST_SIGNATURE}
Hello world
`,
});
const add = jasmine.createSpy('changes.add');
const session = {
draft: ()=> a,
changes: {
add: add,
},
}
SignatureComposerExtension.finalizeSessionBeforeSending({session});
expect(add).toHaveBeenCalledWith({
body: `This is a test! ${TEST_SIGNATURE}
Hello world
`,
})
});
});
describe("prepareNewDraft", ()=> {
describe("when a signature is defined", ()=> {
beforeEach(()=> {
spyOn(NylasEnv.config, 'get').andCallFake(()=> TEST_SIGNATURE);
});
it("should insert the signature at the end of the message or before the first quoted text block and have a newline", ()=> {
const a = new Message({
draft: true,
accountId: TEST_ACCOUNT_ID,
body: 'This is a test! Hello world
',
});
const b = new Message({
draft: true,
accountId: TEST_ACCOUNT_ID,
body: 'This is a another test.',
});
SignatureComposerExtension.prepareNewDraft({draft: a});
expect(a.body).toEqual(`This is a test! ${TEST_SIGNATURE}
Hello world
`);
SignatureComposerExtension.prepareNewDraft({draft: b});
expect(b.body).toEqual(`This is a another test.
${TEST_SIGNATURE}`);
});
const scenarios = [
{
name: 'With blockquote',
body: `This is a test! SIG
Hello world
`,
expected: `This is a test! ${TEST_SIGNATURE}
Hello world
`,
},
{
name: 'Populated signature div',
body: `This is a test!
SIG
`,
expected: `This is a test!
${TEST_SIGNATURE}`,
},
{
name: 'Empty signature div',
body: 'This is a test! ',
expected: `This is a test!
${TEST_SIGNATURE}`,
},
{
name: 'With newlines',
body: 'This is a test! \n
\nSIG
\n',
expected: `This is a test!
${TEST_SIGNATURE}`,
},
]
scenarios.forEach((scenario)=> {
it(`should replace the signature if a signature is already present (${scenario.name})`, ()=> {
const message = new Message({
draft: true,
body: scenario.body,
accountId: TEST_ACCOUNT_ID,
})
SignatureComposerExtension.prepareNewDraft({draft: message});
expect(message.body).toEqual(scenario.expected)
});
});
});
describe("when no signature is present in the config file", ()=> {
beforeEach(()=> {
spyOn(NylasEnv.config, 'get').andCallFake(()=> undefined);
});
it("should insert the default signature", ()=> {
const a = new Message({
draft: true,
accountId: TEST_ACCOUNT_ID,
body: 'This is a test! Hello world
',
});
SignatureComposerExtension.prepareNewDraft({draft: a});
expect(a.body).toEqual(`This is a test! ${SignatureStore.DefaultSignature}
Hello world
`);
});
});
describe("when a blank signature is present in the config file", ()=> {
beforeEach(()=> {
spyOn(NylasEnv.config, 'get').andCallFake(()=> "");
});
it("should insert nothing", ()=> {
const a = new Message({
draft: true,
accountId: TEST_ACCOUNT_ID,
body: 'This is a test! Hello world
',
});
SignatureComposerExtension.prepareNewDraft({draft: a});
expect(a.body).toEqual(`This is a test! Hello world
`);
});
});
});
});