Skip to content

Commit

Permalink
#931: test for DeleteUserComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder committed Aug 5, 2024
1 parent fc92272 commit 882b132
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,118 @@
import { ComponentFixture, TestBed } 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]',
};

describe('DeleteUserComponent', () => {
it('todo write tests', () => {});
let component: DeleteUserComponent;
let fixture: ComponentFixture<DeleteUserComponent>;

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

const matDialogMock = {
close: jest.fn(),
open: jest.fn(),
};

const matDialogRefMock = {
afterClosed: jest.fn(),
};

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [],
imports: [],
providers: [{ provide: UserService, useValue: userServiceMock }],
}).compileComponents();

fixture = TestBed.createComponent(DeleteUserComponent);
component = fixture.componentInstance;
});

it('should create component', () => {
expect(component).toBeTruthy();
});

it('should return true if user is in a team', () => {
component.user = testUserWithTeam;
expect(component.isUserMemberOfTeams()).toBe(true);
});

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

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

it('should return true if userOkrData has no keyResults', () => {
component.user = testUserWithoutTeam;
component.userOkrData = {
keyResults: [],
};

expect(component.isUserOwnerOfKeyResults()).toBe(false);
});

it('should return true if user is owner of keyResults', () => {
component.user = testUserWithoutTeam;
component.userOkrData = {
keyResults: [
{
keyResultId: 100,
keyResultName: 'keyResult1',
objectiveId: 200,
objectiveName: 'objective1',
},
],
};

expect(component.isUserOwnerOfKeyResults()).toBe(true);
});

it('should delete user if user is not in a team and user has no keyResults', () => {
component.user = testUserWithoutTeam;
component.userOkrData = {
keyResults: [],
};
matDialogMock.open.mockReturnValue(matDialogRefMock);
matDialogRefMock.afterClosed.mockReturnValue(of());
userServiceMock.deleteUser.mockReturnValue(of());

component.deleteUser();

setTimeout(() => expect(userServiceMock.deleteUser).toHaveBeenCalled(), 100);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { UserOkrData } from '../../shared/types/model/UserOkrData';
export class DeleteUserComponent implements OnInit {
@Input({ required: true }) user!: User;

private userOkrData: UserOkrData | undefined;
userOkrData: UserOkrData | undefined;

constructor(
private readonly userService: UserService,
Expand All @@ -26,9 +26,7 @@ export class DeleteUserComponent implements OnInit {
) {}

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

isUserMemberOfTeams(): boolean {
Expand Down

0 comments on commit 882b132

Please sign in to comment.