From d81d53cf7c759e23a377be9af3e798df716f873f Mon Sep 17 00:00:00 2001 From: MarianS23 Date: Sat, 6 Jul 2024 11:41:05 +0300 Subject: [PATCH] refactored unit tests --- .../users-list/users-list.component.html | 2 +- .../users-list/users-list.component.ts | 3 +- src/app/shared/models/child.model.ts | 1 + .../data/users/users.component.spec.ts | 28 ++++++------------- .../admin-tools/data/users/users.component.ts | 10 +++---- 5 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/app/shared/components/users-list/users-list.component.html b/src/app/shared/components/users-list/users-list.component.html index abe2af5a5a..a54fd888d1 100644 --- a/src/app/shared/components/users-list/users-list.component.html +++ b/src/app/shared/components/users-list/users-list.component.html @@ -133,7 +133,7 @@ - + diff --git a/src/app/shared/components/users-list/users-list.component.ts b/src/app/shared/components/users-list/users-list.component.ts index 66a90ac38e..da2f064ec2 100644 --- a/src/app/shared/components/users-list/users-list.component.ts +++ b/src/app/shared/components/users-list/users-list.component.ts @@ -6,6 +6,7 @@ import { Store } from '@ngxs/store'; import { Constants } from 'shared/constants/constants'; import { UserStatusesTitles } from 'shared/enum/enumUA/statuses'; +import { UserTabsTitles } from 'shared/enum/enumUA/user'; import { Role } from 'shared/enum/role'; import { EmailConfirmationStatuses, UserStatuses, UserStatusIcons } from 'shared/enum/statuses'; import { InvitationData, UnionTableData } from 'shared/models/users-table'; @@ -34,7 +35,7 @@ export class UsersListComponent implements OnInit, AfterViewInit, OnChanges { @ViewChild(MatSort) private sort: MatSort; - + public readonly UserTabsTitles = UserTabsTitles; public readonly userStatuses = UserStatuses; public readonly statuses = UserStatusesTitles; public readonly statusIcons = UserStatusIcons; diff --git a/src/app/shared/models/child.model.ts b/src/app/shared/models/child.model.ts index b38bf45c08..21847ebfe1 100644 --- a/src/app/shared/models/child.model.ts +++ b/src/app/shared/models/child.model.ts @@ -16,6 +16,7 @@ export class Child implements Person { socialGroupIds?: number[] = []; placeOfStudy: string; parent?: ParentWithContactInfo; + pib?: string; constructor(childFormValue: Partial, parentId: string, id?: string) { this.id = id; diff --git a/src/app/shell/admin-tools/data/users/users.component.spec.ts b/src/app/shell/admin-tools/data/users/users.component.spec.ts index 7052272f70..31d3bdac66 100644 --- a/src/app/shell/admin-tools/data/users/users.component.spec.ts +++ b/src/app/shell/admin-tools/data/users/users.component.spec.ts @@ -223,7 +223,7 @@ describe('UsersComponent', () => { }); }); describe('onDeleteChild method', () => { - let matDialogSpy: jest.SpyInstance; + // let matDialogSpy: jest.SpyInstance; let mockChild: Child; beforeEach(() => { mockChild = { @@ -234,30 +234,20 @@ describe('UsersComponent', () => { dateOfBirth: '', gender: 0, isParent: null, - placeOfStudy: '' + placeOfStudy: '', + pib: 'qwerty' }; - // const mockChild: Child = { - // id: '1', - // firstName: 'John', - // lastName: 'Doe', - // dateOfBirth: '', - // gender: 0, - // parentId: '', - // isParent: false, - // placeOfStudy: '' - // }; - - matDialogSpy = jest.spyOn(matDialog, 'open').mockReturnValue({ - afterClosed: () => of(true) // Mocking the afterClosed method correctly - } as MatDialogRef); // Use `any` here to avoid type checking issues }); it('should open dialog with correct parameters', () => { + const matDialogSpy: jest.SpyInstance = jest.spyOn(matDialog, 'open').mockReturnValue({ + afterClosed: () => of(true) + } as MatDialogRef); const expectedMatDialogData = { width: Constants.MODAL_SMALL, data: { type: ModalConfirmationType.deleteChild, - property: `${mockChild.firstName} ${mockChild.lastName}` + property: mockChild.pib } }; @@ -267,7 +257,7 @@ describe('UsersComponent', () => { }); it('should call deleteChild with correct parameters when confirmed', () => { - matDialogSpy.mockReturnValueOnce({ + jest.spyOn(matDialog, 'open').mockReturnValueOnce({ afterClosed: () => of(true) } as MatDialogRef); @@ -279,7 +269,7 @@ describe('UsersComponent', () => { }); it('should not call deleteChild when not confirmed', () => { - matDialogSpy.mockReturnValueOnce({ + jest.spyOn(matDialog, 'open').mockReturnValueOnce({ afterClosed: () => of(false) } as MatDialogRef); const deleteChildSpy = jest.spyOn(component, 'deleteChild'); diff --git a/src/app/shell/admin-tools/data/users/users.component.ts b/src/app/shell/admin-tools/data/users/users.component.ts index c9ccd530bd..26af874611 100644 --- a/src/app/shell/admin-tools/data/users/users.component.ts +++ b/src/app/shell/admin-tools/data/users/users.component.ts @@ -127,20 +127,20 @@ export class UsersComponent implements OnInit, OnDestroy { /** * This method delete child By Id */ + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types public onDeleteChild(child: Child): void { this.matDialog .open(ConfirmationModalWindowComponent, { width: Constants.MODAL_SMALL, data: { type: ModalConfirmationType.deleteChild, - property: `${child.firstName} ${child.lastName}` + property: child.pib } }) .afterClosed() - .subscribe((result: boolean) => { - if (result) { - this.deleteChild(child.id, this.childrenParams); - } + .pipe(filter(Boolean)) + .subscribe(() => { + this.deleteChild(child.id, this.childrenParams); }); }