Mailspring/internal_packages/open-tracking/spec/open-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

68 lines
2.3 KiB
JavaScript

import {Message} from 'nylas-exports'
import {PLUGIN_ID, PLUGIN_URL} from '../lib/open-tracking-constants';
import OpenTrackingAfterSend from '../lib/open-tracking-after-send'
function fakeResponse(statusCode, body) {
return [{statusCode}, body];
}
describe("Open tracking afterDraftSend callback", () => {
beforeEach(() => {
this.message = new Message();
this.postResponse = 200;
spyOn(OpenTrackingAfterSend, "post").andCallFake(() => Promise.resolve(fakeResponse(this.postResponse, "")));
spyOn(NylasEnv, "isMainWindow").andReturn(true);
});
it("takes no action when the message has no metadata", () => {
OpenTrackingAfterSend.afterDraftSend({message: this.message});
expect(OpenTrackingAfterSend.post).not.toHaveBeenCalled();
});
it("takes no action when the message has malformed metadata", () => {
this.message.applyPluginMetadata(PLUGIN_ID, {gar: "bage"});
OpenTrackingAfterSend.afterDraftSend({message: this.message});
expect(OpenTrackingAfterSend.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
OpenTrackingAfterSend.afterDraftSend({message: this.message});
expect(OpenTrackingAfterSend.post).toHaveBeenCalled();
const {url, body} = OpenTrackingAfterSend.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");
OpenTrackingAfterSend.afterDraftSend({message: this.message});
expect(OpenTrackingAfterSend.post).toHaveBeenCalled();
waitsFor(() => {
return NylasEnv.reportError.callCount > 0;
});
runs(() => {
expect(NylasEnv.showErrorDialog).toHaveBeenCalled();
expect(NylasEnv.reportError).toHaveBeenCalled();
});
});
});
});