2016-05-02 11:43:58 -04:00
|
|
|
import DiscourseURL from "discourse/lib/url";
|
|
|
|
import ClickTrack from "discourse/lib/click-track";
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
var windowOpen, win, redirectTo;
|
2016-05-02 11:43:58 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.module("lib:click-track-profile-page", {
|
|
|
|
beforeEach() {
|
2016-05-02 11:43:58 -04:00
|
|
|
// Prevent any of these tests from navigating away
|
2018-06-15 11:03:24 -04:00
|
|
|
win = { focus: function() {} };
|
2016-05-02 11:43:58 -04:00
|
|
|
redirectTo = sandbox.stub(DiscourseURL, "redirectTo");
|
|
|
|
windowOpen = sandbox.stub(window, "open").returns(win);
|
|
|
|
sandbox.stub(win, "focus");
|
|
|
|
|
|
|
|
fixture().html(
|
|
|
|
`<p class="excerpt first" data-post-id="42" data-topic-id="1337" 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.com">google.com</a>
|
2016-05-02 11:43:58 -04:00
|
|
|
<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 class="no-track-link" href="http://www.google.com">google.com</a>
|
|
|
|
<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>
|
|
|
|
<a class="hashtag" href="http://discuss.domain.com">#hashtag</a>
|
|
|
|
</p>
|
|
|
|
<p class="excerpt second" data-post-id="24" data-topic-id="7331" data-user-id="1413">
|
|
|
|
<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.com">google.com</a>
|
2016-05-02 11:43:58 -04:00
|
|
|
<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 class="no-track-link" href="http://www.google.com">google.com</a>
|
|
|
|
<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>
|
|
|
|
<a class="hashtag" href="http://discuss.domain.com">#hashtag</a>
|
2018-06-15 11:03:24 -04:00
|
|
|
</p>`
|
|
|
|
);
|
2016-05-02 11:43:58 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var track = ClickTrack.trackClick;
|
|
|
|
|
|
|
|
// test
|
|
|
|
var generateClickEventOn = function(selector) {
|
|
|
|
return $.Event("click", { currentTarget: fixture(selector)[0] });
|
|
|
|
};
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("does not track clicks on lightboxes", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
var clickEvent = generateClickEventOn(".lightbox");
|
2016-05-02 11:43:58 -04:00
|
|
|
sandbox.stub(clickEvent, "preventDefault");
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(track(clickEvent));
|
|
|
|
assert.ok(!clickEvent.preventDefault.calledOnce);
|
2016-05-02 11:43:58 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("it calls preventDefault when clicking on an a", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
var clickEvent = generateClickEventOn("a");
|
2016-05-02 11:43:58 -04:00
|
|
|
sandbox.stub(clickEvent, "preventDefault");
|
|
|
|
track(clickEvent);
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(clickEvent.preventDefault.calledOnce);
|
|
|
|
assert.ok(DiscourseURL.redirectTo.calledOnce);
|
2016-05-02 11:43:58 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("does not track clicks when forcibly disabled", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(track(generateClickEventOn(".no-track-link")));
|
2016-05-02 11:43:58 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("does not track clicks on back buttons", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(track(generateClickEventOn(".back")));
|
2016-05-02 11:43:58 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("does not track clicks on category badges", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(track(generateClickEventOn(".hashtag")));
|
2016-05-02 11:43:58 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("removes the href and put it as a data attribute", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
track(generateClickEventOn("a"));
|
2016-05-02 11:43:58 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
var $link = fixture("a").first();
|
|
|
|
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"));
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(DiscourseURL.redirectTo.calledOnce);
|
2016-05-02 11:43:58 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
asyncTestDiscourse("restores the href after a while", function(assert) {
|
|
|
|
assert.expect(1);
|
2016-05-02 11:43:58 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
track(generateClickEventOn("a"));
|
2016-05-02 11:43:58 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
const done = assert.async();
|
2016-05-02 11:43:58 -04:00
|
|
|
setTimeout(function() {
|
2017-06-14 13:57:58 -04:00
|
|
|
done();
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.equal(fixture("a").attr("href"), "http://www.google.com");
|
2016-05-02 11:43:58 -04:00
|
|
|
}, 75);
|
|
|
|
});
|
|
|
|
|
|
|
|
var testOpenInANewTab = function(description, clickEventModifier) {
|
2017-06-14 13:57:58 -04:00
|
|
|
test(description, function(assert) {
|
2018-06-15 11:03:24 -04:00
|
|
|
var clickEvent = generateClickEventOn("a");
|
2016-05-02 11:43:58 -04:00
|
|
|
clickEventModifier(clickEvent);
|
|
|
|
sandbox.stub(clickEvent, "preventDefault");
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(track(clickEvent));
|
|
|
|
assert.ok(!clickEvent.preventDefault.calledOnce);
|
2016-05-02 11:43:58 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
testOpenInANewTab("it opens in a new tab when pressing shift", function(
|
|
|
|
clickEvent
|
|
|
|
) {
|
2016-05-02 11:43:58 -04:00
|
|
|
clickEvent.shiftKey = true;
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
testOpenInANewTab("it opens in a new tab when pressing meta", function(
|
|
|
|
clickEvent
|
|
|
|
) {
|
2016-05-02 11:43:58 -04:00
|
|
|
clickEvent.metaKey = true;
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
testOpenInANewTab("it opens in a new tab when pressing ctrl", function(
|
|
|
|
clickEvent
|
|
|
|
) {
|
2016-05-02 11:43:58 -04:00
|
|
|
clickEvent.ctrlKey = true;
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
testOpenInANewTab("it opens in a new tab on middle click", function(
|
|
|
|
clickEvent
|
|
|
|
) {
|
2016-05-02 11:43:58 -04:00
|
|
|
clickEvent.button = 2;
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("tracks via AJAX if we're on the same site", assert => {
|
2016-05-02 11:43:58 -04:00
|
|
|
sandbox.stub(DiscourseURL, "routeTo");
|
|
|
|
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(!track(generateClickEventOn("#same-site")));
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(DiscourseURL.routeTo.calledOnce);
|
2016-05-02 11:43:58 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("does not track via AJAX for attachments", assert => {
|
2016-05-02 11:43:58 -04:00
|
|
|
sandbox.stub(DiscourseURL, "routeTo");
|
|
|
|
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(!track(generateClickEventOn(".attachment")));
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(DiscourseURL.redirectTo.calledOnce);
|
2016-05-02 11:43:58 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("tracks custom urls when opening in another window", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
var clickEvent = generateClickEventOn("a");
|
|
|
|
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));
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(
|
|
|
|
windowOpen.calledWith(
|
|
|
|
"/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42&topic_id=1337",
|
|
|
|
"_blank"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test(
|
|
|
|
"tracks custom urls on second excerpt when opening in another window",
|
|
|
|
assert => {
|
|
|
|
var clickEvent = generateClickEventOn(".second a");
|
|
|
|
sandbox
|
|
|
|
.stub(Discourse.User, "currentProp")
|
|
|
|
.withArgs("external_links_in_new_tab")
|
|
|
|
.returns(true);
|
|
|
|
assert.ok(!track(clickEvent));
|
|
|
|
assert.ok(
|
|
|
|
windowOpen.calledWith(
|
|
|
|
"/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=24&topic_id=7331",
|
|
|
|
"_blank"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2016-05-02 11:43:58 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("tracks custom urls when opening in another window", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
var clickEvent = generateClickEventOn("a");
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(!track(clickEvent));
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(
|
|
|
|
redirectTo.calledWith(
|
|
|
|
"/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42&topic_id=1337"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test(
|
|
|
|
"tracks custom urls on second excerpt when opening in another window",
|
|
|
|
assert => {
|
|
|
|
var clickEvent = generateClickEventOn(".second a");
|
|
|
|
assert.ok(!track(clickEvent));
|
|
|
|
assert.ok(
|
|
|
|
redirectTo.calledWith(
|
|
|
|
"/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=24&topic_id=7331"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|