frontend-toegevoegd
This commit is contained in:
69
agiledancers-fe/src/app/core/school-api.spec.ts
Normal file
69
agiledancers-fe/src/app/core/school-api.spec.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { SchoolApi } from './school-api';
|
||||
|
||||
describe('SchoolApi contract', () => {
|
||||
let api: SchoolApi;
|
||||
let http: HttpTestingController;
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [provideHttpClient(), provideHttpClientTesting()],
|
||||
});
|
||||
api = TestBed.inject(SchoolApi);
|
||||
http = TestBed.inject(HttpTestingController);
|
||||
});
|
||||
afterEach(() => http.verify());
|
||||
|
||||
it('loads all resources from the same school', () => {
|
||||
const received = vi.fn();
|
||||
api.schoolData(7).subscribe(received);
|
||||
const school = { id: 7, name: 'Studio', dancerIds: [], danceGroupIds: [], showIds: [] };
|
||||
http.expectOne('/api/dance-schools/7').flush(school);
|
||||
http.expectOne('/api/dance-schools/7/dancers').flush([]);
|
||||
http.expectOne('/api/dance-schools/7/dance-groups').flush([]);
|
||||
expect(received).not.toHaveBeenCalled();
|
||||
http.expectOne('/api/dance-schools/7/shows').flush([]);
|
||||
expect(received).toHaveBeenCalledWith({ school, dancers: [], groups: [], shows: [] });
|
||||
});
|
||||
|
||||
it('updates an existing entity even when its id is zero', () => {
|
||||
api.saveSchool({ name: 'Renamed' }, 0).subscribe();
|
||||
const request = http.expectOne('/api/dance-schools/0');
|
||||
expect(request.request.method).toBe('PUT');
|
||||
expect(request.request.body).toEqual({ name: 'Renamed' });
|
||||
request.flush({ id: 0, name: 'Renamed', dancerIds: [], danceGroupIds: [], showIds: [] });
|
||||
});
|
||||
|
||||
it('keeps group details separate from memberships', () => {
|
||||
api
|
||||
.saveGroup(
|
||||
7,
|
||||
{ category: 'Teens', type: 'REGULAR', startAge: 12, endAge: 18, styles: ['HIPHOP'] },
|
||||
0,
|
||||
)
|
||||
.subscribe();
|
||||
const request = http.expectOne('/api/dance-schools/7/dance-groups/0');
|
||||
expect(request.request.method).toBe('PUT');
|
||||
expect(request.request.body).not.toHaveProperty('dancerIds');
|
||||
request.flush({});
|
||||
api.membership(7, 0, 3, true).subscribe();
|
||||
const add = http.expectOne('/api/dance-schools/7/dance-groups/0/dancers/3');
|
||||
expect(add.request.method).toBe('PUT');
|
||||
expect(add.request.body).toBeNull();
|
||||
add.flush(null);
|
||||
api.membership(7, 0, 3, false).subscribe();
|
||||
const remove = http.expectOne('/api/dance-schools/7/dance-groups/0/dancers/3');
|
||||
expect(remove.request.method).toBe('DELETE');
|
||||
remove.flush(null);
|
||||
});
|
||||
|
||||
it('propagates server errors instead of pretending a save succeeded', () => {
|
||||
const failure = vi.fn();
|
||||
api.saveDancer(7, { name: 'Sam', birthDate: '2010-05-12' }).subscribe({ error: failure });
|
||||
http
|
||||
.expectOne('/api/dance-schools/7/dancers')
|
||||
.flush({ message: 'Invalid data' }, { status: 400, statusText: 'Bad Request' });
|
||||
expect(failure).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user