54d1534d4d
Signed-off-by: Gato <cedric@goutailler-olivier.fr>
82 lines
2.8 KiB
TypeScript
82 lines
2.8 KiB
TypeScript
import { provideHttpClient } from '@angular/common/http';
|
|
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
|
|
import { TestBed } from '@angular/core/testing';
|
|
import { ProjectsApiService } from './projects-api.service';
|
|
import { ProjectEntity } from './projects.store';
|
|
|
|
const API = '/api';
|
|
|
|
const makeProject = (overrides: Partial<ProjectEntity> = {}): ProjectEntity => ({
|
|
id: 1,
|
|
name: 'Mon Projet',
|
|
owner: 'Alice',
|
|
status: 'Actif',
|
|
progress: 50,
|
|
...overrides,
|
|
});
|
|
|
|
describe('ProjectsApiService', () => {
|
|
let service: ProjectsApiService;
|
|
let http: HttpTestingController;
|
|
|
|
beforeEach(() => {
|
|
TestBed.configureTestingModule({
|
|
providers: [provideHttpClient(), provideHttpClientTesting()],
|
|
});
|
|
service = TestBed.inject(ProjectsApiService);
|
|
http = TestBed.inject(HttpTestingController);
|
|
});
|
|
|
|
afterEach(() => http.verify());
|
|
|
|
describe('getAll', () => {
|
|
it('sends GET /api/projects and returns projects', () => {
|
|
const projects = [makeProject({ id: 1 }), makeProject({ id: 2 })];
|
|
let result: ProjectEntity[] | undefined;
|
|
service.getAll().subscribe((data) => (result = data));
|
|
const req = http.expectOne(`${API}/projects`);
|
|
expect(req.request.method).toBe('GET');
|
|
req.flush(projects);
|
|
expect(result).toEqual(projects);
|
|
});
|
|
});
|
|
|
|
describe('create', () => {
|
|
it('sends POST /api/projects with the body and returns the created project', () => {
|
|
const body = { name: 'Nouveau', owner: 'Bob', status: 'Nouveau' as const, progress: 0 };
|
|
const response = makeProject({ id: 10, name: 'Nouveau' });
|
|
let result: ProjectEntity | undefined;
|
|
service.create(body).subscribe((data) => (result = data));
|
|
const req = http.expectOne(`${API}/projects`);
|
|
expect(req.request.method).toBe('POST');
|
|
expect(req.request.body).toEqual(body);
|
|
req.flush(response);
|
|
expect(result).toEqual(response);
|
|
});
|
|
});
|
|
|
|
describe('update', () => {
|
|
it('sends PUT /api/projects/:id and returns the updated project', () => {
|
|
const project = makeProject({ id: 1, name: 'Updated' });
|
|
let result: ProjectEntity | undefined;
|
|
service.update(1, project).subscribe((data) => (result = data));
|
|
const req = http.expectOne(`${API}/projects/1`);
|
|
expect(req.request.method).toBe('PUT');
|
|
expect(req.request.body).toEqual(project);
|
|
req.flush(project);
|
|
expect(result).toEqual(project);
|
|
});
|
|
});
|
|
|
|
describe('remove', () => {
|
|
it('sends DELETE /api/projects/:id and completes', () => {
|
|
let completed = false;
|
|
service.remove(1).subscribe({ complete: () => (completed = true) });
|
|
const req = http.expectOne(`${API}/projects/1`);
|
|
expect(req.request.method).toBe('DELETE');
|
|
req.flush(null);
|
|
expect(completed).toBe(true);
|
|
});
|
|
});
|
|
});
|