Mailspring/internal_packages/link-tracking/spec/link-tracking-composer-extension-spec.es6
Ben Gotow 552b66fbaf fix(syncback): Bidirectional transforms, ready-to-send saved state
Summary:
This diff replaces "finalizeSessionBeforeSending" with a
plugin hook that is bidirectional and allows us to put the draft in
the "ready to send" state every time we save it, and restore it to
the "ready to edit" state every time a draft session is created to
edit it.

This diff also significantly restructures the draft tasks:

1. SyncbackDraftUploadsTask:
   - ensures that `uploads` are converted to `files` and that any
     existing files on the draft are part of the correct account.

1. SyncbackDraftTask:
   - saves the draft, nothing else.

3. SendDraftTask
   - sends the draft, nothing else.
   - deletes the entire uploads directory for the draft

Test Plan: WIP

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D2753
2016-03-16 19:27:12 -07:00

89 lines
3.5 KiB
JavaScript

import LinkTrackingComposerExtension from '../lib/link-tracking-composer-extension'
import {PLUGIN_ID, PLUGIN_URL} from '../lib/link-tracking-constants';
import {Message, QuotedHTMLTransformer} from 'nylas-exports';
const testContent = `TEST_BODY<br>
<a href="www.replaced.com">test</a>
<a style="color: #aaa" href="http://replaced">asdad</a>
<a hre="www.stillhere.com">adsasd</a>
<a stillhere="">stillhere</a>
<div href="stillhere"></div>
http://www.stillhere.com`;
const replacedContent = (accountId, messageUid) => `TEST_BODY<br>
<a href="${PLUGIN_URL}/link/${accountId}/${messageUid}/0?redirect=www.replaced.com">test</a>
<a style="color: #aaa" href="${PLUGIN_URL}/link/${accountId}/${messageUid}/1?redirect=http%3A%2F%2Freplaced">asdad</a>
<a hre="www.stillhere.com">adsasd</a>
<a stillhere="">stillhere</a>
<div href="stillhere"></div>
http://www.stillhere.com`;
const quote = `<blockquote class="gmail_quote"> twst </blockquote>`;
const testBody = `<head></head><body>${testContent}${quote}</body>`;
const replacedBody = (accountId, messageUid, unquoted) =>
`<head></head><body>${replacedContent(accountId, messageUid)}${unquoted ? "" : quote}</body>`;
describe("Link tracking composer extension", () => {
// Set up a draft, session that returns the draft, and metadata
beforeEach(() => {
this.draft = new Message({accountId: "test"});
this.draft.body = testBody;
});
describe("applyTransformsToDraft", () => {
it("takes no action if there is no metadata", () => {
const out = LinkTrackingComposerExtension.applyTransformsToDraft({draft: this.draft});
expect(out.body).toEqual(this.draft.body);
});
describe("With properly formatted metadata and correct params", () => {
beforeEach(() => {
this.metadata = {tracked: true};
this.draft.applyPluginMetadata(PLUGIN_ID, this.metadata);
});
it("replaces links in the unquoted portion of the body", () => {
const out = LinkTrackingComposerExtension.applyTransformsToDraft({draft: this.draft});
const outUnquoted = QuotedHTMLTransformer.removeQuotedHTML(out.body);
expect(outUnquoted).toContain(replacedBody(this.draft.accountId, this.metadata.uid, true));
expect(out.body).toContain(replacedBody(this.draft.accountId, this.metadata.uid, false));
});
it("sets a uid and list of links on the metadata", () => {
LinkTrackingComposerExtension.applyTransformsToDraft({draft: this.draft});
expect(this.metadata.uid).not.toBeUndefined();
expect(this.metadata.links).not.toBeUndefined();
expect(this.metadata.links.length).toEqual(2);
for (const link of this.metadata.links) {
expect(link.click_count).toEqual(0);
}
});
});
});
describe("unapplyTransformsToDraft", () => {
it("takes no action if there are no tracked links in the body", () => {
const out = LinkTrackingComposerExtension.unapplyTransformsToDraft({
draft: this.draft.clone(),
});
expect(out.body).toEqual(this.draft.body);
});
it("replaces tracked links with the original links, restoring the body exactly", () => {
this.metadata = {tracked: true};
this.draft.applyPluginMetadata(PLUGIN_ID, this.metadata);
const withImg = LinkTrackingComposerExtension.applyTransformsToDraft({
draft: this.draft.clone(),
});
const withoutImg = LinkTrackingComposerExtension.unapplyTransformsToDraft({
draft: withImg.clone(),
});
expect(withoutImg.body).toEqual(this.draft.body);
});
});
});