Skip to content

Commit

Permalink
feat: add tests for signUp service
Browse files Browse the repository at this point in the history
  • Loading branch information
ki8vi committed Aug 16, 2024
1 parent 73a4449 commit fb4c737
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
55 changes: 53 additions & 2 deletions src/app/api/signUpService/sign-up.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,67 @@
import { TestBed } from '@angular/core/testing';

import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { SignUpService } from './sign-up.service';
import { User } from '../models/user';
import { OverriddenHttpErrorResponse } from '../models/errorResponse';

describe('SignUpService', () => {
let service: SignUpService;
let httpTestingController: HttpTestingController;

beforeEach(() => {
TestBed.configureTestingModule({});
TestBed.configureTestingModule({
providers: [
provideHttpClient(),
provideHttpClientTesting(),
],
});
service = TestBed.inject(SignUpService);
httpTestingController = TestBed.inject(HttpTestingController);
});

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

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

it('service should correctly send request', (done) => {
const userData: User = { email: '[email protected]', password: 'Abcdefghigk' };
service.signUp(userData).subscribe((res) => {
expect(res).toEqual({});
done()
});

const req = httpTestingController.expectOne('/api/signup');
expect(req.request.method).toBe('POST');
expect(req.request.body).toEqual(userData);
req.flush({});
});

it('should correctly handle error if user already exist', (done) => {
const userData: User = { email: '[email protected]', password: 'Passwordtest' };

service.signUp(userData).subscribe({
next: () => fail('expected error from server'),
error: (err: OverriddenHttpErrorResponse) => {
expect(err.status).toBe(400);
expect(err.error.message).toBe('User already exists');
expect(err.error.reason).toBe('invalidUniqueKey');
expect(err.ok).toBe(false);
done();
}
});

const req = httpTestingController.expectOne('/api/signup');
expect(req.request.method).toBe('POST');
expect(req.request.body).toEqual(userData);

req.flush({
message: 'User already exists',
reason: 'invalidUniqueKey'
}, { status: 400, statusText: 'Bad Request' });
});
});
6 changes: 3 additions & 3 deletions src/app/api/signUpService/sign-up.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Observable } from 'rxjs';
import { User } from '../models/user';

@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class SignUpService {
private httpClient = inject(HttpClient);
Expand All @@ -16,9 +16,9 @@ export class SignUpService {

/* Example of using in component
this.signUpService.signUp({ email: 'test@test.com', password: 'Test-password' }).subscribe({
next: () => navigateByUrl('/login'),
next: () => navigateByUrl('/login'), -> successfull registered
error: (err: OverriddenHttpErrorResponse) => {
console.error(err.error.message);
console.error(err.error.message); -> errors
},
});
*/

0 comments on commit fb4c737

Please sign in to comment.