-
Notifications
You must be signed in to change notification settings - Fork 3
/
login.spec.ts
178 lines (105 loc) · 4.59 KB
/
login.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
describe('Login', () => {
let $q:ng.IQService,
fixtures = {
failLogin: false,
getLoginSuccessPromise(fail){
return {
promise: !fail ? $q.when('success') : $q.reject(new NgJwtAuth.NgJwtAuthException('error')),
};
}
};
describe('Configuration', () => {
let LoginController:ng.IControllerService,
$scope:ng.IScope,
$rootScope:ng.IRootScopeService,
$timeout:ng.ITimeoutService,
$mdDialog:ng.material.IDialogService,
authService:NgJwtAuth.NgJwtAuthService,
deferredCredentials:ng.IDeferred<any>,
loginSuccess:{promise:ng.IPromise<any>}
;
beforeEach(() => {
module('app');
});
beforeEach(()=> {
inject(($controller, _$rootScope_, _ngJwtAuthService_, _$mdDialog_, _$q_, _$timeout_) => {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$timeout = _$timeout_;
$mdDialog = _$mdDialog_;
authService = _ngJwtAuthService_;
deferredCredentials = _$q_.defer();
$q = _$q_;
loginSuccess = fixtures.getLoginSuccessPromise(fixtures.failLogin);
LoginController = $controller(app.guest.login.namespace+'.controller', {
$scope: $scope,
$mdDialog: $mdDialog,
deferredCredentials:deferredCredentials,
loginSuccess: loginSuccess,
});
})
});
beforeEach(() => {
sinon.spy($mdDialog, 'hide');
sinon.spy($mdDialog, 'cancel');
sinon.spy($mdDialog, 'show');
});
afterEach(() => {
(<any>$mdDialog).hide.restore();
(<any>$mdDialog).cancel.restore();
(<any>$mdDialog).show.restore();
});
it('should be a valid controller', () => {
expect(LoginController).to.be.ok;
});
it('should have initialised the auth service', () => {
expect((<any>authService).refreshTimerPromise).to.be.ok;
});
describe('dialog interactions - valid login', () => {
it('should cancel dialog when requested', () => { //@todo resolve why the cancel function is not firing the $mdDialog.show().catch() method
(<any>$scope).cancelLoginDialog();
$timeout.flush(); //flush timeout as the modal is delayed
expect($mdDialog.cancel).to.have.been.called;
expect(deferredCredentials.promise).eventually.to.be.rejected;
$scope.$apply();
});
it('should show the login dialog when prompted', () => {
authService.promptLogin();
$timeout.flush(); //flush timeout as the modal is delayed
expect($mdDialog.show).to.have.been.called;
});
it('should resolve the deferred credentials when valid login credentials are passed, then hide the dialog on success', function(){
let creds = {
username: 'foo',
password: 'bar',
};
(<any>$scope).login(creds.username, creds.password);
expect(deferredCredentials.promise).eventually.to.become(creds);
expect(loginSuccess.promise).eventually.to.become('success');
$scope.$apply();
return loginSuccess.promise.then(() => {
expect($mdDialog.hide).to.have.been.called;
});
});
after(() => {
fixtures.failLogin = true; //set up login failure for next describe block
});
});
describe('dialog interactions - invalid login', () => {
it('should show an error when invalid login credentials are passed', (done) => {
let creds = {
username: 'foo',
password: 'bar',
};
(<any>$scope).login(creds.username, creds.password);
expect(deferredCredentials.promise).eventually.to.become(creds);
expect(loginSuccess.promise).eventually.to.be.rejectedWith('error');
$scope.$apply();
loginSuccess.promise.finally(() => {
expect((<any>$scope).loginError).to.have.length.above(0);
done();
});
});
});
});
});