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(tu-01-25): implement sign up api #38

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"ng-zorro-antd": "^18.1.0",
"rxjs": "^7.8.1",
"ts-node": "^10.9.2",
"tslib": "^2.3.0"
"tslib": "^2.3.0",
"@planess/train-a-backend": "^0.0.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "^18.1.4",
Expand Down
10 changes: 10 additions & 0 deletions src/app/api/models/errorResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { HttpErrorResponse } from '@angular/common/http';

interface Error {
message: string;
reason: string;
}

export interface OverriddenHttpErrorResponse extends HttpErrorResponse {
error: Error;
}
4 changes: 4 additions & 0 deletions src/app/api/models/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface User {
email: string;
password: string;
}
68 changes: 68 additions & 0 deletions src/app/api/signUpService/sign-up.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';

import { OverriddenHttpErrorResponse } from '../models/errorResponse';
import { User } from '../models/user';
import { SignUpService } from './sign-up.service';

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

beforeEach(() => {
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' },
);
});
});
26 changes: 26 additions & 0 deletions src/app/api/signUpService/sign-up.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';

import { Observable } from 'rxjs';

import { User } from '../models/user';

@Injectable({
providedIn: 'root',
})
export class SignUpService {
private httpClient = inject(HttpClient);

public signUp(userData: User): Observable<object> {
return this.httpClient.post('/api/signup', userData);
}
}

/* Example of using in component
this.signUpService.signUp({ email: '[email protected]', password: 'Test-password' }).subscribe({
next: () => navigateByUrl('/login'), -> successfull registered
error: (err: OverriddenHttpErrorResponse) => {
console.error(err.error.message); -> errors
},
});
*/
2 changes: 2 additions & 0 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { provideHttpClient } from '@angular/common/http';
import { ApplicationConfig, provideExperimentalZonelessChangeDetection } from '@angular/core';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
import { provideRouter, withComponentInputBinding, withInMemoryScrolling, withViewTransitions } from '@angular/router';
Expand All @@ -14,6 +15,7 @@ const appConfig: ApplicationConfig = {
withInMemoryScrolling({ scrollPositionRestoration: 'enabled' }),
),
provideAnimationsAsync(),
provideHttpClient(),
],
};
export default appConfig;
10 changes: 7 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { bootstrapApplication } from '@angular/platform-browser';

import { startServer } from '@planess/train-a-backend';

import { AppComponent } from './app/app.component';
import appConfig from './app/app.config';

bootstrapApplication(AppComponent, appConfig).catch((err) => {
throw new Error(String(err));
});
startServer()
.then(() => bootstrapApplication(AppComponent, appConfig))
.catch((err) => {
throw new Error(String(err));
});