2016-03-08 12:54:43 +08:00
|
|
|
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`;
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
const quote = `<blockquote class="gmail_quote"> twst </blockquote>`;
|
|
|
|
const testBody = `<head></head><body>${testContent}${quote}</body>`;
|
2016-03-08 12:54:43 +08:00
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
const replacedBody = (accountId, messageUid, unquoted) =>
|
|
|
|
`<head></head><body>${replacedContent(accountId, messageUid)}${unquoted ? "" : quote}</body>`;
|
|
|
|
|
|
|
|
describe("Link tracking composer extension", () => {
|
2016-03-08 12:54:43 +08:00
|
|
|
// Set up a draft, session that returns the draft, and metadata
|
2016-03-17 10:27:12 +08:00
|
|
|
beforeEach(() => {
|
2016-03-08 12:54:43 +08:00
|
|
|
this.draft = new Message({accountId: "test"});
|
|
|
|
this.draft.body = testBody;
|
|
|
|
});
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
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);
|
2016-03-08 12:54:43 +08:00
|
|
|
});
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
describe("With properly formatted metadata and correct params", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
this.metadata = {tracked: true};
|
|
|
|
this.draft.applyPluginMetadata(PLUGIN_ID, this.metadata);
|
|
|
|
});
|
2016-03-08 12:54:43 +08:00
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
it("replaces links in the unquoted portion of the body", () => {
|
|
|
|
const out = LinkTrackingComposerExtension.applyTransformsToDraft({draft: this.draft});
|
|
|
|
const outUnquoted = QuotedHTMLTransformer.removeQuotedHTML(out.body);
|
2016-03-08 12:54:43 +08:00
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
expect(outUnquoted).toContain(replacedBody(this.draft.accountId, this.metadata.uid, true));
|
|
|
|
expect(out.body).toContain(replacedBody(this.draft.accountId, this.metadata.uid, false));
|
2016-03-08 12:54:43 +08:00
|
|
|
});
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-03-08 12:54:43 +08:00
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
describe("unapplyTransformsToDraft", () => {
|
|
|
|
it("takes no action if there are no tracked links in the body", () => {
|
|
|
|
const out = LinkTrackingComposerExtension.unapplyTransformsToDraft({
|
|
|
|
draft: this.draft.clone(),
|
2016-03-08 12:54:43 +08:00
|
|
|
});
|
2016-03-17 10:27:12 +08:00
|
|
|
expect(out.body).toEqual(this.draft.body);
|
|
|
|
});
|
2016-03-08 12:54:43 +08:00
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
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(),
|
2016-03-08 12:54:43 +08:00
|
|
|
});
|
2016-03-17 10:27:12 +08:00
|
|
|
expect(withoutImg.body).toEqual(this.draft.body);
|
|
|
|
});
|
2016-03-08 12:54:43 +08:00
|
|
|
});
|
|
|
|
});
|