Skip to content

Commit

Permalink
Updating login controller to use notification service and writing uni…
Browse files Browse the repository at this point in the history
…t tests.
  • Loading branch information
Jeremy Sik committed Aug 12, 2015
1 parent 753af79 commit dc5979a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
20 changes: 7 additions & 13 deletions app/src/app/guest/login/login.spec.ts
Expand Up @@ -31,7 +31,7 @@ namespace app.guest.login {
$rootScope:ng.IRootScopeService,
$timeout:ng.ITimeoutService,
$mdDialog:ng.material.IDialogService,
$mdToast:ng.material.IToastService,
notificationService:common.services.notification.NotificationService,
authService:NgJwtAuth.NgJwtAuthService,
deferredCredentials:ng.IDeferred<any>,
loginSuccess:{promise:ng.IPromise<any>}
Expand All @@ -40,16 +40,13 @@ namespace app.guest.login {
beforeEach(() => {

module('app');
});

beforeEach(()=> {

inject(($controller, _$rootScope_, _ngJwtAuthService_, _$mdDialog_, _$mdToast_, _$q_, _$timeout_) => {
inject(($controller, _$rootScope_, _ngJwtAuthService_, _$mdDialog_, _notificationService_, _$q_, _$timeout_) => {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$timeout = _$timeout_;
$mdDialog = _$mdDialog_;
$mdToast = _$mdToast_;
notificationService = _notificationService_;
authService = _ngJwtAuthService_;
$q = _$q_;
deferredCredentials = $q.defer();
Expand All @@ -63,14 +60,11 @@ namespace app.guest.login {
loginSuccess: loginSuccess,
});
})
});

beforeEach(() => {

sinon.spy($mdDialog, 'hide');
sinon.spy($mdDialog, 'cancel');
sinon.spy($mdDialog, 'show');
sinon.spy($mdToast, 'show');
sinon.spy(notificationService, 'toast');

});

Expand All @@ -79,7 +73,7 @@ namespace app.guest.login {
(<any>$mdDialog).hide.restore();
(<any>$mdDialog).cancel.restore();
(<any>$mdDialog).show.restore();
(<any>$mdToast).show.restore();
(<any>notificationService).toast.restore();

});

Expand Down Expand Up @@ -187,7 +181,7 @@ namespace app.guest.login {
progressSpy.should.have.been.calledWith(credsFail);
progressSpy.should.have.been.calledWith(credsPass);
progressSpy.should.have.been.calledTwice;
expect($mdToast.show).to.have.been.calledOnce;
expect(notificationService.toast).to.have.been.calledOnce;
});

});
Expand All @@ -206,7 +200,7 @@ namespace app.guest.login {
return loginSuccess.promise.then(function () {
progressSpy.should.have.been.calledWith(credsFail);
progressSpy.should.have.been.calledWith(credsPass);
expect($mdToast.show).to.have.been.calledTwice;
expect(notificationService.toast).to.have.been.calledTwice;
});

})
Expand Down
12 changes: 3 additions & 9 deletions app/src/app/guest/login/login.ts
Expand Up @@ -54,11 +54,11 @@ namespace app.guest.login {

public socialLogin;

static $inject = ['$rootScope', '$mdDialog', '$mdToast', 'ngJwtAuthService', 'deferredCredentials', 'loginSuccess', 'userService'];
static $inject = ['$rootScope', '$mdDialog', 'notificationService', 'ngJwtAuthService', 'deferredCredentials', 'loginSuccess', 'userService'];

constructor(private $rootScope:global.IRootScope,
private $mdDialog:ng.material.IDialogService,
private $mdToast:ng.material.IToastService,
private notificationService:common.services.notification.NotificationService,
private ngJwtAuthService:NgJwtAuth.NgJwtAuthService,
private deferredCredentials:ng.IDeferred<NgJwtAuth.ICredentials>,
private loginSuccess:{promise:ng.IPromise<NgJwtAuth.IUser>},
Expand All @@ -82,13 +82,7 @@ namespace app.guest.login {
null,
(err:Error) => {
if (err instanceof NgJwtAuth.NgJwtAuthCredentialsFailedException) {
this.$mdToast.show(
(<any>this.$mdToast).simple() //<any> added so the parent method doesn't throw error, see https://github.com/borisyankov/DefinitelyTyped/issues/4843#issuecomment-124443371
.hideDelay(2000)
.position('top')
.content(err.message)
.parent('#loginDialog')
);
this.notificationService.toast(err.message).options({parent:'#loginDialog'}).pop();
} else {
console.error(err);
}
Expand Down

0 comments on commit dc5979a

Please sign in to comment.