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