import { Message, SignatureStore } from 'mailspring-exports';
import SignatureComposerExtension from '../lib/signature-composer-extension';
const TEST_ID = 1;
const TEST_SIGNATURE = {
id: TEST_ID,
title: 'test-sig',
body: '
This is my signature.
',
};
const TEST_SIGNATURES = {};
TEST_SIGNATURES[TEST_ID] = TEST_SIGNATURE;
describe('SignatureComposerExtension', function signatureComposerExtension() {
describe('prepareNewDraft', () => {
describe('when a signature is defined', () => {
beforeEach(() => {
spyOn(AppEnv.config, 'get').andCallFake(
key => (key === 'signatures' ? TEST_SIGNATURES : null)
);
spyOn(SignatureStore, 'signatureForEmail').andReturn(TEST_SIGNATURE);
SignatureStore.activate();
});
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,
from: ['one@nylas.com'],
accountId: TEST_ACCOUNT_ID,
body: 'This is a test! Hello world
',
});
const b = new Message({
draft: true,
from: ['one@nylas.com'],
accountId: TEST_ACCOUNT_ID,
body: 'This is a another test.',
});
SignatureComposerExtension.prepareNewDraft({ draft: a });
expect(a.body).toEqual(
`This is a test!
${
TEST_SIGNATURE.body
}Hello world
`
);
SignatureComposerExtension.prepareNewDraft({ draft: b });
expect(b.body).toEqual(
`This is a another test.
${TEST_SIGNATURE.body}`
);
});
const scenarios = [
{
name: 'With blockquote',
body: `This is a test! SIG
Hello world
`,
expected: `This is a test! ${
TEST_SIGNATURE.body
}Hello world
`,
},
{
name: 'Populated signature div',
body: `This is a test! SIG
`,
expected: `This is a test! ${TEST_SIGNATURE.body}`,
},
{
name: 'Empty signature div',
body: 'This is a test! ',
expected: `This is a test! ${TEST_SIGNATURE.body}`,
},
{
name: 'With newlines',
body: 'This is a test!
\n
\nSIG
\n',
expected: `This is a test!
${TEST_SIGNATURE.body}`,
},
];
scenarios.forEach(scenario => {
it(`should replace the signature if a signature is already present (${
scenario.name
})`, () => {
const message = new Message({
draft: true,
from: ['one@nylas.com'],
body: scenario.body,
accountId: TEST_ACCOUNT_ID,
});
SignatureComposerExtension.prepareNewDraft({ draft: message });
expect(message.body).toEqual(scenario.expected);
});
});
});
});
});