mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Migrate all jasmine specs to Qunit. Removed Jasmine.
This commit is contained in:
173
test/javascripts/components/click_track_test.js
Normal file
173
test/javascripts/components/click_track_test.js
Normal file
@@ -0,0 +1,173 @@
|
||||
/*global module:true test:true ok:true visit:true equal:true exists:true count:true equal:true present:true sinon:true blank:true */
|
||||
|
||||
module("Discourse.ClickTrack", {
|
||||
setup: function() {
|
||||
|
||||
// Prevent any of these tests from navigating away
|
||||
this.win = {focus: function() { } };
|
||||
this.redirectTo = sinon.stub(Discourse.URL, "redirectTo");
|
||||
sinon.stub(Discourse, "ajax");
|
||||
this.windowOpen = sinon.stub(window, "open").returns(this.win);
|
||||
sinon.stub(this.win, "focus");
|
||||
|
||||
$('#qunit-scratch').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>',
|
||||
' </article>',
|
||||
'</div>'].join("\n"));
|
||||
},
|
||||
|
||||
teardown: function() {
|
||||
$('#topic').remove();
|
||||
$('#qunit-scratch').html('');
|
||||
|
||||
Discourse.URL.redirectTo.restore();
|
||||
Discourse.ajax.restore();
|
||||
window.open.restore();
|
||||
this.win.focus.restore();
|
||||
}
|
||||
});
|
||||
|
||||
var track = Discourse.ClickTrack.trackClick;
|
||||
|
||||
var generateClickEventOn = function(selector) {
|
||||
return $.Event("click", { currentTarget: $(selector)[0] });
|
||||
}
|
||||
|
||||
test("does not track clicks on lightboxes", function() {
|
||||
var clickEvent = generateClickEventOn('.lightbox');
|
||||
this.stub(clickEvent, "preventDefault");
|
||||
ok(track(clickEvent));
|
||||
ok(!clickEvent.preventDefault.calledOnce);
|
||||
});
|
||||
|
||||
test("it calls preventDefault when clicking on an a", function() {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
this.stub(clickEvent, "preventDefault");
|
||||
track(clickEvent);
|
||||
ok(clickEvent.preventDefault.calledOnce);
|
||||
ok(Discourse.URL.redirectTo.calledOnce);
|
||||
});
|
||||
|
||||
test("does not track clicks on back buttons", function() {
|
||||
ok(track(generateClickEventOn('.back')))
|
||||
});
|
||||
|
||||
test("does not track clicks on quote buttons", function() {
|
||||
ok(track(generateClickEventOn('.quote-other-topic')))
|
||||
});
|
||||
|
||||
test("removes the href and put it as a data attribute", function() {
|
||||
track(generateClickEventOn('a'));
|
||||
|
||||
var $link = $('a').first();
|
||||
ok($link.hasClass('no-href'));
|
||||
equal($link.data('href'), 'http://www.google.com');
|
||||
blank($link.attr('href'));
|
||||
ok($link.data('auto-route'));
|
||||
ok(Discourse.URL.redirectTo.calledOnce);
|
||||
});
|
||||
|
||||
|
||||
var badgeClickCount = function(id, expected) {
|
||||
track(generateClickEventOn('#' + id));
|
||||
var $badge = $('span.badge', $('#' + id).first());
|
||||
equal(parseInt($badge.html(), 10), expected);
|
||||
};
|
||||
|
||||
test("does not update badge clicks on my own link", function() {
|
||||
this.stub(Discourse.User, 'current').returns(314);
|
||||
badgeClickCount('with-badge', 1);
|
||||
});
|
||||
|
||||
test("does not update badge clicks in my own post", function() {
|
||||
this.stub(Discourse.User, 'current').returns(3141);
|
||||
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() {
|
||||
var clickEvent = generateClickEventOn('a')
|
||||
clickEvent.which = 3;
|
||||
return track(clickEvent);
|
||||
};
|
||||
|
||||
test("right clicks change the href", function() {
|
||||
ok(trackRightClick());
|
||||
equal($('a').first().prop('href'), "http://www.google.com/");
|
||||
});
|
||||
|
||||
test("right clicks are tracked", function() {
|
||||
Discourse.SiteSettings.track_external_right_clicks = true;
|
||||
trackRightClick();
|
||||
equal($('a').first().attr('href'), "/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42");
|
||||
Discourse.SiteSettings.track_external_right_clicks = false;
|
||||
});
|
||||
|
||||
|
||||
var expectToOpenInANewTab = function(clickEvent) {
|
||||
ok(!track(clickEvent));
|
||||
ok(Discourse.ajax.calledOnce);
|
||||
ok(window.open.calledOnce);
|
||||
};
|
||||
|
||||
test("it opens in a new tab when pressing shift", function() {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
clickEvent.shiftKey = true;
|
||||
expectToOpenInANewTab(clickEvent);
|
||||
});
|
||||
|
||||
test("it opens in a new tab when pressing meta", function() {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
clickEvent.metaKey = true;
|
||||
expectToOpenInANewTab(clickEvent);
|
||||
});
|
||||
|
||||
test("it opens in a new tab when pressing meta", function() {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
clickEvent.ctrlKey = true;
|
||||
expectToOpenInANewTab(clickEvent);
|
||||
});
|
||||
|
||||
test("it opens in a new tab when pressing meta", function() {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
clickEvent.which = 2;
|
||||
expectToOpenInANewTab(clickEvent);
|
||||
});
|
||||
|
||||
test("tracks via AJAX if we're on the same site", function() {
|
||||
this.stub(Discourse.URL, "routeTo");
|
||||
this.stub(Discourse.URL, "origin").returns("http://discuss.domain.com");
|
||||
|
||||
ok(!track(generateClickEventOn('#same-site')));
|
||||
ok(Discourse.ajax.calledOnce);
|
||||
ok(Discourse.URL.routeTo.calledOnce);
|
||||
});
|
||||
|
||||
|
||||
test("tracks custom urls when opening in another window", function() {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
this.stub(Discourse.User, "current").returns(true);
|
||||
ok(!track(clickEvent));
|
||||
ok(this.windowOpen.calledWith('/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42', '_blank'));
|
||||
});
|
||||
|
||||
test("tracks custom urls when opening in another window", function() {
|
||||
var clickEvent = generateClickEventOn('a');
|
||||
ok(!track(clickEvent));
|
||||
ok(this.redirectTo.calledWith('/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42'));
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
/*global module:true test:true ok:true visit:true equal:true exists:true count:true equal:true present:true md5:true */
|
||||
/*global module:true test:true ok:true visit:true equal:true exists:true count:true equal:true present:true md5:true sanitizeHtml:true */
|
||||
|
||||
module("Discourse.Markdown");
|
||||
|
||||
@@ -93,3 +93,9 @@ test("Oneboxing", function() {
|
||||
|
||||
});
|
||||
|
||||
test("SanitizeHTML", function() {
|
||||
|
||||
equal(sanitizeHtml("<div><script>alert('hi');</script></div>"), "<div></div>");
|
||||
equal(sanitizeHtml("<div><p class=\"funky\" wrong='1'>hello</p></div>"), "<div><p class=\"funky\">hello</p></div>");
|
||||
|
||||
});
|
||||
73
test/javascripts/components/preload_store_test.js
Normal file
73
test/javascripts/components/preload_store_test.js
Normal file
@@ -0,0 +1,73 @@
|
||||
/*global module:true test:true ok:true visit:true expect:true exists:true equal:true count:true present:true asyncTest:true blank:true */
|
||||
|
||||
module("Discourse.PreloadStore", {
|
||||
setup: function() {
|
||||
PreloadStore.store('bane', 'evil');
|
||||
}
|
||||
});
|
||||
|
||||
test("get", function() {
|
||||
blank(PreloadStore.get('joker'), "returns blank for a missing key");
|
||||
equal(PreloadStore.get('bane'), 'evil', "returns the value for that key");
|
||||
});
|
||||
|
||||
test("remove", function() {
|
||||
PreloadStore.remove('bane');
|
||||
blank(PreloadStore.get('bane'), "removes the value if the key exists");
|
||||
});
|
||||
|
||||
asyncTest("getAndRemove returns a promise that resolves to null", function() {
|
||||
expect(1);
|
||||
|
||||
PreloadStore.getAndRemove('joker').then(function(result) {
|
||||
blank(result);
|
||||
start();
|
||||
});
|
||||
});
|
||||
|
||||
asyncTest("getAndRemove returns a promise that resolves to the result of the finder", function() {
|
||||
expect(1);
|
||||
|
||||
var finder = function() { return 'batdance'; };
|
||||
PreloadStore.getAndRemove('joker', finder).then(function(result) {
|
||||
equal(result, 'batdance');
|
||||
start();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
asyncTest("getAndRemove returns a promise that resolves to the result of the finder's promise", function() {
|
||||
expect(1);
|
||||
|
||||
var finder = function() {
|
||||
return Ember.Deferred.promise(function(promise) { promise.resolve('hahahah'); });
|
||||
};
|
||||
|
||||
PreloadStore.getAndRemove('joker', finder).then(function(result) {
|
||||
equal(result, 'hahahah');
|
||||
start();
|
||||
});
|
||||
});
|
||||
|
||||
asyncTest("returns a promise that rejects with the result of the finder's rejected promise", function() {
|
||||
expect(1);
|
||||
|
||||
var finder = function() {
|
||||
return Ember.Deferred.promise(function(promise) { promise.reject('error'); });
|
||||
};
|
||||
|
||||
PreloadStore.getAndRemove('joker', finder).then(null, function(result) {
|
||||
equal(result, 'error');
|
||||
start();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
asyncTest("returns a promise that resolves to 'evil'", function() {
|
||||
expect(1);
|
||||
|
||||
PreloadStore.getAndRemove('bane').then(function(result) {
|
||||
equal(result, 'evil');
|
||||
start();
|
||||
});
|
||||
});
|
||||
52
test/javascripts/components/utilities_test.js
Normal file
52
test/javascripts/components/utilities_test.js
Normal file
@@ -0,0 +1,52 @@
|
||||
/*global module:true test:true ok:true visit:true expect:true exists:true count:true equal:true */
|
||||
|
||||
module("Discourse.Utilities");
|
||||
|
||||
var utils = Discourse.Utilities;
|
||||
|
||||
test("emailValid", function() {
|
||||
ok(utils.emailValid('Bob@example.com'), "allows upper case in the first part of emails");
|
||||
ok(utils.emailValid('bob@EXAMPLE.com'), "allows upper case in the email domain");
|
||||
});
|
||||
|
||||
|
||||
var validUpload = utils.validateFilesForUpload;
|
||||
|
||||
test("validateFilesForUpload", function() {
|
||||
ok(!validUpload(null), "no files are invalid");
|
||||
ok(!validUpload(undefined), "undefined files are invalid");
|
||||
ok(!validUpload([]), "empty array of files is invalid");
|
||||
});
|
||||
|
||||
test("uploading one file", function() {
|
||||
this.stub(bootbox, "alert");
|
||||
|
||||
ok(!validUpload([1, 2]));
|
||||
ok(bootbox.alert.calledOnce);
|
||||
});
|
||||
|
||||
test("ensures an image upload", function() {
|
||||
var html = { type: "text/html" };
|
||||
this.stub(bootbox, "alert");
|
||||
|
||||
ok(!validUpload([html]));
|
||||
ok(bootbox.alert.calledOnce);
|
||||
});
|
||||
|
||||
test("prevents files that are too big from being uploaded", function() {
|
||||
var image = { type: "image/png", size: 10 * 1024 };
|
||||
Discourse.SiteSettings.max_upload_size_kb = 5;
|
||||
this.stub(bootbox, "alert");
|
||||
|
||||
ok(!validUpload([image]));
|
||||
ok(bootbox.alert.calledOnce);
|
||||
});
|
||||
|
||||
test("allows valid uploads to go through", function() {
|
||||
var image = { type: "image/png", size: 10 * 1024 };
|
||||
Discourse.SiteSettings.max_upload_size_kb = 15;
|
||||
this.stub(bootbox, "alert");
|
||||
|
||||
ok(validUpload([image]));
|
||||
ok(!bootbox.alert.calledOnce);
|
||||
});
|
||||
Reference in New Issue
Block a user