Replace CurrentUserMixin with an injected currentUser

This is a lot simpler and removes the need for stubbing singletons
in unit tests.
This commit is contained in:
Robin Ward 2015-02-26 12:25:25 -05:00
parent 1f716f5514
commit be9feeb918
17 changed files with 32 additions and 77 deletions

View file

@ -1 +1 @@
export default Ember.Controller.extend(Discourse.Presence, Discourse.HasCurrentUser); export default Ember.Controller.extend(Discourse.Presence);

View file

@ -1,4 +1,4 @@
export default Ember.ArrayController.extend(Discourse.HasCurrentUser, { export default Ember.ArrayController.extend({
needs: ['header'], needs: ['header'],
loadingNotifications: Em.computed.alias('controllers.header.loadingNotifications') loadingNotifications: Em.computed.alias('controllers.header.loadingNotifications')
}); });

View file

@ -1 +1 @@
export default Ember.ObjectController.extend(Discourse.Presence, Discourse.HasCurrentUser); export default Ember.ObjectController.extend(Discourse.Presence);

View file

@ -1,4 +1,4 @@
export default Ember.ObjectController.extend(Discourse.HasCurrentUser, { export default Ember.ObjectController.extend({
needs: ['site-map'], needs: ['site-map'],
unreadTotal: function() { unreadTotal: function() {

View file

@ -1,4 +1,4 @@
export default Ember.ArrayController.extend(Discourse.HasCurrentUser, { export default Ember.ArrayController.extend({
needs: ['application'], needs: ['application'],
showBadgesLink: function(){return Discourse.SiteSettings.enable_badges;}.property(), showBadgesLink: function(){return Discourse.SiteSettings.enable_badges;}.property(),

View file

@ -1,8 +1,8 @@
export default Ember.ArrayController.extend(Discourse.HasCurrentUser, { export default Ember.ArrayController.extend({
showAdminLinks: Em.computed.alias("currentUser.staff"), showAdminLinks: Em.computed.alias("currentUser.staff"),
actions: { actions: {
logout: function() { logout() {
Discourse.logout(); Discourse.logout();
return false; return false;
} }

View file

@ -1,7 +1,7 @@
import ObjectController from 'discourse/controllers/object'; import ObjectController from 'discourse/controllers/object';
// Lists of topics on a user's page. // Lists of topics on a user's page.
export default ObjectController.extend(Discourse.HasCurrentUser, { export default ObjectController.extend({
needs: ["application", "user"], needs: ["application", "user"],
hideCategory: false, hideCategory: false,
showParticipants: false, showParticipants: false,

View file

@ -42,9 +42,9 @@ export default {
app.inject('view', 'session', 'session:main'); app.inject('view', 'session', 'session:main');
app.inject('model', 'session', 'session:main'); app.inject('model', 'session', 'session:main');
// Inject currentUser. Components only for now to prevent any breakage
app.register('current-user:main', Discourse.User.current(), { instantiate: false }); app.register('current-user:main', Discourse.User.current(), { instantiate: false });
app.inject('component', 'currentUser', 'current-user:main'); app.inject('component', 'currentUser', 'current-user:main');
app.inject('controller', 'currentUser', 'current-user:main');
app.register('store:main', Store); app.register('store:main', Store);
app.inject('route', 'store', 'store:main'); app.inject('route', 'store', 'store:main');

View file

@ -1,12 +0,0 @@
/**
This mixin provides a `currentUser` property that can be used to retrieve information
about the currently logged in user. It is mostly useful to controllers so it can be
exposted to templates.
**/
Discourse.HasCurrentUser = Em.Mixin.create({
currentUser: function() {
return Discourse.User.current();
}.property().volatile()
});

View file

@ -28,8 +28,7 @@ export default function(filter, params) {
category: model, category: model,
filterMode: filterMode, filterMode: filterMode,
noSubcategories: params && params.no_subcategories, noSubcategories: params && params.no_subcategories,
canEditCategory: model.get('can_edit'), canEditCategory: model.get('can_edit')
canChangeCategoryNotificationLevel: Discourse.User.current()
}); });
}, },

View file

@ -10,7 +10,7 @@
{{custom-html "extraNavItem"}} {{custom-html "extraNavItem"}}
</ul> </ul>
{{#if canChangeCategoryNotificationLevel}} {{#if currentUser}}
{{category-notifications-button category=category}} {{category-notifications-button category=category}}
{{/if}} {{/if}}

View file

@ -4,5 +4,4 @@ module("DiscourseController");
test("includes mixins", function() { test("includes mixins", function() {
ok(Discourse.Presence.detect(DiscourseController.create()), "Discourse.Presence"); ok(Discourse.Presence.detect(DiscourseController.create()), "Discourse.Presence");
ok(Discourse.HasCurrentUser.detect(DiscourseController.create()), "Discourse.HasCurrentUser");
}); });

View file

@ -3,26 +3,23 @@ moduleFor("controller:header", "controller:header", {
}); });
test("showNotifications action", function() { test("showNotifications action", function() {
var resolveRequestWith; let resolveRequestWith;
var request = new Ember.RSVP.Promise(function(resolve) { const request = new Ember.RSVP.Promise(function(resolve) {
resolveRequestWith = resolve; resolveRequestWith = resolve;
}); });
var controller = this.subject(); const currentUser = Discourse.User.create({ unread_notifications: 1});
var viewSpy = { const controller = this.subject({ currentUser: currentUser });
showDropdownBySelector: sinon.spy() const viewSpy = { showDropdownBySelector: sinon.spy() };
};
sandbox.stub(Discourse, "ajax").withArgs("/notifications").returns(request); sandbox.stub(Discourse, "ajax").withArgs("/notifications").returns(request);
sandbox.stub(Discourse.User, "current").returns(Discourse.User.create({
unread_notifications: 1
}));
Ember.run(function() { Ember.run(function() {
controller.send("showNotifications", viewSpy); controller.send("showNotifications", viewSpy);
}); });
equal(controller.get("notifications"), null, "notifications are null before data has finished loading"); equal(controller.get("notifications"), null, "notifications are null before data has finished loading");
equal(Discourse.User.current().get("unread_notifications"), 1, "current user's unread notifications count is not zeroed before data has finished loading"); equal(currentUser.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"); ok(viewSpy.showDropdownBySelector.calledWith("#user-notifications"), "dropdown with loading glyph is shown before data has finished loading");
Ember.run(function() { Ember.run(function() {
@ -31,6 +28,6 @@ test("showNotifications action", function() {
// Can't use deepEquals because controller.get("notifications") is an ArrayProxy, not an Array // Can't use deepEquals because controller.get("notifications") is an ArrayProxy, not an Array
ok(controller.get("notifications").indexOf("notification") !== -1, "notification is in the controller"); ok(controller.get("notifications").indexOf("notification") !== -1, "notification is in the controller");
equal(Discourse.User.current().get("unread_notifications"), 0, "current user's unread notifications count is zeroed after data has finished loading"); equal(currentUser.get("unread_notifications"), 0, "current user's unread notifications count is zeroed after data has finished loading");
ok(viewSpy.showDropdownBySelector.calledWith("#user-notifications"), "dropdown with notifications is shown after data has finished loading"); ok(viewSpy.showDropdownBySelector.calledWith("#user-notifications"), "dropdown with notifications is shown after data has finished loading");
}); });

View file

@ -1,7 +0,0 @@
moduleFor('controller:notifications', 'controller:notifications', {
needs: ['controller:header']
});
test("mixes in HasCurrentUser", function() {
ok(Discourse.HasCurrentUser.detect(this.subject()));
});

View file

@ -13,26 +13,21 @@ moduleFor("controller:site-map", "controller:site-map", {
}); });
test("showAdminLinks", function() { test("showAdminLinks", function() {
var currentUserStub = Ember.Object.create(); const currentUser = Ember.Object.create({ staff: true });
sandbox.stub(Discourse.User, "current").returns(currentUserStub); const controller = this.subject({ currentUser });
currentUserStub.set("staff", true);
var controller = this.subject();
equal(controller.get("showAdminLinks"), true, "is true when current user is a staff member"); equal(controller.get("showAdminLinks"), true, "is true when current user is a staff member");
currentUserStub.set("staff", false); currentUser.set("staff", false);
equal(controller.get("showAdminLinks"), false, "is false when current user is not a staff member"); equal(controller.get("showAdminLinks"), false, "is false when current user is not a staff member");
}); });
test("flaggedPostsCount", function() { test("flaggedPostsCount", function() {
var currentUserStub = Ember.Object.create(); const currentUser = Ember.Object.create({ site_flagged_posts_count: 5 });
sandbox.stub(Discourse.User, "current").returns(currentUserStub); const controller = this.subject({ currentUser });
currentUserStub.set("site_flagged_posts_count", 5);
var controller = this.subject();
equal(controller.get("flaggedPostsCount"), 5, "returns current user's flagged posts count"); equal(controller.get("flaggedPostsCount"), 5, "returns current user's flagged posts count");
currentUserStub.set("site_flagged_posts_count", 0); currentUser.set("site_flagged_posts_count", 0);
equal(controller.get("flaggedPostsCount"), 0, "is bound (reacts to change of current user's flagged posts count)"); equal(controller.get("flaggedPostsCount"), 0, "is bound (reacts to change of current user's flagged posts count)");
}); });

View file

@ -1,23 +1,19 @@
moduleFor("controller:user-dropdown"); moduleFor("controller:user-dropdown");
test("logout action logs out the current user", function () { test("logout action logs out the current user", function () {
var logout_mock = sinon.mock(Discourse, "logout"); const logoutMock = sinon.mock(Discourse, "logout");
logout_mock.expects("logout").once(); logoutMock.expects("logout").once();
var controller = this.subject(); this.subject().send('logout');
controller.send("logout");
logout_mock.verify(); logoutMock.verify();
}); });
test("showAdminLinks", function() { test("showAdminLinks", function() {
var currentUserStub = Ember.Object.create(); const currentUser = Ember.Object.create({ staff: true });
sandbox.stub(Discourse.User, "current").returns(currentUserStub); const controller = this.subject({ currentUser });
currentUserStub.set("staff", true);
var controller = this.subject();
equal(controller.get("showAdminLinks"), true, "is true when current user is a staff member"); equal(controller.get("showAdminLinks"), true, "is true when current user is a staff member");
currentUserStub.set("staff", false); currentUser.set("staff", false);
equal(controller.get("showAdminLinks"), false, "is false when current user is not a staff member"); equal(controller.get("showAdminLinks"), false, "is false when current user is not a staff member");
}); });

View file

@ -1,12 +0,0 @@
module("Discourse.HasCurrentUser");
test("adds `currentUser` property to an object and ensures it is not cached", function() {
sandbox.stub(Discourse.User, "current");
var testObj = Ember.Object.createWithMixins(Discourse.HasCurrentUser, {});
Discourse.User.current.returns("first user");
equal(testObj.get("currentUser"), "first user", "on the first call property returns initial user");
Discourse.User.current.returns("second user");
equal(testObj.get("currentUser"), "second user", "if the user changes, on the second call property returns changed user");
});