diff --git a/src/classrooms/_tests_/classrooms.api.spec.ts b/src/classrooms/_tests_/classrooms.api.spec.ts new file mode 100644 index 00000000..ae868b8c --- /dev/null +++ b/src/classrooms/_tests_/classrooms.api.spec.ts @@ -0,0 +1,38 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { getClassrooms } from '@/classrooms/api/classrooms.api' + +describe('classrooms.api', () => { + afterEach(() => { + vi.unstubAllGlobals() + }) + + it('normalizes owned classrooms that use id instead of classroomId', async () => { + const fetchMock = vi.fn().mockResolvedValue({ + ok: true, + json: async () => [ + { + id: 7, + title: 'Detektivgjengen 6B', + description: '', + pupilCount: 24, + pendingPupilCount: 0, + teacherCount: 2, + taskCount: 5, + }, + ], + }) + vi.stubGlobal('fetch', fetchMock) + + await expect(getClassrooms('Bearer token')).resolves.toEqual([ + { + classroomId: 7, + title: 'Detektivgjengen 6B', + description: '', + pupilCount: 24, + pendingPupilCount: 0, + teacherCount: 2, + taskCount: 5, + }, + ]) + }) +}) diff --git a/src/classrooms/api/classrooms.api.ts b/src/classrooms/api/classrooms.api.ts index 693897a5..b56b3375 100644 --- a/src/classrooms/api/classrooms.api.ts +++ b/src/classrooms/api/classrooms.api.ts @@ -13,7 +13,8 @@ export type CreateClassroomRequest = { } export interface OwnedClassroomResponse { - classroomId: number + classroomId?: number + id?: number description: string title: string pupilCount: number @@ -368,7 +369,7 @@ export async function getClassrooms( ) return classrooms.map((c) => ({ - classroomId: c.classroomId, + classroomId: c.classroomId ?? c.id ?? 0, title: c.title, description: c.description, pupilCount: c.pupilCount,