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
test asdad adsasd stillhere
http://www.stillhere.com`; const replacedContent = (accountId, messageUid) => `TEST_BODY
test asdad adsasd stillhere
http://www.stillhere.com`; const quote = `
twst
`; const testBody = `${testContent}${quote}`; const replacedBody = (accountId, messageUid, unquoted) => `${replacedContent(accountId, messageUid)}${unquoted ? "" : quote}`; 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); }); }); });