Skip to content
Snippets Groups Projects
Commit b77b5b86 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6311 do not show snackbar on existing command#errorMessage

parent 7154efb9
No related branches found
No related tags found
No related merge requests found
import { TestBed } from '@angular/core/testing';
import { import {
ApiError, ApiError,
ApiErrorAction, ApiErrorAction,
...@@ -7,6 +6,7 @@ import { ...@@ -7,6 +6,7 @@ import {
} from '@alfa-client/tech-shared'; } from '@alfa-client/tech-shared';
import { Mock, mock } from '@alfa-client/test-utils'; import { Mock, mock } from '@alfa-client/test-utils';
import { SnackBarService } from '@alfa-client/ui'; import { SnackBarService } from '@alfa-client/ui';
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing'; import { provideMockActions } from '@ngrx/effects/testing';
import { Action, Store, createAction, props } from '@ngrx/store'; import { Action, Store, createAction, props } from '@ngrx/store';
import { TypedAction } from '@ngrx/store/src/models'; import { TypedAction } from '@ngrx/store/src/models';
...@@ -26,7 +26,12 @@ import { CommandLinkRel } from '../command.linkrel'; ...@@ -26,7 +26,12 @@ import { CommandLinkRel } from '../command.linkrel';
import { CREATE_COMMAND_MESSAGE_BY_ORDER, CommandErrorMessage } from '../command.message'; import { CREATE_COMMAND_MESSAGE_BY_ORDER, CommandErrorMessage } from '../command.message';
import { CommandListResource, CommandResource, CreateCommandProps } from '../command.model'; import { CommandListResource, CommandResource, CreateCommandProps } from '../command.model';
import { CommandRepository } from '../command.repository'; import { CommandRepository } from '../command.repository';
import { CommandProps, LoadCommandListSuccessProps, createCommandFailure } from './command.actions'; import {
CommandProps,
LoadCommandListSuccessProps,
SnackBarProps,
createCommandFailure,
} from './command.actions';
import { CommandEffects } from './command.effects'; import { CommandEffects } from './command.effects';
import * as CommandActions from './command.actions'; import * as CommandActions from './command.actions';
...@@ -35,13 +40,17 @@ describe('CommandEffects', () => { ...@@ -35,13 +40,17 @@ describe('CommandEffects', () => {
let actions: Observable<Action>; let actions: Observable<Action>;
let effects: CommandEffects; let effects: CommandEffects;
const repository: Mock<CommandRepository> = mock(CommandRepository); let repository: Mock<CommandRepository>;
const snackBarService: Mock<SnackBarService> = mock(SnackBarService); let snackBarService: Mock<SnackBarService>;
const store: Mock<SnackBarService> = mock(SnackBarService); let store: Mock<Store>;
let testScheduler: TestScheduler; let testScheduler: TestScheduler;
beforeEach(() => { beforeEach(() => {
repository = mock(CommandRepository);
snackBarService = mock(SnackBarService);
store = mock(Store);
testScheduler = new TestScheduler((actual, expected) => expect(actual).toEqual(expected)); testScheduler = new TestScheduler((actual, expected) => expect(actual).toEqual(expected));
TestBed.configureTestingModule({ TestBed.configureTestingModule({
...@@ -374,7 +383,8 @@ describe('CommandEffects', () => { ...@@ -374,7 +383,8 @@ describe('CommandEffects', () => {
const createCommandProps: CreateCommandProps = createCreateCommandProps(); const createCommandProps: CreateCommandProps = createCreateCommandProps();
const command: CommandResource = createCommandResource(); const command: CommandResource = createCommandResource();
it('should show snackBar on existing snackBarMessage', () => { it('should call handle snackbar by command', () => {
effects.handleSnackbarByCommand = jest.fn();
const showSnackbarAction: TypedAction<string> = CommandActions.showSnackbar({ const showSnackbarAction: TypedAction<string> = CommandActions.showSnackbar({
createCommandProps, createCommandProps,
command, command,
...@@ -383,6 +393,19 @@ describe('CommandEffects', () => { ...@@ -383,6 +393,19 @@ describe('CommandEffects', () => {
actions = of(showSnackbarAction); actions = of(showSnackbarAction);
effects.showSnackbar$.subscribe(); effects.showSnackbar$.subscribe();
expect(effects.handleSnackbarByCommand).toHaveBeenCalled();
});
});
describe('handle snackbar by command', () => {
const createCommandProps: CreateCommandProps = createCreateCommandProps();
const command: CommandResource = createCommandResource();
it('should show snackBar on existing snackBarMessage', () => {
const snackBarProps: SnackBarProps = { createCommandProps, command };
effects.handleSnackbarByCommand(snackBarProps);
expect(snackBarService.show).toHaveBeenCalledWith( expect(snackBarService.show).toHaveBeenCalledWith(
command, command,
createCommandProps.snackBarMessage, createCommandProps.snackBarMessage,
...@@ -390,13 +413,12 @@ describe('CommandEffects', () => { ...@@ -390,13 +413,12 @@ describe('CommandEffects', () => {
}); });
it('should show snackBar on undefined snackBarMessage', () => { it('should show snackBar on undefined snackBarMessage', () => {
const showSnackbarAction: TypedAction<string> = CommandActions.showSnackbar({ const snackBarProps: SnackBarProps = {
createCommandProps: { ...createCommandProps, snackBarMessage: undefined }, createCommandProps: { ...createCommandProps, snackBarMessage: undefined },
command, command,
}); };
actions = of(showSnackbarAction); effects.handleSnackbarByCommand(snackBarProps);
effects.showSnackbar$.subscribe();
expect(snackBarService.show).toHaveBeenCalledWith( expect(snackBarService.show).toHaveBeenCalledWith(
command, command,
...@@ -404,16 +426,24 @@ describe('CommandEffects', () => { ...@@ -404,16 +426,24 @@ describe('CommandEffects', () => {
); );
}); });
//Faellt um, wenn man ihn mit den anderen Tests zusammen laufen laesst it('should NOT show snackBar on empty snackBarMessage', () => {
it.skip('FIXME should NOT show snackBar on empty snackBarMessage', () => { const snackBarProps: SnackBarProps = {
const showSnackbarAction: TypedAction<string> = CommandActions.showSnackbar({
createCommandProps: { ...createCommandProps, snackBarMessage: EMPTY_STRING }, createCommandProps: { ...createCommandProps, snackBarMessage: EMPTY_STRING },
command, command,
};
effects.handleSnackbarByCommand(snackBarProps);
expect(snackBarService.show).not.toHaveBeenCalled();
}); });
snackBarService.show.mockClear();
effects.showSnackbar$.subscribe(); it('should NOT show snackBar on existing error message', () => {
actions = of(showSnackbarAction); const snackBarProps: SnackBarProps = {
createCommandProps,
command: { ...command, errorMessage: 'dummyErrorMessage' },
};
effects.handleSnackbarByCommand(snackBarProps);
expect(snackBarService.show).not.toHaveBeenCalled(); expect(snackBarService.show).not.toHaveBeenCalled();
}); });
......
import { Injectable } from '@angular/core'; import { EMPTY_STRING } from '@alfa-client/tech-shared';
import { SnackBarService } from '@alfa-client/ui'; import { SnackBarService } from '@alfa-client/ui';
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects'; import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { TypedAction } from '@ngrx/store/src/models'; import { TypedAction } from '@ngrx/store/src/models';
import { isEmpty, isEqual, isUndefined } from 'lodash-es';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { catchError, delay, map, mergeMap, switchMap, tap } from 'rxjs/operators'; import { catchError, delay, map, mergeMap, switchMap, tap } from 'rxjs/operators';
import { COMMAND_ERROR_MESSAGES, CREATE_COMMAND_MESSAGE_BY_ORDER } from '../command.message'; import { COMMAND_ERROR_MESSAGES, CREATE_COMMAND_MESSAGE_BY_ORDER } from '../command.message';
import { CommandListResource, CommandResource, CreateCommandProps } from '../command.model'; import { CommandListResource, CommandResource, CreateCommandProps } from '../command.model';
import { CommandRepository } from '../command.repository'; import { CommandRepository } from '../command.repository';
import { hasCommandError, isConcurrentModification, isPending, isRevokeable } from '../command.util'; import {
hasCommandError,
isConcurrentModification,
isPending,
isRevokeable,
} from '../command.util';
import { import {
CommandProps, CommandProps,
LoadCommandListProps, LoadCommandListProps,
...@@ -27,8 +34,6 @@ import { ...@@ -27,8 +34,6 @@ import {
showRevokeSnackbar, showRevokeSnackbar,
showSnackbar, showSnackbar,
} from './command.actions'; } from './command.actions';
import { isEqual, isUndefined } from 'lodash-es';
import { EMPTY_STRING } from '@alfa-client/tech-shared';
@Injectable() @Injectable()
export class CommandEffects { export class CommandEffects {
...@@ -99,6 +104,8 @@ export class CommandEffects { ...@@ -99,6 +104,8 @@ export class CommandEffects {
} }
if (hasCommandError(command) && isConcurrentModification(command.errorMessage)) { if (hasCommandError(command) && isConcurrentModification(command.errorMessage)) {
this.showError(command); this.showError(command);
//FIXME Anstelle der createCommandSucess Action sollte eine createCommandFailure Action geworfen werden.
//Hierzu muss ein HttpErrorResponse für die errorMessage definieren werden
return [createCommandSuccess({ command }), publishConcurrentModificationAction()]; return [createCommandSuccess({ command }), publishConcurrentModificationAction()];
} }
return [createCommandSuccess({ command }), showSnackbar({ createCommandProps, command })]; return [createCommandSuccess({ command }), showSnackbar({ createCommandProps, command })];
...@@ -131,15 +138,20 @@ export class CommandEffects { ...@@ -131,15 +138,20 @@ export class CommandEffects {
() => () =>
this.actions$.pipe( this.actions$.pipe(
ofType(showSnackbar), ofType(showSnackbar),
tap((props: SnackBarProps) => { tap((props: SnackBarProps) => this.handleSnackbarByCommand(props)),
if (!isEqual(props.createCommandProps.snackBarMessage, EMPTY_STRING)) {
this.snackbarService.show(props.command, this.getSnackBarMessage(props));
}
}),
), ),
{ dispatch: false }, { dispatch: false },
); );
handleSnackbarByCommand(props: SnackBarProps): void {
if (
!isEqual(props.createCommandProps.snackBarMessage, EMPTY_STRING) &&
isEmpty(props.command.errorMessage)
) {
this.snackbarService.show(props.command, this.getSnackBarMessage(props));
}
}
private getSnackBarMessage(props: SnackBarProps): string { private getSnackBarMessage(props: SnackBarProps): string {
return isUndefined(props.createCommandProps.snackBarMessage) ? return isUndefined(props.createCommandProps.snackBarMessage) ?
CREATE_COMMAND_MESSAGE_BY_ORDER[props.command.order] CREATE_COMMAND_MESSAGE_BY_ORDER[props.command.order]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment