Skip to content

Commit

Permalink
#931: tests for deleteUser()
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder committed Aug 7, 2024
1 parent 4f98384 commit a084700
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { UserService } from '../../services/user.service';
import { team1 } from '../../shared/testData';
import { DeleteUserComponent } from './delete-user.component';
import { User } from '../../shared/types/model/User';
import { of } from 'rxjs';

export const testUserWithTeam: User = {
id: 1,
firstname: 'Bob',
lastname: 'Baumeister',
isOkrChampion: false,
userTeamList: [
{
id: 1,
team: team1,
isTeamAdmin: false,
},
],
email: '[email protected]',
};

export const testUserWithoutTeam: User = {
id: 2,
firstname: 'Hans',
lastname: 'Muster',
isOkrChampion: false,
userTeamList: [],
email: '[email protected]',
};
import { UserOkrData } from '../../shared/types/model/UserOkrData';

describe('DeleteUserComponent', () => {
let component: DeleteUserComponent;
let fixture: ComponentFixture<DeleteUserComponent>;

const userServiceMock = {
deleteUser: jest.fn(),
isUserMemberOfTeams: jest.fn(),
getUserOkrData: jest.fn(),
};

const matDialogMock = {
Expand All @@ -47,6 +24,7 @@ describe('DeleteUserComponent', () => {
};

beforeEach(async () => {
jest.resetAllMocks();
await TestBed.configureTestingModule({
declarations: [],
imports: [],
Expand All @@ -61,24 +39,66 @@ describe('DeleteUserComponent', () => {
expect(component).toBeTruthy();
});

it('should return true if user is in a team', () => {
component.user = testUserWithTeam;
it('should load UserOkrData and MemberTeamStatus (case is member of teams + with KeyResults)', fakeAsync(() => {
const userOkrData: UserOkrData = {
keyResults: [
{
keyResultId: 100,
keyResultName: 'keyResult1',
objectiveId: 200,
objectiveName: 'objective1',
},
],
};

component.currentTeams$ = of([]);
userServiceMock.getUserOkrData.mockReturnValue(of(userOkrData));
userServiceMock.isUserMemberOfTeams.mockReturnValue(of(true));

component.ngOnInit();
tick();

expect(component.userOkrData).toBe(userOkrData);
expect(component.userIsMemberOfTeams).toBe(true);
}));

it('should load UserOkrData and MemberTeamStatus (case not member of teams + no KeyResults)', fakeAsync(() => {
const userOkrDataWithoutKeyResults: UserOkrData = {
keyResults: [],
};

component.currentTeams$ = of([]);
userServiceMock.getUserOkrData.mockReturnValue(of(userOkrDataWithoutKeyResults));
userServiceMock.isUserMemberOfTeams.mockReturnValue(of(false));

component.ngOnInit();
tick();

expect(component.userOkrData).toBe(userOkrDataWithoutKeyResults);
expect(component.userIsMemberOfTeams).toBe(false);
}));

it('should return true if userIsMemberOfTeams is undefined', () => {
component.userIsMemberOfTeams = undefined;
expect(component.isUserMemberOfTeams()).toBe(true);
});

it('should return true if userIsMemberOfTeams is true', () => {
component.userIsMemberOfTeams = true;
expect(component.isUserMemberOfTeams()).toBe(true);
});

it.skip('should return false if user it not in a team', () => {
component.user = testUserWithoutTeam;
it('should return false if userIsMemberOfTeams is false', () => {
component.userIsMemberOfTeams = false;
expect(component.isUserMemberOfTeams()).toBe(false);
});

it('should return true userOkrData is undefined', () => {
component.user = testUserWithoutTeam;
it('should return true if userOkrData is undefined', () => {
component.userOkrData = undefined;
expect(component.isUserOwnerOfKeyResults()).toBe(true);
});

it('should return false if userOkrData has no keyResults', () => {
component.user = testUserWithoutTeam;
component.userOkrData = {
keyResults: [],
};
Expand All @@ -87,7 +107,6 @@ describe('DeleteUserComponent', () => {
});

it('should return true if user is owner of keyResults', () => {
component.user = testUserWithoutTeam;
component.userOkrData = {
keyResults: [
{
Expand All @@ -102,17 +121,67 @@ describe('DeleteUserComponent', () => {
expect(component.isUserOwnerOfKeyResults()).toBe(true);
});

it('should not delete user when user is member of teams', () => {
component.user = {
id: 2,
firstname: 'Hans',
lastname: 'Muster',
isOkrChampion: false,
userTeamList: [],
email: '[email protected]',
};
component.userIsMemberOfTeams = true;

component.deleteUser();

expect(userServiceMock.deleteUser).not.toHaveBeenCalledWith(component.user);
});

it('should not delete user when user is owner of keyResults', () => {
component.user = {
id: 2,
firstname: 'Hans',
lastname: 'Muster',
isOkrChampion: false,
userTeamList: [],
email: '[email protected]',
};
component.userIsMemberOfTeams = false;
component.userOkrData = {
keyResults: [
{
keyResultId: 100,
keyResultName: 'keyResult1',
objectiveId: 200,
objectiveName: 'objective1',
},
],
};

component.deleteUser();

expect(userServiceMock.deleteUser).not.toHaveBeenCalledWith(component.user);
});

it('should delete user if user is not in a team and user has no keyResults', () => {
component.user = testUserWithoutTeam;
component.user = {
id: 2,
firstname: 'Hans',
lastname: 'Muster',
isOkrChampion: false,
userTeamList: [],
email: '[email protected]',
};
component.userOkrData = {
keyResults: [],
};
component.userIsMemberOfTeams = false;
matDialogMock.open.mockReturnValue(matDialogRefMock);
matDialogRefMock.afterClosed.mockReturnValue(of());
userServiceMock.deleteUser.mockReturnValue(of());

component.deleteUser();

setTimeout(() => expect(userServiceMock.deleteUser).toHaveBeenCalled(), 100);
setTimeout(() => expect(userServiceMock.deleteUser).toHaveBeenCalledWith(component.user), 100);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Location } from '@angular/common';
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
import { CancelDialogComponent, CancelDialogData } from '../../shared/dialog/cancel-dialog/cancel-dialog.component';
import { OKR_DIALOG_CONFIG } from '../../shared/constantLibary';
import { filter, mergeMap, Observable } from 'rxjs';
import { mergeMap, Observable } from 'rxjs';
import { AlertDialogComponent, AlertDialogData } from '../../shared/dialog/alert-dialog/alert-dialog.component';
import { UserOkrData } from '../../shared/types/model/UserOkrData';
import { UserTeam } from '../../shared/types/model/UserTeam';
Expand Down Expand Up @@ -33,9 +33,7 @@ export class DeleteUserComponent implements OnInit {
this.updateUserMemberTeamsStatus();
});

this.userService //
.getUserOkrData(this.user) //
.subscribe((okrData) => (this.userOkrData = okrData));
this.loadUserOkrData();
}

updateUserMemberTeamsStatus() {
Expand All @@ -44,6 +42,12 @@ export class DeleteUserComponent implements OnInit {
.subscribe((isMemberOfTeams) => (this.userIsMemberOfTeams = isMemberOfTeams));
}

loadUserOkrData() {
this.userService //
.getUserOkrData(this.user) //
.subscribe((okrData) => (this.userOkrData = okrData));
}

isUserMemberOfTeams(): boolean {
return this.userIsMemberOfTeams !== undefined ? this.userIsMemberOfTeams.valueOf() : true;
}
Expand Down Expand Up @@ -120,10 +124,7 @@ export class DeleteUserComponent implements OnInit {
this.dialog
.open(CancelDialogComponent, dialogConfig)
.afterClosed()
.pipe(
filter((confirm) => confirm),
mergeMap(() => this.userService.deleteUser(user)),
)
.pipe(mergeMap(() => this.userService.deleteUser(user)))
.subscribe(() => {
this.userService.reloadUsers();
this.location.back();
Expand Down

0 comments on commit a084700

Please sign in to comment.