mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 03:36:18 +08:00
DEV: introduces prettier for es6 files
This commit is contained in:
@ -1,14 +1,12 @@
|
||||
import DiscourseURL from "discourse/lib/url";
|
||||
import ClickTrack from "discourse/lib/click-track";
|
||||
|
||||
var windowOpen,
|
||||
win,
|
||||
redirectTo;
|
||||
var windowOpen, win, redirectTo;
|
||||
|
||||
QUnit.module("lib:click-track", {
|
||||
beforeEach() {
|
||||
// Prevent any of these tests from navigating away
|
||||
win = {focus: function() { } };
|
||||
win = { focus: function() {} };
|
||||
redirectTo = sandbox.stub(DiscourseURL, "redirectTo");
|
||||
windowOpen = sandbox.stub(window, "open").returns(win);
|
||||
sandbox.stub(win, "focus");
|
||||
@ -35,7 +33,8 @@ QUnit.module("lib:click-track", {
|
||||
<a class="inside-quote" href="http://discuss.domain.com">foobar</a>
|
||||
</aside>
|
||||
</article>
|
||||
</div>`);
|
||||
</div>`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@ -47,14 +46,14 @@ var generateClickEventOn = function(selector) {
|
||||
};
|
||||
|
||||
QUnit.test("does not track clicks on lightboxes", function(assert) {
|
||||
var clickEvent = generateClickEventOn('.lightbox');
|
||||
var clickEvent = generateClickEventOn(".lightbox");
|
||||
sandbox.stub(clickEvent, "preventDefault");
|
||||
assert.ok(track(clickEvent));
|
||||
assert.ok(!clickEvent.preventDefault.calledOnce);
|
||||
});
|
||||
|
||||
QUnit.test("it calls preventDefault when clicking on an a", function(assert) {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
var clickEvent = generateClickEventOn("a");
|
||||
sandbox.stub(clickEvent, "preventDefault");
|
||||
track(clickEvent);
|
||||
assert.ok(clickEvent.preventDefault.calledOnce);
|
||||
@ -62,90 +61,106 @@ QUnit.test("it calls preventDefault when clicking on an a", function(assert) {
|
||||
});
|
||||
|
||||
QUnit.test("does not track clicks when forcibly disabled", function(assert) {
|
||||
assert.ok(track(generateClickEventOn('.no-track-link')));
|
||||
assert.ok(track(generateClickEventOn(".no-track-link")));
|
||||
});
|
||||
|
||||
QUnit.test("does not track clicks on back buttons", function(assert) {
|
||||
assert.ok(track(generateClickEventOn('.back')));
|
||||
assert.ok(track(generateClickEventOn(".back")));
|
||||
});
|
||||
|
||||
QUnit.test("does not track clicks in quotes", function(assert) {
|
||||
track(generateClickEventOn('.inside-quote'));
|
||||
track(generateClickEventOn(".inside-quote"));
|
||||
assert.ok(DiscourseURL.redirectTo.calledWith("http://discuss.domain.com"));
|
||||
});
|
||||
|
||||
QUnit.test("does not track clicks on category badges", assert => {
|
||||
assert.ok(track(generateClickEventOn('.hashtag')));
|
||||
assert.ok(track(generateClickEventOn(".hashtag")));
|
||||
});
|
||||
|
||||
QUnit.test("does not track clicks on mailto", function(assert) {
|
||||
assert.ok(track(generateClickEventOn('.mailto')));
|
||||
assert.ok(track(generateClickEventOn(".mailto")));
|
||||
});
|
||||
|
||||
QUnit.test("removes the href and put it as a data attribute", function(assert) {
|
||||
track(generateClickEventOn('a'));
|
||||
track(generateClickEventOn("a"));
|
||||
|
||||
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'));
|
||||
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"));
|
||||
assert.ok(DiscourseURL.redirectTo.calledOnce);
|
||||
});
|
||||
|
||||
asyncTestDiscourse("restores the href after a while", function(assert) {
|
||||
assert.expect(1);
|
||||
|
||||
track(generateClickEventOn('a'));
|
||||
track(generateClickEventOn("a"));
|
||||
|
||||
const done = assert.async();
|
||||
setTimeout(function() {
|
||||
done();
|
||||
assert.equal(fixture('a').attr('href'), "http://www.google.com");
|
||||
assert.equal(fixture("a").attr("href"), "http://www.google.com");
|
||||
}, 75);
|
||||
});
|
||||
|
||||
var badgeClickCount = function(assert, id, expected) {
|
||||
track(generateClickEventOn('#' + id));
|
||||
var $badge = $('span.badge', fixture('#' + id).first());
|
||||
track(generateClickEventOn("#" + id));
|
||||
var $badge = $("span.badge", fixture("#" + id).first());
|
||||
assert.equal(parseInt($badge.html(), 10), expected);
|
||||
};
|
||||
|
||||
QUnit.test("does not update badge clicks on my own link", function(assert) {
|
||||
sandbox.stub(Discourse.User, 'currentProp').withArgs('id').returns(314);
|
||||
badgeClickCount(assert, 'with-badge', 1);
|
||||
sandbox
|
||||
.stub(Discourse.User, "currentProp")
|
||||
.withArgs("id")
|
||||
.returns(314);
|
||||
badgeClickCount(assert, "with-badge", 1);
|
||||
});
|
||||
|
||||
QUnit.test("does not update badge clicks in my own post", function(assert) {
|
||||
sandbox.stub(Discourse.User, 'currentProp').withArgs('id').returns(3141);
|
||||
badgeClickCount(assert, 'with-badge-but-not-mine', 1);
|
||||
sandbox
|
||||
.stub(Discourse.User, "currentProp")
|
||||
.withArgs("id")
|
||||
.returns(3141);
|
||||
badgeClickCount(assert, "with-badge-but-not-mine", 1);
|
||||
});
|
||||
|
||||
QUnit.test("updates badge counts correctly", function(assert) {
|
||||
badgeClickCount(assert, 'inside-onebox', 1);
|
||||
badgeClickCount(assert, 'inside-onebox-forced', 2);
|
||||
badgeClickCount(assert, 'with-badge', 2);
|
||||
badgeClickCount(assert, "inside-onebox", 1);
|
||||
badgeClickCount(assert, "inside-onebox-forced", 2);
|
||||
badgeClickCount(assert, "with-badge", 2);
|
||||
});
|
||||
|
||||
var trackRightClick = function() {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
var clickEvent = generateClickEventOn("a");
|
||||
clickEvent.which = 3;
|
||||
return track(clickEvent);
|
||||
};
|
||||
|
||||
QUnit.test("right clicks change the href", function(assert) {
|
||||
assert.ok(trackRightClick());
|
||||
assert.equal(fixture('a').first().prop('href'), "http://www.google.com/");
|
||||
assert.equal(
|
||||
fixture("a")
|
||||
.first()
|
||||
.prop("href"),
|
||||
"http://www.google.com/"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("right clicks are tracked", function(assert) {
|
||||
Discourse.SiteSettings.track_external_right_clicks = true;
|
||||
trackRightClick();
|
||||
assert.equal(fixture('a').first().attr('href'), "/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42&topic_id=1337");
|
||||
assert.equal(
|
||||
fixture("a")
|
||||
.first()
|
||||
.attr("href"),
|
||||
"/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42&topic_id=1337"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("preventDefault is not called for right clicks", function(assert) {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
var clickEvent = generateClickEventOn("a");
|
||||
clickEvent.which = 3;
|
||||
sandbox.stub(clickEvent, "preventDefault");
|
||||
assert.ok(track(clickEvent));
|
||||
@ -154,7 +169,7 @@ QUnit.test("preventDefault is not called for right clicks", function(assert) {
|
||||
|
||||
var testOpenInANewTab = function(description, clickEventModifier) {
|
||||
test(description, function(assert) {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
var clickEvent = generateClickEventOn("a");
|
||||
clickEventModifier(clickEvent);
|
||||
sandbox.stub(clickEvent, "preventDefault");
|
||||
assert.ok(track(clickEvent));
|
||||
@ -162,19 +177,27 @@ var testOpenInANewTab = function(description, clickEventModifier) {
|
||||
});
|
||||
};
|
||||
|
||||
testOpenInANewTab("it opens in a new tab when pressing shift", function(clickEvent) {
|
||||
testOpenInANewTab("it opens in a new tab when pressing shift", function(
|
||||
clickEvent
|
||||
) {
|
||||
clickEvent.shiftKey = true;
|
||||
});
|
||||
|
||||
testOpenInANewTab("it opens in a new tab when pressing meta", function(clickEvent) {
|
||||
testOpenInANewTab("it opens in a new tab when pressing meta", function(
|
||||
clickEvent
|
||||
) {
|
||||
clickEvent.metaKey = true;
|
||||
});
|
||||
|
||||
testOpenInANewTab("it opens in a new tab when pressing ctrl", function(clickEvent) {
|
||||
testOpenInANewTab("it opens in a new tab when pressing ctrl", function(
|
||||
clickEvent
|
||||
) {
|
||||
clickEvent.ctrlKey = true;
|
||||
});
|
||||
|
||||
testOpenInANewTab("it opens in a new tab on middle click", function(clickEvent) {
|
||||
testOpenInANewTab("it opens in a new tab on middle click", function(
|
||||
clickEvent
|
||||
) {
|
||||
clickEvent.button = 2;
|
||||
});
|
||||
|
||||
@ -182,7 +205,7 @@ QUnit.test("tracks via AJAX if we're on the same site", function(assert) {
|
||||
sandbox.stub(DiscourseURL, "routeTo");
|
||||
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
||||
|
||||
assert.ok(!track(generateClickEventOn('#same-site')));
|
||||
assert.ok(!track(generateClickEventOn("#same-site")));
|
||||
assert.ok(DiscourseURL.routeTo.calledOnce);
|
||||
});
|
||||
|
||||
@ -190,19 +213,35 @@ QUnit.test("does not track via AJAX for attachments", function(assert) {
|
||||
sandbox.stub(DiscourseURL, "routeTo");
|
||||
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
||||
|
||||
assert.ok(!track(generateClickEventOn('.attachment')));
|
||||
assert.ok(!track(generateClickEventOn(".attachment")));
|
||||
assert.ok(DiscourseURL.redirectTo.calledOnce);
|
||||
});
|
||||
|
||||
QUnit.test("tracks custom urls when opening in another window", function(assert) {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
sandbox.stub(Discourse.User, "currentProp").withArgs('external_links_in_new_tab').returns(true);
|
||||
QUnit.test("tracks custom urls when opening in another window", function(
|
||||
assert
|
||||
) {
|
||||
var clickEvent = generateClickEventOn("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=42&topic_id=1337', '_blank'));
|
||||
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 when opening in another window", function(assert) {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
QUnit.test("tracks custom urls when opening in another window", function(
|
||||
assert
|
||||
) {
|
||||
var clickEvent = generateClickEventOn("a");
|
||||
assert.ok(!track(clickEvent));
|
||||
assert.ok(redirectTo.calledWith('/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42&topic_id=1337'));
|
||||
assert.ok(
|
||||
redirectTo.calledWith(
|
||||
"/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42&topic_id=1337"
|
||||
)
|
||||
);
|
||||
});
|
||||
|
Reference in New Issue
Block a user