Mailspring/internal_packages/link-tracking/spec/link-tracking-after-send-spec.es6
Drew Regitsky b2db5190c8 test(plugins): Add specs, refactor/fixes for open and link tracking
Summary:
Add specs to test the components of open tracking and link
tracking. Notably does not test the overall functionality, which
still needs specs.

Test Plan: adds specs

Reviewers: juan, evan, bengotow

Reviewed By: evan, bengotow

Differential Revision: https://phab.nylas.com/D2667
2016-03-07 20:54:43 -08:00

63 lines
2.3 KiB
JavaScript

import {Message} from 'nylas-exports'
import {PLUGIN_ID, PLUGIN_URL} from '../lib/link-tracking-constants';
import LinkTrackingAfterSend from '../lib/link-tracking-after-send'
describe("Link tracking afterDraftSend callback", () => {
beforeEach(() => {
this.message = new Message();
this.postResponse = 200;
spyOn(LinkTrackingAfterSend, "post").andCallFake(() => Promise.resolve([{statusCode: this.postResponse}, ""]));
spyOn(NylasEnv, "isMainWindow").andReturn(true);
});
it("takes no action when the message has no metadata", () => {
LinkTrackingAfterSend.afterDraftSend({message: this.message});
expect(LinkTrackingAfterSend.post).not.toHaveBeenCalled();
});
it("takes no action when the message has malformed metadata", () => {
this.message.applyPluginMetadata(PLUGIN_ID, {gar: "bage"});
LinkTrackingAfterSend.afterDraftSend({message: this.message});
expect(LinkTrackingAfterSend.post).not.toHaveBeenCalled();
});
describe("When metadata is present", () => {
beforeEach(() => {
this.metadata = {uid: "TEST_UID"};
this.message.applyPluginMetadata(PLUGIN_ID, this.metadata);
});
it("posts UID => message ID to the server", () => {
// Spy on the POST request, then call the afterDraftSend function
LinkTrackingAfterSend.afterDraftSend({message: this.message});
expect(LinkTrackingAfterSend.post).toHaveBeenCalled();
const {url, body} = LinkTrackingAfterSend.post.mostRecentCall.args[0];
const {uid, message_id} = JSON.parse(body);
expect(url).toEqual(`${PLUGIN_URL}/plugins/register-message`);
expect(uid).toEqual(this.metadata.uid);
expect(message_id).toEqual(this.message.id);
});
it("shows an error dialog if the request fails", () => {
// Spy on the POST request and dialog function
this.postResponse = 400;
spyOn(NylasEnv, "showErrorDialog");
spyOn(NylasEnv, "reportError");
LinkTrackingAfterSend.afterDraftSend({message: this.message});
expect(LinkTrackingAfterSend.post).toHaveBeenCalled();
waitsFor(() => {
return NylasEnv.reportError.callCount > 0;
});
runs(() => {
expect(NylasEnv.showErrorDialog).toHaveBeenCalled();
expect(NylasEnv.reportError).toHaveBeenCalled();
});
});
});
});