Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): add user service test #47

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 123 additions & 3 deletions src/app/user/services/user/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,140 @@
import { TestBed } from '@angular/core/testing';

import { UserService } from './user.service';
import {
HttpTestingController,
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { LoadingService } from '../../../shared/services/loading.service';
import { ForgetPasswordRequest, NewPasswordRequest } from '../../models/User';
import { UserInformation } from '../../models/ManageUsers';
import { environment } from '../../../../../api-config/api-url';
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';

describe('UserService', () => {
let service: UserService;
let httpMock: HttpTestingController;
let loadingService: jasmine.SpyObj<LoadingService>;
const apiUrl = environment.apiUrl + '/api/User';

beforeEach(() => {
const loadingSpy = jasmine.createSpyObj('LoadingService', ['setLoading']);

TestBed.configureTestingModule({
providers: [provideHttpClient(), provideHttpClientTesting()],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
UserService,
{ provide: LoadingService, useValue: loadingSpy },
],
});

service = TestBed.inject(UserService);
httpMock = TestBed.inject(HttpTestingController);
loadingService = TestBed.inject(
LoadingService,
) as jasmine.SpyObj<LoadingService>;
});

afterEach(() => {
httpMock.verify();
});

it('should be created', () => {
expect(service).toBeTruthy();
});

it('should send request-reset-password request', () => {
const email = '[email protected]';

service.requestResetPassword(email).subscribe();

const req = httpMock.expectOne(`${apiUrl}/request-reset-password`);
expect(req.request.method).toBe('POST');
expect(req.request.body).toEqual({ email });
expect(loadingService.setLoading).toHaveBeenCalledWith(true);
req.flush({});
});

it('should send reset-password request', () => {
const resetRequest: ForgetPasswordRequest = {
email: '[email protected]',
resetPasswordToken: '123456',
newPassword: 'newpassword123',
confirmPassword: 'newpassword123',
};

service.resetPassword(resetRequest).subscribe();

const req = httpMock.expectOne(`${apiUrl}/reset-password`);
expect(req.request.method).toBe('POST');
expect(req.request.body).toEqual(resetRequest);
expect(loadingService.setLoading).toHaveBeenCalledWith(true);
req.flush({});
});

it('should send new-password request', () => {
const newPasswordRequest: NewPasswordRequest = {
oldPassword: 'oldpassword123',
newPassword: 'newpassword123',
confirmPassword: 'newpassword123',
};

service.newPassword(newPasswordRequest).subscribe();

const req = httpMock.expectOne(`${apiUrl}/new-password`);
expect(req.request.method).toBe('POST');
expect(req.request.body).toEqual(newPasswordRequest);
expect(loadingService.setLoading).toHaveBeenCalledWith(true);
req.flush({});
});

it('should get login user information', () => {
const mockUserInfo: UserInformation = {
firstName: 'string',
lastName: 'string',
email: '[email protected]',
phoneNumber: '09121112233',
image: 'string.jpg',
};

service.getLoginUserInfo().subscribe((userInfo) => {
expect(userInfo).toEqual(mockUserInfo);
});

const req = httpMock.expectOne(`${apiUrl}/get-user-information`);
expect(req.request.method).toBe('GET');
expect(loadingService.setLoading).toHaveBeenCalledWith(true);
req.flush(mockUserInfo);
});

it('should send update-user request', () => {
const userInfo: UserInformation = {
firstName: 'string',
lastName: 'string',
email: '[email protected]',
phoneNumber: '09121112233',
image: 'string.jpg',
};

service.updateUser(userInfo).subscribe();

const req = httpMock.expectOne(`${apiUrl}/update-user`);
expect(req.request.method).toBe('PUT');
expect(req.request.body).toEqual(userInfo);
expect(loadingService.setLoading).toHaveBeenCalledWith(true);
req.flush({});
});

it('should upload image', () => {
const file = new File([''], 'filename.jpg');

service.uploadImage(file).subscribe();

const req = httpMock.expectOne(`${apiUrl}/upload-image`);
expect(req.request.method).toBe('POST');
expect(req.request.body instanceof FormData).toBeTrue();
expect(req.request.withCredentials).toBeTrue();
expect(loadingService.setLoading).toHaveBeenCalledWith(true);
req.flush({});
});
});