Fixed issue with authentication when SSR is disabled

This commit is contained in:
Giuseppe Digilio
2020-04-28 10:06:34 +02:00
parent 5c7cd4a27d
commit 4a65051641
3 changed files with 72 additions and 21 deletions

View File

@@ -1,9 +1,9 @@
import { TestBed } from '@angular/core/testing';
import { fakeAsync, flush, TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Store } from '@ngrx/store';
import { Store, StoreModule } from '@ngrx/store';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { cold, hot } from 'jasmine-marbles';
import { Observable, of as observableOf, throwError as observableThrow } from 'rxjs';
import { AuthEffects } from './auth.effects';
@@ -29,41 +29,53 @@ import {
} from './auth.actions';
import { authMethodsMock, AuthServiceStub } from '../../shared/testing/auth-service.stub';
import { AuthService } from './auth.service';
import { AuthState } from './auth.reducer';
import { authReducer } from './auth.reducer';
import { AuthStatus } from './models/auth-status.model';
import { EPersonMock } from '../../shared/testing/eperson.mock';
import { AppState, storeModuleConfig } from '../../app.reducer';
import { StoreActionTypes } from '../../store.actions';
import { isAuthenticated, isAuthenticatedLoaded } from './selectors';
describe('AuthEffects', () => {
fdescribe('AuthEffects', () => {
let authEffects: AuthEffects;
let actions: Observable<any>;
let authServiceStub;
const store: Store<AuthState> = jasmine.createSpyObj('store', {
/* tslint:disable:no-empty */
dispatch: {},
/* tslint:enable:no-empty */
select: observableOf(true)
});
let initialState;
let token;
let store: MockStore<AppState>;
function init() {
authServiceStub = new AuthServiceStub();
token = authServiceStub.getToken();
initialState = {
core: {
auth: {
authenticated: false,
loaded: false,
loading: false,
authMethods: []
}
}
};
}
beforeEach(() => {
init();
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({ auth: authReducer }, storeModuleConfig)
],
providers: [
AuthEffects,
provideMockStore({ initialState }),
{ provide: AuthService, useValue: authServiceStub },
{ provide: Store, useValue: store },
provideMockActions(() => actions),
// other providers
],
});
authEffects = TestBed.get(AuthEffects);
store = TestBed.get(Store);
});
describe('authenticate$', () => {
@@ -362,4 +374,40 @@ describe('AuthEffects', () => {
});
})
});
describe('clearInvalidTokenOnRehydrate$', () => {
beforeEach(() => {
store.overrideSelector(isAuthenticated, false);
});
describe('when auth loaded is false', () => {
it('should not call removeToken method', (done) => {
store.overrideSelector(isAuthenticatedLoaded, false);
actions = hot('--a-|', { a: { type: StoreActionTypes.REHYDRATE } });
spyOn(authServiceStub, 'removeToken');
authEffects.clearInvalidTokenOnRehydrate$.subscribe(() => {
expect(authServiceStub.removeToken).not.toHaveBeenCalled();
});
done();
});
});
describe('when auth loaded is true', () => {
it('should call removeToken method', fakeAsync(() => {
store.overrideSelector(isAuthenticatedLoaded, true);
actions = hot('--a-|', { a: { type: StoreActionTypes.REHYDRATE } });
spyOn(authServiceStub, 'removeToken');
authEffects.clearInvalidTokenOnRehydrate$.subscribe(() => {
expect(authServiceStub.removeToken).toHaveBeenCalled();
flush();
});
}));
});
});
});