Lots of work on tests

This commit is contained in:
Robin Ward
2014-07-30 18:56:01 -04:00
parent b6684e7168
commit 6f36d5996d
43 changed files with 248 additions and 311 deletions

View File

@@ -1,12 +1,16 @@
var windowOpen,
win,
redirectTo;
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");
win = {focus: function() { } };
redirectTo = sandbox.stub(Discourse.URL, "redirectTo");
sandbox.stub(Discourse, "ajax");
windowOpen = sandbox.stub(window, "open").returns(win);
sandbox.stub(win, "focus");
fixture().html([
'<div id="topic" id="1337">',
@@ -23,13 +27,6 @@ module("Discourse.ClickTrack", {
' <a class="attachment" href="http://discuss.domain.com/uploads/default/1234/1532357280.txt">log.txt</a>',
' </article>',
'</div>'].join("\n"));
},
teardown: function() {
Discourse.URL.redirectTo.restore();
Discourse.ajax.restore();
window.open.restore();
this.win.focus.restore();
}
});
@@ -42,14 +39,14 @@ var generateClickEventOn = function(selector) {
test("does not track clicks on lightboxes", function() {
var clickEvent = generateClickEventOn('.lightbox');
this.stub(clickEvent, "preventDefault");
sandbox.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");
sandbox.stub(clickEvent, "preventDefault");
track(clickEvent);
ok(clickEvent.preventDefault.calledOnce);
ok(Discourse.URL.redirectTo.calledOnce);
@@ -82,12 +79,12 @@ var badgeClickCount = function(id, expected) {
};
test("does not update badge clicks on my own link", function() {
this.stub(Discourse.User, 'currentProp').withArgs('id').returns(314);
sandbox.stub(Discourse.User, 'currentProp').withArgs('id').returns(314);
badgeClickCount('with-badge', 1);
});
test("does not update badge clicks in my own post", function() {
this.stub(Discourse.User, 'currentProp').withArgs('id').returns(3141);
sandbox.stub(Discourse.User, 'currentProp').withArgs('id').returns(3141);
badgeClickCount('with-badge-but-not-mine', 1);
});
@@ -117,7 +114,7 @@ test("right clicks are tracked", function() {
test("preventDefault is not called for right clicks", function() {
var clickEvent = generateClickEventOn('a');
clickEvent.which = 3;
this.stub(clickEvent, "preventDefault");
sandbox.stub(clickEvent, "preventDefault");
ok(track(clickEvent));
ok(!clickEvent.preventDefault.calledOnce);
});
@@ -126,7 +123,7 @@ var testOpenInANewTab = function(description, clickEventModifier) {
test(description, function() {
var clickEvent = generateClickEventOn('a');
clickEventModifier(clickEvent);
this.stub(clickEvent, "preventDefault");
sandbox.stub(clickEvent, "preventDefault");
ok(track(clickEvent));
ok(Discourse.ajax.calledOnce);
ok(!clickEvent.preventDefault.calledOnce);
@@ -150,8 +147,8 @@ testOpenInANewTab("it opens in a new tab on middle click", function(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");
sandbox.stub(Discourse.URL, "routeTo");
sandbox.stub(Discourse.URL, "origin").returns("http://discuss.domain.com");
ok(!track(generateClickEventOn('#same-site')));
ok(Discourse.ajax.calledOnce);
@@ -159,8 +156,8 @@ test("tracks via AJAX if we're on the same site", function() {
});
test("does not track via AJAX for attachments", function() {
this.stub(Discourse.URL, "routeTo");
this.stub(Discourse.URL, "origin").returns("http://discuss.domain.com");
sandbox.stub(Discourse.URL, "routeTo");
sandbox.stub(Discourse.URL, "origin").returns("http://discuss.domain.com");
ok(!track(generateClickEventOn('.attachment')));
ok(Discourse.URL.redirectTo.calledOnce);
@@ -168,13 +165,13 @@ test("does not track via AJAX for attachments", function() {
test("tracks custom urls when opening in another window", function() {
var clickEvent = generateClickEventOn('a');
this.stub(Discourse.User, "currentProp").withArgs('external_links_in_new_tab').returns(true);
sandbox.stub(Discourse.User, "currentProp").withArgs('external_links_in_new_tab').returns(true);
ok(!track(clickEvent));
ok(this.windowOpen.calledWith('/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42', '_blank'));
ok(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'));
ok(redirectTo.calledWith('/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42'));
});

View File

@@ -1,6 +1,6 @@
module("Discourse.Computed", {
setup: function() {
sinon.stub(I18n, "t", function(scope) {
sandbox.stub(I18n, "t", function(scope) {
return "%@ translated: " + scope;
});
},

View File

@@ -35,7 +35,7 @@ test("undefined color", function() {
test("allowUncategorized", function() {
var uncategorized = Discourse.Category.create({name: 'uncategorized', id: 345});
this.stub(Discourse.Site, 'currentProp').withArgs('uncategorized_category_id').returns(345);
sandbox.stub(Discourse.Site, 'currentProp').withArgs('uncategorized_category_id').returns(345);
blank(html.categoryBadge(uncategorized), "it doesn't return HTML for uncategorized by default");
present(html.categoryBadge(uncategorized, {allowUncategorized: true}), "it returns HTML");

View File

@@ -5,7 +5,7 @@ module("Discourse.Onebox", {
});
asyncTestDiscourse("Stops rapid calls with cache true", function() {
this.stub(Discourse, "ajax").returns(Ember.RSVP.resolve());
sandbox.stub(Discourse, "ajax").returns(Ember.RSVP.resolve());
Discourse.Onebox.load(this.anchor, true);
Discourse.Onebox.load(this.anchor, true);
@@ -14,7 +14,7 @@ asyncTestDiscourse("Stops rapid calls with cache true", function() {
});
asyncTestDiscourse("Stops rapid calls with cache true", function() {
this.stub(Discourse, "ajax").returns(Ember.RSVP.resolve());
sandbox.stub(Discourse, "ajax").returns(Ember.RSVP.resolve());
Discourse.Onebox.load(this.anchor, false);
Discourse.Onebox.load(this.anchor, false);

View File

@@ -1,7 +1,7 @@
module("Discourse.URL");
test("isInternal with a HTTP url", function() {
this.stub(Discourse.URL, "origin").returns("http://eviltrout.com");
sandbox.stub(Discourse.URL, "origin").returns("http://eviltrout.com");
not(Discourse.URL.isInternal(null), "a blank URL is not internal");
ok(Discourse.URL.isInternal("/test"), "relative URLs are internal");
@@ -11,7 +11,7 @@ test("isInternal with a HTTP url", function() {
});
test("isInternal with a HTTPS url", function() {
this.stub(Discourse.URL, "origin").returns("https://eviltrout.com");
sandbox.stub(Discourse.URL, "origin").returns("https://eviltrout.com");
ok(Discourse.URL.isInternal("http://eviltrout.com/monocle"), "HTTPS urls match HTTP urls");
});
@@ -20,7 +20,7 @@ test("isInternal with a HTTPS url", function() {
// --------------------------------------------
// test("routeTo", function() {
// this.stub(Discourse.URL, "handleURL", function (path) { return path === "/t/topic-title/42"; });
// sandbox.stub(Discourse.URL, "handleURL", function (path) { return path === "/t/topic-title/42"; });
// ok(Discourse.URL.routeTo("https://discourse.org/t/topic-title/42"), "can route HTTPS");
// ok(Discourse.URL.routeTo("http://discourse.org/t/topic-title/42"), "can route HTTP");
@@ -33,7 +33,7 @@ test("isInternal with a HTTPS url", function() {
// test("navigatedToHome", function() {
// var fakeDiscoveryController = { send: function() { return true; } };
// var mock = sinon.mock(fakeDiscoveryController);
// this.stub(Discourse.URL, "controllerFor").returns(fakeDiscoveryController);
// sandbox.stub(Discourse.URL, "controllerFor").returns(fakeDiscoveryController);
//
// mock.expects("send").withArgs('refresh').twice();
// ok(Discourse.URL.navigatedToHome("/", "/"));

View File

@@ -16,7 +16,7 @@ test("validateUploadedFiles", function() {
});
test("uploading one file", function() {
this.stub(bootbox, "alert");
sandbox.stub(bootbox, "alert");
not(validUpload([1, 2]));
ok(bootbox.alert.calledWith(I18n.t('post.errors.too_many_uploads')));
@@ -24,7 +24,7 @@ test("uploading one file", function() {
test("new user cannot upload images", function() {
Discourse.SiteSettings.newuser_max_images = 0;
this.stub(bootbox, "alert");
sandbox.stub(bootbox, "alert");
not(validUpload([{name: "image.png"}]), 'the upload is not valid');
ok(bootbox.alert.calledWith(I18n.t('post.errors.image_upload_not_allowed_for_new_user')), 'the alert is called');
@@ -32,7 +32,7 @@ test("new user cannot upload images", function() {
test("new user cannot upload attachments", function() {
Discourse.SiteSettings.newuser_max_attachments = 0;
this.stub(bootbox, "alert");
sandbox.stub(bootbox, "alert");
not(validUpload([{name: "roman.txt"}]));
ok(bootbox.alert.calledWith(I18n.t('post.errors.attachment_upload_not_allowed_for_new_user')));
@@ -41,7 +41,7 @@ test("new user cannot upload attachments", function() {
test("ensures an authorized upload", function() {
var html = { name: "unauthorized.html" };
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
this.stub(bootbox, "alert");
sandbox.stub(bootbox, "alert");
not(validUpload([html]));
ok(bootbox.alert.calledWith(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions })));
@@ -51,7 +51,7 @@ test("prevents files that are too big from being uploaded", function() {
var image = { name: "image.png", size: 10 * 1024 };
Discourse.SiteSettings.max_image_size_kb = 5;
Discourse.User.currentProp("trust_level", 1);
this.stub(bootbox, "alert");
sandbox.stub(bootbox, "alert");
not(validUpload([image]));
ok(bootbox.alert.calledWith(I18n.t('post.errors.image_too_large', { max_size_kb: 5 })));
@@ -71,7 +71,7 @@ var dummyBlob = function() {
test("allows valid uploads to go through", function() {
Discourse.User.currentProp("trust_level", 1);
Discourse.SiteSettings.max_image_size_kb = 15;
this.stub(bootbox, "alert");
sandbox.stub(bootbox, "alert");
// image
var image = { name: "image.png", size: 10 * 1024 };