Skip to content

Commit

Permalink
refactored unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarianS23 committed Jul 6, 2024
1 parent 9435386 commit d81d53c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<ng-container *ngIf="user.isAdmin && user.status === userStatuses.NeverLogged">
<button mat-menu-item (click)="sendInvitation.emit({user, adminType})">{{ 'BUTTONS.SEND_INVITATION' | translate }}</button>
</ng-container>
<button *ngIf="user.role !== 'PARENTS'" mat-menu-item (click)="delete.emit(user)">{{ 'BUTTONS.DELETE_USER' | translate }}</button>
<button *ngIf="user.role === UserTabsTitles.child" mat-menu-item (click)="delete.emit(user)">{{ 'BUTTONS.DELETE_USER' | translate }}</button>
</mat-menu>
</td>
</ng-container>
Expand Down
3 changes: 2 additions & 1 deletion src/app/shared/components/users-list/users-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/app/shared/models/child.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class Child implements Person {
socialGroupIds?: number[] = [];
placeOfStudy: string;
parent?: ParentWithContactInfo;
pib?: string;

constructor(childFormValue: Partial<Child>, parentId: string, id?: string) {
this.id = id;
Expand Down
28 changes: 9 additions & 19 deletions src/app/shell/admin-tools/data/users/users.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('UsersComponent', () => {
});
});
describe('onDeleteChild method', () => {
let matDialogSpy: jest.SpyInstance;
// let matDialogSpy: jest.SpyInstance;
let mockChild: Child;
beforeEach(() => {
mockChild = {
Expand All @@ -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<any>); // 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<any>);
const expectedMatDialogData = {
width: Constants.MODAL_SMALL,
data: {
type: ModalConfirmationType.deleteChild,
property: `${mockChild.firstName} ${mockChild.lastName}`
property: mockChild.pib
}
};

Expand All @@ -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<any>);

Expand All @@ -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<any>);
const deleteChildSpy = jest.spyOn<any, any>(component, 'deleteChild');
Expand Down
10 changes: 5 additions & 5 deletions src/app/shell/admin-tools/data/users/users.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down

0 comments on commit d81d53c

Please sign in to comment.