74053: redirectOn404Or401

This commit is contained in:
Kristof De Langhe
2020-10-27 11:25:12 +01:00
parent d4bd128b67
commit 736704aae8
8 changed files with 36 additions and 24 deletions

View File

@@ -13,7 +13,8 @@ import {
getRequestFromRequestUUID,
getResourceLinksFromResponse,
getResponseFromEntry,
getSucceededRemoteData, redirectToPageNotFoundOn404
getSucceededRemoteData,
redirectOn404Or401
} from './operators';
import { RemoteData } from '../data/remote-data';
import { RemoteDataError } from '../data/remote-data-error';
@@ -23,7 +24,7 @@ import {
createSuccessfulRemoteDataObject
} from '../../shared/remote-data.utils';
describe('Core Module - RxJS Operators', () => {
fdescribe('Core Module - RxJS Operators', () => {
let scheduler: TestScheduler;
let requestService: RequestService;
const testRequestHref = 'https://rest.api/';
@@ -199,7 +200,7 @@ describe('Core Module - RxJS Operators', () => {
});
});
describe('redirectToPageNotFoundOn404', () => {
describe('redirectOn404Or401', () => {
let router;
beforeEach(() => {
router = jasmine.createSpyObj('router', ['navigateByUrl']);
@@ -208,21 +209,28 @@ describe('Core Module - RxJS Operators', () => {
it('should call navigateByUrl to a 404 page, when the remote data contains a 404 error', () => {
const testRD = createFailedRemoteDataObject(undefined, new RemoteDataError(404, 'Not Found', 'Object was not found'));
observableOf(testRD).pipe(redirectToPageNotFoundOn404(router)).subscribe();
observableOf(testRD).pipe(redirectOn404Or401(router)).subscribe();
expect(router.navigateByUrl).toHaveBeenCalledWith('/404', { skipLocationChange: true });
});
it('should not call navigateByUrl to a 404 page, when the remote data contains another error than a 404', () => {
it('should call navigateByUrl to a 401 page, when the remote data contains a 401 error', () => {
const testRD = createFailedRemoteDataObject(undefined, new RemoteDataError(401, 'Unauthorized', 'The current user is unauthorized'));
observableOf(testRD).pipe(redirectOn404Or401(router)).subscribe();
expect(router.navigateByUrl).toHaveBeenCalledWith('/401', { skipLocationChange: true });
});
it('should not call navigateByUrl to a 404 or 401 page, when the remote data contains another error than a 404 or 401', () => {
const testRD = createFailedRemoteDataObject(undefined, new RemoteDataError(500, 'Server Error', 'Something went wrong'));
observableOf(testRD).pipe(redirectToPageNotFoundOn404(router)).subscribe();
observableOf(testRD).pipe(redirectOn404Or401(router)).subscribe();
expect(router.navigateByUrl).not.toHaveBeenCalled();
});
it('should not call navigateByUrl to a 404 page, when the remote data contains no error', () => {
it('should not call navigateByUrl to a 404 or 401 page, when the remote data contains no error', () => {
const testRD = createSuccessfulRemoteDataObject(undefined);
observableOf(testRD).pipe(redirectToPageNotFoundOn404(router)).subscribe();
observableOf(testRD).pipe(redirectOn404Or401(router)).subscribe();
expect(router.navigateByUrl).not.toHaveBeenCalled();
});
});