2015-08-12 00:27:07 +08:00
|
|
|
import { blank } from 'helpers/qunit-helpers';
|
2015-08-11 05:11:27 +08:00
|
|
|
import DiscourseURL from "discourse/lib/url";
|
2015-04-02 00:23:27 +08:00
|
|
|
import ClickTrack from "discourse/lib/click-track";
|
|
|
|
|
2014-07-31 06:56:01 +08:00
|
|
|
var windowOpen,
|
|
|
|
win,
|
|
|
|
redirectTo;
|
|
|
|
|
2015-08-08 03:08:27 +08:00
|
|
|
module("lib:click-track", {
|
2013-06-20 03:06:23 +08:00
|
|
|
setup: function() {
|
|
|
|
|
|
|
|
// Prevent any of these tests from navigating away
|
2014-07-31 06:56:01 +08:00
|
|
|
win = {focus: function() { } };
|
2015-08-11 05:11:27 +08:00
|
|
|
redirectTo = sandbox.stub(DiscourseURL, "redirectTo");
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(Discourse, "ajax");
|
|
|
|
windowOpen = sandbox.stub(window, "open").returns(win);
|
|
|
|
sandbox.stub(win, "focus");
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2016-01-19 15:54:49 +08:00
|
|
|
fixture().html(
|
|
|
|
`<div id="topic" id="1337">
|
|
|
|
<article data-post-id="42" data-user-id="3141">
|
|
|
|
<a href="http://www.google.com">google.com</a>
|
|
|
|
<a class="lightbox back quote-other-topic" href="http://www.google.com">google.com</a>
|
|
|
|
<a id="with-badge" data-user-id="314" href="http://www.google.com">google.com<span class="badge">1</span></a>
|
|
|
|
<a id="with-badge-but-not-mine" href="http://www.google.com">google.com<span class="badge">1</span></a>
|
|
|
|
<div class="onebox-result">
|
|
|
|
<a id="inside-onebox" href="http://www.google.com">google.com<span class="badge">1</span></a>
|
|
|
|
<a id="inside-onebox-forced" class="track-link" href="http://www.google.com">google.com<span class="badge">1</span></a>
|
|
|
|
</div>
|
|
|
|
<a id="same-site" href="http://discuss.domain.com">forum</a>
|
|
|
|
<a class="attachment" href="http://discuss.domain.com/uploads/default/1234/1532357280.txt">log.txt</a>
|
2016-01-21 12:27:16 +08:00
|
|
|
<a class="hashtag" href="http://discuss.domain.com">#hashtag</a>
|
2016-01-19 15:54:49 +08:00
|
|
|
</article>
|
|
|
|
</div>`);
|
2013-06-20 03:06:23 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-04-02 00:23:27 +08:00
|
|
|
var track = ClickTrack.trackClick;
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2013-06-22 02:06:20 +08:00
|
|
|
// test
|
2013-06-20 03:06:23 +08:00
|
|
|
var generateClickEventOn = function(selector) {
|
2013-11-09 03:30:20 +08:00
|
|
|
return $.Event("click", { currentTarget: fixture(selector)[0] });
|
2013-06-22 02:06:20 +08:00
|
|
|
};
|
2013-06-20 03:06:23 +08:00
|
|
|
|
|
|
|
test("does not track clicks on lightboxes", function() {
|
|
|
|
var clickEvent = generateClickEventOn('.lightbox');
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(clickEvent, "preventDefault");
|
2013-06-20 03:06:23 +08:00
|
|
|
ok(track(clickEvent));
|
|
|
|
ok(!clickEvent.preventDefault.calledOnce);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("it calls preventDefault when clicking on an a", function() {
|
|
|
|
var clickEvent = generateClickEventOn('a');
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(clickEvent, "preventDefault");
|
2013-06-20 03:06:23 +08:00
|
|
|
track(clickEvent);
|
|
|
|
ok(clickEvent.preventDefault.calledOnce);
|
2015-08-11 05:11:27 +08:00
|
|
|
ok(DiscourseURL.redirectTo.calledOnce);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("does not track clicks on back buttons", function() {
|
2013-06-22 02:06:20 +08:00
|
|
|
ok(track(generateClickEventOn('.back')));
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("does not track clicks on quote buttons", function() {
|
2013-06-22 02:06:20 +08:00
|
|
|
ok(track(generateClickEventOn('.quote-other-topic')));
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2016-01-19 15:54:49 +08:00
|
|
|
test("does not track clicks on category badges", () => {
|
2016-01-21 12:27:16 +08:00
|
|
|
ok(!track(generateClickEventOn('.hashtag')));
|
2016-01-19 15:54:49 +08:00
|
|
|
});
|
|
|
|
|
2013-06-20 03:06:23 +08:00
|
|
|
test("removes the href and put it as a data attribute", function() {
|
|
|
|
track(generateClickEventOn('a'));
|
|
|
|
|
2013-11-09 03:30:20 +08:00
|
|
|
var $link = fixture('a').first();
|
2013-06-20 03:06:23 +08:00
|
|
|
ok($link.hasClass('no-href'));
|
|
|
|
equal($link.data('href'), 'http://www.google.com');
|
|
|
|
blank($link.attr('href'));
|
|
|
|
ok($link.data('auto-route'));
|
2015-08-11 05:11:27 +08:00
|
|
|
ok(DiscourseURL.redirectTo.calledOnce);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2015-08-12 05:34:02 +08:00
|
|
|
asyncTestDiscourse("restores the href after a while", function() {
|
2015-02-10 00:49:59 +08:00
|
|
|
expect(1);
|
|
|
|
|
|
|
|
track(generateClickEventOn('a'));
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
start();
|
|
|
|
equal(fixture('a').attr('href'), "http://www.google.com");
|
|
|
|
}, 75);
|
|
|
|
});
|
2013-06-20 03:06:23 +08:00
|
|
|
|
|
|
|
var badgeClickCount = function(id, expected) {
|
|
|
|
track(generateClickEventOn('#' + id));
|
2013-11-09 03:30:20 +08:00
|
|
|
var $badge = $('span.badge', fixture('#' + id).first());
|
2013-06-20 03:06:23 +08:00
|
|
|
equal(parseInt($badge.html(), 10), expected);
|
|
|
|
};
|
|
|
|
|
|
|
|
test("does not update badge clicks on my own link", function() {
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(Discourse.User, 'currentProp').withArgs('id').returns(314);
|
2013-06-20 03:06:23 +08:00
|
|
|
badgeClickCount('with-badge', 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("does not update badge clicks in my own post", function() {
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(Discourse.User, 'currentProp').withArgs('id').returns(3141);
|
2013-06-20 03:06:23 +08:00
|
|
|
badgeClickCount('with-badge-but-not-mine', 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("updates badge counts correctly", function() {
|
|
|
|
badgeClickCount('inside-onebox', 1);
|
|
|
|
badgeClickCount('inside-onebox-forced', 2);
|
|
|
|
badgeClickCount('with-badge', 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
var trackRightClick = function() {
|
2013-06-22 02:06:20 +08:00
|
|
|
var clickEvent = generateClickEventOn('a');
|
2013-06-20 03:06:23 +08:00
|
|
|
clickEvent.which = 3;
|
|
|
|
return track(clickEvent);
|
|
|
|
};
|
|
|
|
|
|
|
|
test("right clicks change the href", function() {
|
|
|
|
ok(trackRightClick());
|
2013-11-09 03:30:20 +08:00
|
|
|
equal(fixture('a').first().prop('href'), "http://www.google.com/");
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("right clicks are tracked", function() {
|
|
|
|
Discourse.SiteSettings.track_external_right_clicks = true;
|
|
|
|
trackRightClick();
|
2013-11-09 03:30:20 +08:00
|
|
|
equal(fixture('a').first().attr('href'), "/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42");
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2014-03-13 13:33:19 +08:00
|
|
|
test("preventDefault is not called for right clicks", function() {
|
|
|
|
var clickEvent = generateClickEventOn('a');
|
|
|
|
clickEvent.which = 3;
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(clickEvent, "preventDefault");
|
2014-03-13 13:33:19 +08:00
|
|
|
ok(track(clickEvent));
|
|
|
|
ok(!clickEvent.preventDefault.calledOnce);
|
|
|
|
});
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2014-03-13 13:33:19 +08:00
|
|
|
var testOpenInANewTab = function(description, clickEventModifier) {
|
|
|
|
test(description, function() {
|
|
|
|
var clickEvent = generateClickEventOn('a');
|
|
|
|
clickEventModifier(clickEvent);
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(clickEvent, "preventDefault");
|
2014-03-13 13:33:19 +08:00
|
|
|
ok(track(clickEvent));
|
|
|
|
ok(Discourse.ajax.calledOnce);
|
|
|
|
ok(!clickEvent.preventDefault.calledOnce);
|
|
|
|
});
|
2013-06-20 03:06:23 +08:00
|
|
|
};
|
|
|
|
|
2014-03-13 13:33:19 +08:00
|
|
|
testOpenInANewTab("it opens in a new tab when pressing shift", function(clickEvent) {
|
2013-06-20 03:06:23 +08:00
|
|
|
clickEvent.shiftKey = true;
|
|
|
|
});
|
|
|
|
|
2014-03-13 13:33:19 +08:00
|
|
|
testOpenInANewTab("it opens in a new tab when pressing meta", function(clickEvent) {
|
2013-06-20 03:06:23 +08:00
|
|
|
clickEvent.metaKey = true;
|
|
|
|
});
|
|
|
|
|
2014-03-13 13:33:19 +08:00
|
|
|
testOpenInANewTab("it opens in a new tab when pressing ctrl", function(clickEvent) {
|
2013-06-20 03:06:23 +08:00
|
|
|
clickEvent.ctrlKey = true;
|
|
|
|
});
|
|
|
|
|
2014-03-13 13:33:19 +08:00
|
|
|
testOpenInANewTab("it opens in a new tab on middle click", function(clickEvent) {
|
2013-06-20 03:06:23 +08:00
|
|
|
clickEvent.which = 2;
|
|
|
|
});
|
|
|
|
|
|
|
|
test("tracks via AJAX if we're on the same site", function() {
|
2015-08-11 05:11:27 +08:00
|
|
|
sandbox.stub(DiscourseURL, "routeTo");
|
|
|
|
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
2013-06-20 03:06:23 +08:00
|
|
|
|
|
|
|
ok(!track(generateClickEventOn('#same-site')));
|
|
|
|
ok(Discourse.ajax.calledOnce);
|
2015-08-11 05:11:27 +08:00
|
|
|
ok(DiscourseURL.routeTo.calledOnce);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2013-07-11 04:59:16 +08:00
|
|
|
test("does not track via AJAX for attachments", function() {
|
2015-08-11 05:11:27 +08:00
|
|
|
sandbox.stub(DiscourseURL, "routeTo");
|
|
|
|
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
2013-07-11 04:59:16 +08:00
|
|
|
|
|
|
|
ok(!track(generateClickEventOn('.attachment')));
|
2015-08-11 05:11:27 +08:00
|
|
|
ok(DiscourseURL.redirectTo.calledOnce);
|
2013-07-11 04:59:16 +08:00
|
|
|
});
|
2013-06-20 03:06:23 +08:00
|
|
|
|
|
|
|
test("tracks custom urls when opening in another window", function() {
|
|
|
|
var clickEvent = generateClickEventOn('a');
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(Discourse.User, "currentProp").withArgs('external_links_in_new_tab').returns(true);
|
2013-06-20 03:06:23 +08:00
|
|
|
ok(!track(clickEvent));
|
2014-07-31 06:56:01 +08:00
|
|
|
ok(windowOpen.calledWith('/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42', '_blank'));
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("tracks custom urls when opening in another window", function() {
|
|
|
|
var clickEvent = generateClickEventOn('a');
|
|
|
|
ok(!track(clickEvent));
|
2014-07-31 06:56:01 +08:00
|
|
|
ok(redirectTo.calledWith('/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42'));
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|