Skip to content

Commit

Permalink
#931: additional test for getUserOkrData() and deleteUser() in UserSe…
Browse files Browse the repository at this point in the history
…rvice
  • Loading branch information
clean-coder committed Jul 31, 2024
1 parent 3436d1d commit fc92272
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
29 changes: 23 additions & 6 deletions frontend/src/app/services/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
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';

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(() => {
Expand All @@ -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([]);
Expand Down Expand Up @@ -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({});
}));
});
2 changes: 1 addition & 1 deletion frontend/src/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class UserService {
return this.httpClient.post<User>(`${this.API_URL}/createall`, userList).pipe(tap(() => this.reloadUsers()));
}

userOkrData(user: User): Observable<UserOkrData> {
getUserOkrData(user: User): Observable<UserOkrData> {
return this.httpClient.get<UserOkrData>(`${this.API_URL}/${user.id}/userokrdata`, {});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down

0 comments on commit fc92272

Please sign in to comment.