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
@@ -13,43 +13,44 @@ var buildAdminUser = function(args) {
}, args || {}));
};
moduleFor("controller:flag");
moduleFor("controller:flag", "controller:flag", {
needs: ['controller:modal']
});
test("canDeleteSpammer not staff", function(){
var flagController = controllerFor('flag', buildPost());
this.stub(Discourse.User, 'currentProp').withArgs('staff').returns(false);
var flagController = this.subject({ model: buildPost() });
sandbox.stub(Discourse.User, 'currentProp').withArgs('staff').returns(false);
flagController.set('selected', Discourse.PostActionType.create({name_key: 'spam'}));
equal(flagController.get('canDeleteSpammer'), false, 'false if current user is not staff');
});
var canDeleteSpammer = function(test, postActionType, expected, testName) {
test.flagController.set('selected', Discourse.PostActionType.create({name_key: postActionType}));
equal(test.flagController.get('canDeleteSpammer'), expected, testName);
var canDeleteSpammer = function(flagController, postActionType, expected, testName) {
flagController.set('selected', Discourse.PostActionType.create({name_key: postActionType}));
equal(flagController.get('canDeleteSpammer'), expected, testName);
};
test("canDeleteSpammer spam not selected", function(){
this.stub(Discourse.User, 'currentProp').withArgs('staff').returns(true);
this.flagController = controllerFor('flag', buildPost());
this.flagController.set('userDetails', buildAdminUser({can_delete_all_posts: true, can_be_deleted: true}));
canDeleteSpammer(this, 'off_topic', false, 'false if current user is staff, but selected is off_topic');
canDeleteSpammer(this, 'inappropriate', false, 'false if current user is staff, but selected is inappropriate');
canDeleteSpammer(this, 'notify_user', false, 'false if current user is staff, but selected is notify_user');
canDeleteSpammer(this, 'notify_moderators', false, 'false if current user is staff, but selected is notify_moderators');
sandbox.stub(Discourse.User, 'currentProp').withArgs('staff').returns(true);
var flagController = this.subject({ model: buildPost() });
flagController.set('userDetails', buildAdminUser({can_delete_all_posts: true, can_be_deleted: true}));
canDeleteSpammer(flagController, 'off_topic', false, 'false if current user is staff, but selected is off_topic');
canDeleteSpammer(flagController, 'inappropriate', false, 'false if current user is staff, but selected is inappropriate');
canDeleteSpammer(flagController, 'notify_user', false, 'false if current user is staff, but selected is notify_user');
canDeleteSpammer(flagController, 'notify_moderators', false, 'false if current user is staff, but selected is notify_moderators');
});
test("canDeleteSpammer spam selected", function(){
this.stub(Discourse.User, 'currentProp').withArgs('staff').returns(true);
this.flagController = controllerFor('flag', buildPost());
sandbox.stub(Discourse.User, 'currentProp').withArgs('staff').returns(true);
var flagController = this.subject({ model: buildPost() });
flagController.set('userDetails', buildAdminUser({can_delete_all_posts: true, can_be_deleted: true}));
canDeleteSpammer(flagController, 'spam', true, 'true if current user is staff, selected is spam, posts and user can be deleted');
this.flagController.set('userDetails', buildAdminUser({can_delete_all_posts: true, can_be_deleted: true}));
canDeleteSpammer(this, 'spam', true, 'true if current user is staff, selected is spam, posts and user can be deleted');
flagController.set('userDetails', buildAdminUser({can_delete_all_posts: false, can_be_deleted: true}));
canDeleteSpammer(flagController, 'spam', false, 'false if current user is staff, selected is spam, posts cannot be deleted');
this.flagController.set('userDetails', buildAdminUser({can_delete_all_posts: false, can_be_deleted: true}));
canDeleteSpammer(this, 'spam', false, 'false if current user is staff, selected is spam, posts cannot be deleted');
flagController.set('userDetails', buildAdminUser({can_delete_all_posts: true, can_be_deleted: false}));
canDeleteSpammer(flagController, 'spam', false, 'false if current user is staff, selected is spam, user cannot be deleted');
this.flagController.set('userDetails', buildAdminUser({can_delete_all_posts: true, can_be_deleted: false}));
canDeleteSpammer(this, 'spam', false, 'false if current user is staff, selected is spam, user cannot be deleted');
this.flagController.set('userDetails', buildAdminUser({can_delete_all_posts: false, can_be_deleted: false}));
canDeleteSpammer(this, 'spam', false, 'false if current user is staff, selected is spam, user cannot be deleted');
flagController.set('userDetails', buildAdminUser({can_delete_all_posts: false, can_be_deleted: false}));
canDeleteSpammer(flagController, 'spam', false, 'false if current user is staff, selected is spam, user cannot be deleted');
});
@@ -1,4 +1,4 @@
module("controller:header", "Header Controller");
moduleFor("controller:header", "controller:header");
test("showNotifications action", function() {
var resolveRequestWith;
@@ -6,17 +6,15 @@ test("showNotifications action", function() {
resolveRequestWith = resolve;
});
var controller = controllerFor('header');
var controller = this.subject();
var viewSpy = {
showDropdownBySelector: sinon.spy()
};
this.stub(Discourse, "ajax").withArgs("/notifications").returns(request);
this.stub(Discourse.User, "current").returns(Discourse.User.create({
sandbox.stub(Discourse, "ajax").withArgs("/notifications").returns(request);
sandbox.stub(Discourse.User, "current").returns(Discourse.User.create({
unread_notifications: 1
}));
Ember.run(function() {
controller.send("showNotifications", viewSpy);
});
@@ -25,7 +23,6 @@ test("showNotifications action", function() {
equal(Discourse.User.current().get("unread_notifications"), 1, "current user's unread notifications count is not zeroed before data has finished loading");
ok(viewSpy.showDropdownBySelector.calledWith("#user-notifications"), "dropdown with loading glyph is shown before data has finished loading");
Ember.run(function() {
resolveRequestWith(["notification"]);
});
@@ -1,25 +1,16 @@
var controller, searcherStub;
var searcherStub;
module("controller:search", {
moduleFor("controller:search", "controller:search", {
setup: function() {
Discourse.SiteSettings.min_search_term_length = 2;
// cancels debouncing behavior (calls the debounced function immediately)
sinon.stub(Ember.run, "debounce").callsArg(1);
searcherStub = Ember.Deferred.create();
sinon.stub(Discourse.Search, "forTerm").returns(searcherStub);
controller = testController('search', []);
},
teardown: function() {
Ember.run.debounce.restore();
Discourse.Search.forTerm.restore();
sandbox.stub(Discourse.Search, "forTerm").returns(searcherStub);
}
});
test("when no search term is typed yet", function() {
var controller = this.subject();
ok(!controller.get("loading"), "loading flag is false");
ok(!controller.get("noResults"), "noResults flag is false");
deepEqual(controller.get("content"), [], "content is empty");
@@ -28,9 +19,8 @@ test("when no search term is typed yet", function() {
});
test("when user started typing a search term but did not reach the minimum character count threshold yet", function() {
Ember.run(function() {
controller.set("term", "a");
});
var controller = this.subject();
controller.set("term", "a");
ok(!controller.get("loading"), "loading flag is false");
ok(!controller.get("noResults"), "noResults flag is false");
@@ -40,10 +30,8 @@ test("when user started typing a search term but did not reach the minimum chara
});
test("when user typed a search term that is equal to or exceeds the minimum character count threshold, but results have not yet finished loading", function() {
Ember.run(function() {
controller.set("term", "ab");
});
var controller = this.subject();
controller.set("term", "ab");
ok(controller.get("loading"), "loading flag is true");
ok(!controller.get("noResults"), "noResults flag is false");
deepEqual(controller.get("content"), [], "content is empty");
@@ -52,9 +40,10 @@ test("when user typed a search term that is equal to or exceeds the minimum char
});
test("when user typed a search term that is equal to or exceeds the minimum character count threshold and results have finished loading, but there are no results found", function() {
Ember.run(function() {
controller.set("term", "ab");
var controller = this.subject();
Em.run(function() {
searcherStub.resolve([]);
controller.set("term", "ab");
});
ok(!controller.get("loading"), "loading flag is false");
@@ -65,7 +54,8 @@ test("when user typed a search term that is equal to or exceeds the minimum char
});
test("when user typed a search term that is equal to or exceeds the minimum character count threshold and results have finished loading, and there are results found", function() {
Ember.run(function() {
var controller = this.subject();
Em.run(function() {
controller.set("term", "ab");
searcherStub.resolve([{
type: "user",
@@ -84,7 +74,8 @@ test("when user typed a search term that is equal to or exceeds the minimum char
});
test("starting to type a new term resets the previous search results", function() {
Ember.run(function() {
var controller = this.subject();
Em.run.next(function() {
controller.set("term", "ab");
searcherStub.resolve([
{
@@ -106,7 +97,8 @@ test("starting to type a new term resets the previous search results", function(
});
test("search results from the server are correctly reformatted (sections are sorted, section fields are preserved, item sorting is preserved, item fields are preserved, items are globally indexed across all sections)", function() {
Ember.run(function() {
var controller = this.subject();
Em.run(function() {
controller.set("term", "ab");
searcherStub.resolve([
{
@@ -165,7 +157,8 @@ test("search results from the server are correctly reformatted (sections are sor
});
test("keyboard navigation", function() {
Ember.run(function() {
var controller = this.subject();
Em.run(function() {
controller.set("term", "ab");
searcherStub.resolve([
{
@@ -197,8 +190,9 @@ test("keyboard navigation", function() {
});
test("selecting a highlighted item", function() {
this.stub(Discourse.URL, "routeTo");
sandbox.stub(Discourse.URL, "routeTo");
var controller = this.subject();
Ember.run(function() {
controller.set("term", "ab");
searcherStub.resolve([
@@ -233,6 +227,7 @@ test("selecting a highlighted item", function() {
});
test("search query / the flow of the search", function() {
var controller = this.subject();
Ember.run(function() {
controller.set("searchContext", "context");
controller.set("searchContextEnabled", true);
@@ -279,6 +274,7 @@ test("search query / the flow of the search", function() {
});
test("typing new term when the results are filtered by type cancels type filter", function() {
var controller = this.subject();
Ember.run(function() {
controller.set("term", "ab");
controller.send("moreOfType", "topic");
@@ -1,13 +1,8 @@
var controller;
module("controller:site-map-category", {
setup: function() {
controller = testController('site-map-category');
}
});
moduleFor("controller:site-map-category");
test("showBadges", function() {
this.stub(Discourse.User, "current");
sandbox.stub(Discourse.User, "current");
var controller = this.subject();
Discourse.User.current.returns(null);
ok(!controller.get("showBadges"), "returns false when no user is logged in");
@@ -1,10 +1,8 @@
var controller, oldMobileView;
var oldMobileView;
module("controller:site-map", {
moduleFor("controller:site-map", "controller:site-map", {
setup: function() {
oldMobileView = Discourse.Mobile.mobileView;
controller = testController('site-map');
},
teardown: function() {
@@ -13,14 +11,15 @@ module("controller:site-map", {
});
test("itemController", function() {
equal(controller.get("itemController"), "site-map-category", "defaults to site-map-category");
equal(this.subject().get("itemController"), "site-map-category", "defaults to site-map-category");
});
test("showAdminLinks", function() {
var currentUserStub = Ember.Object.create();
this.stub(Discourse.User, "current").returns(currentUserStub);
sandbox.stub(Discourse.User, "current").returns(currentUserStub);
currentUserStub.set("staff", true);
var controller = this.subject();
equal(controller.get("showAdminLinks"), true, "is true when current user is a staff member");
currentUserStub.set("staff", false);
@@ -29,9 +28,10 @@ test("showAdminLinks", function() {
test("flaggedPostsCount", function() {
var currentUserStub = Ember.Object.create();
this.stub(Discourse.User, "current").returns(currentUserStub);
sandbox.stub(Discourse.User, "current").returns(currentUserStub);
currentUserStub.set("site_flagged_posts_count", 5);
var controller = this.subject();
equal(controller.get("flaggedPostsCount"), 5, "returns current user's flagged posts count");
currentUserStub.set("site_flagged_posts_count", 0);
@@ -40,46 +40,54 @@ test("flaggedPostsCount", function() {
test("faqUrl returns faq url configured in site settings if it is set", function() {
Discourse.SiteSettings.faq_url = "faq-url";
var controller = this.subject();
equal(controller.get("faqUrl"), "faq-url");
});
test("faqUrl returns default '/faq' url when there is no corresponding site setting set", function() {
Discourse.SiteSettings.faq_url = null;
var controller = this.subject();
equal(controller.get("faqUrl"), "/faq");
});
test("showMoblieToggle returns true when mobile theme is enabled in site settings", function() {
Discourse.SiteSettings.enable_mobile_theme = true;
Discourse.Mobile.isMobileDevice = true;
var controller = this.subject();
equal(controller.get("showMobileToggle"), true);
});
test("showMoblieToggle returns false when mobile theme is disabled in site settings", function() {
Discourse.SiteSettings.enable_mobile_theme = false;
Discourse.Mobile.isMobileDevice = true;
var controller = this.subject();
equal(controller.get("showMobileToggle"), false);
});
test("mobileViewLinkTextKey returns translation key for a desktop view if the current view is mobile view", function() {
Discourse.Mobile.mobileView = true;
var controller = this.subject();
equal(controller.get("mobileViewLinkTextKey"), "desktop_view");
});
test("mobileViewLinkTextKey returns translation key for a mobile view if the current view is desktop view", function() {
Discourse.Mobile.mobileView = false;
var controller = this.subject();
equal(controller.get("mobileViewLinkTextKey"), "mobile_view");
});
test("categories", function() {
var categoryListStub = ["category1", "category2"];
this.stub(Discourse.Category, "list").returns(categoryListStub);
sandbox.stub(Discourse.Category, "list").returns(categoryListStub);
var controller = this.subject();
deepEqual(controller.get("categories"), categoryListStub, "returns the list of categories");
});
test("toggleMobleView", function() {
this.stub(Discourse.Mobile, "toggleMobileView");
sandbox.stub(Discourse.Mobile, "toggleMobileView");
var controller = this.subject();
controller.send("toggleMobileView");
ok(Discourse.Mobile.toggleMobileView.calledOnce, "switches between desktop and mobile views");
});
@@ -1,16 +1,10 @@
var controller;
module("controller:user-dropdown", {
setup: function() {
controller = testController('user-dropdown');
}
});
moduleFor("controller:user-dropdown");
test("logout action logs out the current user", function () {
var logout_mock = sinon.mock(Discourse, "logout");
logout_mock.expects("logout").once();
var controller = controllerFor('user-dropdown');
var controller = this.subject();
controller.send("logout");
logout_mock.verify();
@@ -18,9 +12,10 @@ test("logout action logs out the current user", function () {
test("showAdminLinks", function() {
var currentUserStub = Ember.Object.create();
this.stub(Discourse.User, "current").returns(currentUserStub);
sandbox.stub(Discourse.User, "current").returns(currentUserStub);
currentUserStub.set("staff", true);
var controller = this.subject();
equal(controller.get("showAdminLinks"), true, "is true when current user is a staff member");
currentUserStub.set("staff", false);