From b77b5b86b81283655cc2df9fd97c40081f46c260 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Mon, 26 Aug 2024 13:31:01 +0200 Subject: [PATCH] OZG-6311 do not show snackbar on existing command#errorMessage --- .../src/lib/+state/command.effects.spec.ts | 64 ++++++++++++++----- .../src/lib/+state/command.effects.ts | 30 ++++++--- 2 files changed, 68 insertions(+), 26 deletions(-) diff --git a/alfa-client/libs/command-shared/src/lib/+state/command.effects.spec.ts b/alfa-client/libs/command-shared/src/lib/+state/command.effects.spec.ts index c461fcffe5..2ee1199a7e 100644 --- a/alfa-client/libs/command-shared/src/lib/+state/command.effects.spec.ts +++ b/alfa-client/libs/command-shared/src/lib/+state/command.effects.spec.ts @@ -1,4 +1,3 @@ -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, - }); - snackBarService.show.mockClear(); + }; - effects.showSnackbar$.subscribe(); - actions = of(showSnackbarAction); + effects.handleSnackbarByCommand(snackBarProps); + + expect(snackBarService.show).not.toHaveBeenCalled(); + }); + + 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(); }); diff --git a/alfa-client/libs/command-shared/src/lib/+state/command.effects.ts b/alfa-client/libs/command-shared/src/lib/+state/command.effects.ts index 3d7151381f..76c42d3899 100644 --- a/alfa-client/libs/command-shared/src/lib/+state/command.effects.ts +++ b/alfa-client/libs/command-shared/src/lib/+state/command.effects.ts @@ -1,14 +1,21 @@ -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] -- GitLab