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 {
ApiError,
ApiErrorAction,
......@@ -7,6 +6,7 @@ import {
} from '@alfa-client/tech-shared';
import { Mock, mock } from '@alfa-client/test-utils';
import { SnackBarService } from '@alfa-client/ui';
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Action, Store, createAction, props } from '@ngrx/store';
import { TypedAction } from '@ngrx/store/src/models';
......@@ -26,7 +26,12 @@ import { CommandLinkRel } from '../command.linkrel';
import { CREATE_COMMAND_MESSAGE_BY_ORDER, CommandErrorMessage } from '../command.message';
import { CommandListResource, CommandResource, CreateCommandProps } from '../command.model';
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 * as CommandActions from './command.actions';
......@@ -35,13 +40,17 @@ describe('CommandEffects', () => {
let actions: Observable<Action>;
let effects: CommandEffects;
const repository: Mock<CommandRepository> = mock(CommandRepository);
const snackBarService: Mock<SnackBarService> = mock(SnackBarService);
const store: Mock<SnackBarService> = mock(SnackBarService);
let repository: Mock<CommandRepository>;
let snackBarService: Mock<SnackBarService>;
let store: Mock<Store>;
let testScheduler: TestScheduler;
beforeEach(() => {
repository = mock(CommandRepository);
snackBarService = mock(SnackBarService);
store = mock(Store);
testScheduler = new TestScheduler((actual, expected) => expect(actual).toEqual(expected));
TestBed.configureTestingModule({
......@@ -374,7 +383,8 @@ describe('CommandEffects', () => {
const createCommandProps: CreateCommandProps = createCreateCommandProps();
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({
createCommandProps,
command,
......@@ -383,6 +393,19 @@ describe('CommandEffects', () => {
actions = of(showSnackbarAction);
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(
command,
createCommandProps.snackBarMessage,
......@@ -390,13 +413,12 @@ describe('CommandEffects', () => {
});
it('should show snackBar on undefined snackBarMessage', () => {
const showSnackbarAction: TypedAction<string> = CommandActions.showSnackbar({
const snackBarProps: SnackBarProps = {
createCommandProps: { ...createCommandProps, snackBarMessage: undefined },
command,
});
};
actions = of(showSnackbarAction);
effects.showSnackbar$.subscribe();
effects.handleSnackbarByCommand(snackBarProps);
expect(snackBarService.show).toHaveBeenCalledWith(
command,
......@@ -404,16 +426,24 @@ describe('CommandEffects', () => {
);
});
//Faellt um, wenn man ihn mit den anderen Tests zusammen laufen laesst
it.skip('FIXME should NOT show snackBar on empty snackBarMessage', () => {
const showSnackbarAction: TypedAction<string> = CommandActions.showSnackbar({
it('should NOT show snackBar on empty snackBarMessage', () => {
const snackBarProps: SnackBarProps = {
createCommandProps: { ...createCommandProps, snackBarMessage: EMPTY_STRING },
command,
};
effects.handleSnackbarByCommand(snackBarProps);
expect(snackBarService.show).not.toHaveBeenCalled();
});
snackBarService.show.mockClear();
effects.showSnackbar$.subscribe();
actions = of(showSnackbarAction);
it('should NOT show snackBar on existing error message', () => {
const snackBarProps: SnackBarProps = {
createCommandProps,
command: { ...command, errorMessage: 'dummyErrorMessage' },
};
effects.handleSnackbarByCommand(snackBarProps);
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 { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { TypedAction } from '@ngrx/store/src/models';
import { isEmpty, isEqual, isUndefined } from 'lodash-es';
import { of } from 'rxjs';
import { catchError, delay, map, mergeMap, switchMap, tap } from 'rxjs/operators';
import { COMMAND_ERROR_MESSAGES, CREATE_COMMAND_MESSAGE_BY_ORDER } from '../command.message';
import { CommandListResource, CommandResource, CreateCommandProps } from '../command.model';
import { CommandRepository } from '../command.repository';
import { hasCommandError, isConcurrentModification, isPending, isRevokeable } from '../command.util';
import {
hasCommandError,
isConcurrentModification,
isPending,
isRevokeable,
} from '../command.util';
import {
CommandProps,
LoadCommandListProps,
......@@ -27,8 +34,6 @@ import {
showRevokeSnackbar,
showSnackbar,
} from './command.actions';
import { isEqual, isUndefined } from 'lodash-es';
import { EMPTY_STRING } from '@alfa-client/tech-shared';
@Injectable()
export class CommandEffects {
......@@ -99,6 +104,8 @@ export class CommandEffects {
}
if (hasCommandError(command) && isConcurrentModification(command.errorMessage)) {
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 }), showSnackbar({ createCommandProps, command })];
......@@ -131,15 +138,20 @@ export class CommandEffects {
() =>
this.actions$.pipe(
ofType(showSnackbar),
tap((props: SnackBarProps) => {
if (!isEqual(props.createCommandProps.snackBarMessage, EMPTY_STRING)) {
this.snackbarService.show(props.command, this.getSnackBarMessage(props));
}
}),
tap((props: SnackBarProps) => this.handleSnackbarByCommand(props)),
),
{ 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 {
return isUndefined(props.createCommandProps.snackBarMessage) ?
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