From fc922725c8be30e13ee30ecacb5bfa3e80878327 Mon Sep 17 00:00:00 2001 From: clean-coder Date: Wed, 31 Jul 2024 13:16:50 +0200 Subject: [PATCH] #931: additional test for getUserOkrData() and deleteUser() in UserService --- .../src/app/services/user.service.spec.ts | 29 +++++++++++++++---- frontend/src/app/services/user.service.ts | 2 +- .../delete-user/delete-user.component.ts | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/frontend/src/app/services/user.service.spec.ts b/frontend/src/app/services/user.service.spec.ts index 852129736f..51722cabbc 100644 --- a/frontend/src/app/services/user.service.spec.ts +++ b/frontend/src/app/services/user.service.spec.ts @@ -1,4 +1,4 @@ -import { fakeAsync, getTestBed, TestBed, tick } from '@angular/core/testing'; +import { fakeAsync, TestBed, tick } from '@angular/core/testing'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; import { UserService } from './user.service'; import { testUser, users } from '../shared/testData'; @@ -6,15 +6,12 @@ import { testUser, users } from '../shared/testData'; describe('UserService', () => { let service: UserService; let httpMock: HttpTestingController; - const URL = 'api/v1/users'; - beforeEach(() => { TestBed.configureTestingModule({ imports: [HttpClientTestingModule], }); service = TestBed.inject(UserService); - const injector = getTestBed(); - httpMock = injector.get(HttpTestingController); + httpMock = TestBed.inject(HttpTestingController); }); afterEach(() => { @@ -29,7 +26,7 @@ describe('UserService', () => { const spy = jest.spyOn(service, 'reloadUsers'); service.getUsers().subscribe(() => { expect(spy).toBeCalledTimes(1); - httpMock.expectOne(URL); + httpMock.expectOne('api/v1/users'); service.getUsers().subscribe((users) => { expect(spy).toBeCalledTimes(1); expect(users).toStrictEqual([]); @@ -75,4 +72,24 @@ describe('UserService', () => { const req2 = httpMock.expectOne(`api/v1/users`); req2.flush({}); })); + + it('getUserOkrData() should call userokrdata', fakeAsync(() => { + service.getUserOkrData(testUser).subscribe(); + const req = httpMock.expectOne(`api/v1/users/${testUser.id}/userokrdata`); + expect(req.request.method).toBe('GET'); + req.flush(testUser); + })); + + it('deleteUser() should call deleteUser and reloadUsers', fakeAsync(() => { + service.deleteUser(testUser).subscribe(); + const reqDeleteUser = httpMock.expectOne(`api/v1/users/${testUser.id}`); + expect(reqDeleteUser.request.method).toBe('DELETE'); + reqDeleteUser.flush(testUser); + + tick(); + + const reqReloadUsers = httpMock.expectOne(`api/v1/users`); + expect(reqReloadUsers.request.method).toBe('GET'); + reqReloadUsers.flush({}); + })); }); diff --git a/frontend/src/app/services/user.service.ts b/frontend/src/app/services/user.service.ts index 776955a3d7..4280b6db80 100644 --- a/frontend/src/app/services/user.service.ts +++ b/frontend/src/app/services/user.service.ts @@ -64,7 +64,7 @@ export class UserService { return this.httpClient.post(`${this.API_URL}/createall`, userList).pipe(tap(() => this.reloadUsers())); } - userOkrData(user: User): Observable { + getUserOkrData(user: User): Observable { return this.httpClient.get(`${this.API_URL}/${user.id}/userokrdata`, {}); } diff --git a/frontend/src/app/team-management/delete-user/delete-user.component.ts b/frontend/src/app/team-management/delete-user/delete-user.component.ts index c0036b2bf1..827eb23d4c 100644 --- a/frontend/src/app/team-management/delete-user/delete-user.component.ts +++ b/frontend/src/app/team-management/delete-user/delete-user.component.ts @@ -26,7 +26,7 @@ export class DeleteUserComponent implements OnInit { ) {} ngOnInit() { - this.userService.userOkrData(this.user).subscribe((okrData) => { + this.userService.getUserOkrData(this.user).subscribe((okrData) => { this.userOkrData = okrData; }); }