diff --git a/alfa-client/libs/ui/src/lib/ui/ozgcloud-dialog/ozgcloud-dialog.service.spec.ts b/alfa-client/libs/ui/src/lib/ui/ozgcloud-dialog/ozgcloud-dialog.service.spec.ts index 0a38c5ca6d35455763e021836fc40c9efced0160..e7c4b6524db9932fd85899a95f299bdc788b7486 100644 --- a/alfa-client/libs/ui/src/lib/ui/ozgcloud-dialog/ozgcloud-dialog.service.spec.ts +++ b/alfa-client/libs/ui/src/lib/ui/ozgcloud-dialog/ozgcloud-dialog.service.spec.ts @@ -1,17 +1,16 @@ import { TestBed } from '@angular/core/testing'; -import { Dialog } from '@angular/cdk/dialog'; +import { Dialog, DialogConfig } from '@angular/cdk/dialog'; import { OzgcloudDialogService } from './ozgcloud-dialog.service'; +import { mock } from '@alfa-client/test-utils'; describe('OzgcloudDialogService', () => { let service: OzgcloudDialogService; const component = <any>{ name: 'Component' }; - const dialog = { - open: jest.fn(), - getDialogById: jest.fn(), - closeAll: jest.fn(), - }; + const dialog = mock(Dialog); + const dialogData = { id: 'ZumBeispiel' }; + const dialogConfigWithData: DialogConfig = { data: dialogData }; beforeEach(() => { TestBed.configureTestingModule({ @@ -30,26 +29,42 @@ describe('OzgcloudDialogService', () => { }); describe('open', () => { - const config = <any>{ id: 'ZumBeispiel' }; + beforeEach(() => { + dialog.open.mockReset(); + }); + + it('should open dialog with data', () => { + service.open(component, dialogData); - it('should call dialog open with config', () => { - const openMock = (service.open = jest.fn()); + expect(dialog.open).toHaveBeenCalledWith(component, dialogConfigWithData); + }); - service.open(component, config); + it('should open dialog wihtout data', () => { + service.open(component); - expect(openMock).toHaveBeenCalledWith(component, config); + expect(dialog.open).toHaveBeenCalledWith(component, undefined); }); }); describe('openWizard', () => { - const config = <any>{ id: 'ZumBeispiel' }; + beforeEach(() => { + dialog.open.mockReset(); + }); + + it('should open wizard dialog', () => { + service.openWizard(component); + + expect(dialog.open).toHaveBeenCalledWith(component, { + ...service.WIZARD_DIALOG_CONFIG, + }); + }); - it('should call dialog open with conifg', () => { - service.openWizard(component, config); + it('should open wizard dialog with data', () => { + service.openWizard(component, dialogData); expect(dialog.open).toHaveBeenCalledWith(component, { - data: config, ...service.WIZARD_DIALOG_CONFIG, + data: dialogData, }); }); }); diff --git a/alfa-client/libs/ui/src/lib/ui/ozgcloud-dialog/ozgcloud-dialog.service.ts b/alfa-client/libs/ui/src/lib/ui/ozgcloud-dialog/ozgcloud-dialog.service.ts index 1ae5d7f1a7e6906717f27aa3ba2589563b350f50..449aae405b44aa5f59152749e4e40c4e00798cfd 100644 --- a/alfa-client/libs/ui/src/lib/ui/ozgcloud-dialog/ozgcloud-dialog.service.ts +++ b/alfa-client/libs/ui/src/lib/ui/ozgcloud-dialog/ozgcloud-dialog.service.ts @@ -1,6 +1,7 @@ import { Dialog, DialogConfig, DialogRef } from '@angular/cdk/dialog'; import { ComponentType } from '@angular/cdk/portal'; import { Injectable } from '@angular/core'; +import { isNil } from 'lodash-es'; @Injectable({ providedIn: 'root', @@ -14,10 +15,24 @@ export class OzgcloudDialogService { constructor(private dialog: Dialog) {} public openWizard<T, D>(component: ComponentType<T>, data?: D): DialogRef<T> { - return this.open<T, D>(component, data); + return this.openDialog<T>( + component, + this.buildDialogConfigWithData<D>(data, this.WIZARD_DIALOG_CONFIG), + ); } public open<T, D>(component: ComponentType<T>, data?: D): DialogRef<T> { - return this.dialog.open<T, D>(component, { ...this.WIZARD_DIALOG_CONFIG, data }); + return this.openDialog(component, this.buildDialogConfigWithData(data)); + } + + private buildDialogConfigWithData<D>(data: D, dialogConfig?: DialogConfig): DialogConfig | null { + if (isNil(data)) { + return dialogConfig; + } + return { ...dialogConfig, data }; + } + + private openDialog<T>(component: ComponentType<T>, dialogConfig?: DialogConfig): DialogRef<T> { + return this.dialog.open<T>(component, dialogConfig); } }