Skip to content

Commit 9c19389

Browse files
committed
test(frontend): add new test for auth effects
1 parent a5f1063 commit 9c19389

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/main/typescript/app/core/store/auth/auth.effects.spec.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import {Http, BaseRequestOptions, ConnectionBackend, RequestOptions} from '@angular/http';
2-
import {MockBackend} from '@angular/http/testing';
1+
import {Http, BaseRequestOptions, ConnectionBackend, RequestOptions, Response, ResponseOptions} from '@angular/http';
2+
import {MockBackend, MockConnection} from '@angular/http/testing';
33
import {AuthEffects} from './auth.effects';
44
import {EffectsTestingModule, EffectsRunner} from '@ngrx/effects/testing';
5-
import {LogoutAction, ActionTypes, LoadAndProcessTokenAction, AuthSuccessAction} from './auth.actions';
6-
import {TestBed, inject} from '@angular/core/testing';
5+
import {
6+
LogoutAction, ActionTypes, LoadAndProcessTokenAction, AuthSuccessAction,
7+
AuthenticateAction, AuthenticateActionPayload
8+
} from './auth.actions';
9+
import {TestBed, inject, async} from '@angular/core/testing';
710
import {routerActions} from '@ngrx/router-store';
811

912

@@ -47,6 +50,7 @@ describe('Effects: Auth', () => {
4750
runner.queue(new LogoutAction());
4851

4952
authEffects.logout$.subscribe(result => {
53+
console.log(result);
5054
expect(result.type).toEqual(ActionTypes.CLEAR_AUTH_STATE);
5155
});
5256

@@ -56,6 +60,7 @@ describe('Effects: Auth', () => {
5660
authEffects.jwtToken = 'NON_EMPTY';
5761
runner.queue(new LoadAndProcessTokenAction());
5862
authEffects.loadAndProcessToken$.subscribe(result => {
63+
console.log(result);
5964
expect(result.type).toEqual(ActionTypes.PROCESS_TOKEN);
6065
});
6166
});
@@ -65,16 +70,16 @@ describe('Effects: Auth', () => {
6570
runner.queue(new LoadAndProcessTokenAction());
6671

6772
authEffects.loadAndProcessToken$.subscribe(result => {
73+
console.log(result);
6874
expect(result).toBeFalsy();
69-
expect(result).toBeTruthy();
7075
});
7176
});
7277

7378
it('should return actions after auth success', () => {
74-
authEffects.jwtToken = null;
7579
runner.queue(new AuthSuccessAction('TOKEN'));
7680
let eventCount = 0;
7781
authEffects.authSuccess$.subscribe(result => {
82+
console.log(result);
7883
eventCount++;
7984
if (eventCount === 1) {
8085
expect(result.type).toEqual(routerActions.GO);
@@ -87,5 +92,27 @@ describe('Effects: Auth', () => {
8792
});
8893
});
8994

95+
it('should return AuthSuccessAction after succesfull authentication', inject([MockBackend], (mockBackend: MockBackend) => {
96+
97+
const returnedToken = 'HERE.SHOULD.BE.TOKEN';
98+
99+
mockBackend.connections.subscribe(
100+
(connection: MockConnection) => {
101+
console.log('connection made');
102+
connection.mockRespond(new Response(
103+
new ResponseOptions({
104+
body: returnedToken
105+
}
106+
)));
107+
});
108+
109+
runner.queue(new AuthenticateAction(new AuthenticateActionPayload('admin', 'admin')));
110+
authEffects.authenticate$.subscribe(result => {
111+
console.log(result);
112+
expect(result.type).toEqual(ActionTypes.AUTH_SUCCESS);
113+
expect(result.payload).toEqual(returnedToken);
114+
});
115+
}));
116+
90117

91118
});

0 commit comments

Comments
 (0)