2019-10-30 21:48:24 +08:00
|
|
|
import { later } from "@ember/runloop";
|
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";
|
2019-07-02 16:08:28 +08:00
|
|
|
import { fixture, logIn } from "helpers/qunit-helpers";
|
2014-07-31 06:56:01 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.module("lib:click-track", {
|
|
|
|
beforeEach() {
|
2019-04-23 17:41:36 +08:00
|
|
|
logIn();
|
|
|
|
|
|
|
|
let win = { focus: function() {} };
|
|
|
|
sandbox.stub(window, "open").returns(win);
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(win, "focus");
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
sandbox.stub(DiscourseURL, "routeTo");
|
|
|
|
sandbox.stub(DiscourseURL, "redirectTo");
|
|
|
|
|
2017-03-28 04:16:14 +08:00
|
|
|
sessionStorage.clear();
|
|
|
|
|
2016-01-19 15:54:49 +08:00
|
|
|
fixture().html(
|
2016-05-02 23:43:58 +08:00
|
|
|
`<div id="topic" data-topic-id="1337">
|
2016-01-19 15:54:49 +08:00
|
|
|
<article data-post-id="42" data-user-id="3141">
|
|
|
|
<a href="http://www.google.com">google.com</a>
|
2017-10-07 03:14:08 +08:00
|
|
|
<a class="lightbox back" href="http://www.google.fr">google.fr</a>
|
2017-03-13 18:31:37 +08: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 15:54:49 +08:00
|
|
|
<div class="onebox-result">
|
2017-03-13 18:31:37 +08: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 15:54:49 +08:00
|
|
|
</div>
|
2017-03-13 18:31:37 +08:00
|
|
|
<a class="no-track-link" href="http://www.google.com.br">google.com.br</a>
|
2016-01-19 15:54:49 +08: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-21 12:27:16 +08:00
|
|
|
<a class="hashtag" href="http://discuss.domain.com">#hashtag</a>
|
2016-05-04 00:47:01 +08:00
|
|
|
<a class="mailto" href="mailto:foo@bar.com">email-me</a>
|
2019-05-06 23:38:57 +08:00
|
|
|
<a class="a-without-href">no href</a>
|
2016-05-04 00:47:01 +08:00
|
|
|
<aside class="quote">
|
2018-10-04 22:06:31 +08:00
|
|
|
<a href="https://discuss.domain.com/t/welcome-to-meta-discourse-org/1/30">foo</a>
|
|
|
|
<a href="https://google.com">bar</a>
|
2016-05-04 00:47:01 +08:00
|
|
|
</aside>
|
2016-01-19 15:54:49 +08:00
|
|
|
</article>
|
2018-06-15 23:03:24 +08:00
|
|
|
</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
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
function generateClickEventOn(selector) {
|
|
|
|
return $.Event("click", { currentTarget: fixture(selector).first() });
|
|
|
|
}
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("tracks internal URLs", async assert => {
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.expect(2);
|
|
|
|
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
const done = assert.async();
|
|
|
|
/* global server */
|
2019-05-07 18:53:45 +08:00
|
|
|
server.post("/clicks/track", request => {
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.ok(
|
2019-05-07 18:53:45 +08:00
|
|
|
request.requestBody,
|
|
|
|
"url=http%3A%2F%2Fdiscuss.domain.com&post_id=42&topic_id=1337"
|
2019-04-23 17:41:36 +08:00
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.notOk(track(generateClickEventOn("#same-site")));
|
2016-04-11 23:39:49 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not track elements with no href", async assert => {
|
2019-05-06 23:42:08 +08:00
|
|
|
assert.ok(track(generateClickEventOn(".a-without-href")));
|
2019-05-06 23:38:57 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not track attachments", async assert => {
|
2019-04-23 17:41:36 +08:00
|
|
|
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
|
|
|
|
|
|
|
/* global server */
|
2019-05-07 18:53:45 +08:00
|
|
|
server.post("/clicks/track", () => assert.ok(false));
|
2019-04-23 17:41:36 +08:00
|
|
|
|
2019-05-08 16:46:35 +08:00
|
|
|
assert.notOk(track(generateClickEventOn(".attachment")));
|
2019-05-08 17:07:52 +08:00
|
|
|
assert.ok(
|
|
|
|
DiscourseURL.redirectTo.calledWith(
|
|
|
|
"http://discuss.domain.com/uploads/default/1234/1532357280.txt"
|
|
|
|
)
|
|
|
|
);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("tracks external URLs", async assert => {
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.expect(2);
|
|
|
|
|
|
|
|
const done = assert.async();
|
|
|
|
/* global server */
|
2019-05-07 18:53:45 +08:00
|
|
|
server.post("/clicks/track", request => {
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.ok(
|
2019-05-07 18:53:45 +08:00
|
|
|
request.requestBody,
|
|
|
|
"url=http%3A%2F%2Fwww.google.com&post_id=42&topic_id=1337"
|
2019-04-23 17:41:36 +08:00
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.notOk(track(generateClickEventOn("a")));
|
2018-10-25 17:46:05 +08:00
|
|
|
});
|
|
|
|
|
2019-05-29 02:16:42 +08:00
|
|
|
QUnit.skip(
|
2019-04-23 17:41:36 +08:00
|
|
|
"tracks external URLs when opening in another window",
|
|
|
|
async assert => {
|
|
|
|
assert.expect(3);
|
|
|
|
Discourse.User.currentProp("external_links_in_new_tab", true);
|
2018-10-04 22:06:31 +08:00
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
const done = assert.async();
|
|
|
|
/* global server */
|
2019-05-07 18:53:45 +08:00
|
|
|
server.post("/clicks/track", request => {
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.ok(
|
2019-05-07 18:53:45 +08:00
|
|
|
request.requestBody,
|
|
|
|
"url=http%3A%2F%2Fwww.google.com&post_id=42&topic_id=1337"
|
2019-04-23 17:41:36 +08:00
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.notOk(track(generateClickEventOn("a")));
|
|
|
|
assert.ok(window.open.calledWith("http://www.google.com", "_blank"));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not track clicks on lightboxes", async assert => {
|
2019-04-29 22:27:30 +08:00
|
|
|
assert.notOk(track(generateClickEventOn(".lightbox")));
|
2018-10-04 22:06:31 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not track clicks when forcibly disabled", async assert => {
|
2019-04-29 22:27:30 +08:00
|
|
|
assert.notOk(track(generateClickEventOn(".no-track-link")));
|
2019-04-23 17:41:36 +08:00
|
|
|
});
|
2018-10-11 15:51:14 +08:00
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not track clicks on back buttons", async assert => {
|
2019-04-29 22:27:30 +08:00
|
|
|
assert.notOk(track(generateClickEventOn(".back")));
|
2019-04-23 17:41:36 +08:00
|
|
|
});
|
2018-10-11 15:51:14 +08:00
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not track right clicks inside quotes", async assert => {
|
2019-04-23 17:41:36 +08:00
|
|
|
const event = generateClickEventOn(".quote a:first-child");
|
|
|
|
event.which = 3;
|
|
|
|
assert.ok(track(event));
|
2018-10-11 15:51:14 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not track clicks links in quotes", async assert => {
|
2019-04-29 22:27:30 +08:00
|
|
|
Discourse.User.currentProp("external_links_in_new_tab", true);
|
|
|
|
assert.notOk(track(generateClickEventOn(".quote a:last-child")));
|
|
|
|
assert.ok(window.open.calledWith("https://google.com", "_blank"));
|
2016-05-04 00:47:01 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not track clicks on category badges", async assert => {
|
2019-04-29 22:27:30 +08:00
|
|
|
assert.notOk(track(generateClickEventOn(".hashtag")));
|
2016-05-04 00:47:01 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not track clicks on mailto", async assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(track(generateClickEventOn(".mailto")));
|
2016-01-19 15:54:49 +08:00
|
|
|
});
|
|
|
|
|
2019-05-29 02:16:42 +08:00
|
|
|
QUnit.skip("removes the href and put it as a data attribute", async assert => {
|
2019-04-23 17:41:36 +08:00
|
|
|
Discourse.User.currentProp("external_links_in_new_tab", true);
|
|
|
|
|
|
|
|
assert.notOk(track(generateClickEventOn("a")));
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2018-06-15 23:03:24 +08: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"));
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.ok(window.open.calledWith("http://www.google.com", "_blank"));
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("restores the href after a while", async assert => {
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.expect(2);
|
2015-02-10 00:49:59 +08:00
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.notOk(track(generateClickEventOn("a")));
|
2015-02-10 00:49:59 +08:00
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
assert.timeout(75);
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
const done = assert.async();
|
2019-10-30 21:48:24 +08:00
|
|
|
later(() => {
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(fixture("a").attr("href"), "http://www.google.com");
|
2019-07-02 16:08:28 +08:00
|
|
|
done();
|
|
|
|
});
|
2015-02-10 00:49:59 +08:00
|
|
|
});
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
function badgeClickCount(assert, id, expected) {
|
2018-06-15 23:03:24 +08:00
|
|
|
track(generateClickEventOn("#" + id));
|
|
|
|
var $badge = $("span.badge", fixture("#" + id).first());
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(parseInt($badge.html(), 10), expected);
|
2019-04-23 17:41:36 +08:00
|
|
|
}
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not update badge clicks on my own link", async assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
sandbox
|
|
|
|
.stub(Discourse.User, "currentProp")
|
|
|
|
.withArgs("id")
|
|
|
|
.returns(314);
|
|
|
|
badgeClickCount(assert, "with-badge", 1);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("does not update badge clicks in my own post", async assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
sandbox
|
|
|
|
.stub(Discourse.User, "currentProp")
|
|
|
|
.withArgs("id")
|
|
|
|
.returns(3141);
|
|
|
|
badgeClickCount(assert, "with-badge-but-not-mine", 1);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip("updates badge counts correctly", async assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
badgeClickCount(assert, "inside-onebox", 1);
|
|
|
|
badgeClickCount(assert, "inside-onebox-forced", 2);
|
|
|
|
badgeClickCount(assert, "with-badge", 2);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
function testOpenInANewTab(description, clickEventModifier) {
|
2019-07-02 16:08:28 +08:00
|
|
|
QUnit.skip(description, async assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
var clickEvent = generateClickEventOn("a");
|
2014-03-13 13:33:19 +08:00
|
|
|
clickEventModifier(clickEvent);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.ok(track(clickEvent));
|
2019-04-23 17:41:36 +08:00
|
|
|
assert.notOk(clickEvent.defaultPrevented);
|
2014-03-13 13:33:19 +08:00
|
|
|
});
|
2019-04-23 17:41:36 +08:00
|
|
|
}
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
testOpenInANewTab("it opens in a new tab when pressing shift", clickEvent => {
|
2013-06-20 03:06:23 +08:00
|
|
|
clickEvent.shiftKey = true;
|
|
|
|
});
|
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
testOpenInANewTab("it opens in a new tab when pressing meta", clickEvent => {
|
2013-06-20 03:06:23 +08:00
|
|
|
clickEvent.metaKey = true;
|
|
|
|
});
|
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
testOpenInANewTab("it opens in a new tab when pressing ctrl", clickEvent => {
|
2013-06-20 03:06:23 +08:00
|
|
|
clickEvent.ctrlKey = true;
|
|
|
|
});
|
|
|
|
|
2019-04-23 17:41:36 +08:00
|
|
|
testOpenInANewTab("it opens in a new tab on middle click", clickEvent => {
|
2016-04-29 04:13:34 +08:00
|
|
|
clickEvent.button = 2;
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|