feat: use new invite status endpoint to display different invite type counts
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
|
||||
import { setActivePinia, createPinia } from 'pinia'
|
||||
import { getInvitesApi, createInviteApi, deleteInviteApi } from '../invites'
|
||||
import { getInvitesApi, getInviteStatusApi, createInviteApi, deleteInviteApi } from '../invites'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { API_BASE_URL } from '@/api/http'
|
||||
|
||||
@@ -65,6 +65,33 @@ describe('invites API', () => {
|
||||
await expect(getInvitesApi()).rejects.toThrow('Failed')
|
||||
})
|
||||
|
||||
it('getInviteStatusApi sends GET to /user/invites/status and returns the counts', async () => {
|
||||
const mockCounts = { active: 12, expired: 2, used: 8 }
|
||||
const fetchMock = vi.fn<typeof fetch>().mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => mockCounts,
|
||||
} as unknown as Response)
|
||||
global.fetch = fetchMock
|
||||
|
||||
const result = await getInviteStatusApi()
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
`${API_BASE_URL}/user/invites/status`,
|
||||
expect.objectContaining({ method: 'GET' }),
|
||||
)
|
||||
expect(result).toEqual(mockCounts)
|
||||
})
|
||||
|
||||
it('getInviteStatusApi throws on failure', async () => {
|
||||
global.fetch = vi.fn<typeof fetch>().mockResolvedValueOnce({
|
||||
ok: false,
|
||||
json: async () => ({ message: 'Failed to load invite counts' }),
|
||||
} as unknown as Response)
|
||||
|
||||
await expect(getInviteStatusApi()).rejects.toThrow('Failed to load invite counts')
|
||||
})
|
||||
|
||||
it('createInviteApi sends POST to /user/invites and returns the created invite', async () => {
|
||||
const mockInvite = { id: 'invite-1', code: 'ABC123' }
|
||||
const fetchMock = vi.fn<typeof fetch>().mockResolvedValueOnce({
|
||||
|
||||
+9
-1
@@ -1,6 +1,6 @@
|
||||
import { apiClient } from '@/api/client'
|
||||
import { extractErrorMessage } from '@/api/http'
|
||||
import type { Invite, PaginatedInvites } from '@/types/invite'
|
||||
import type { Invite, InviteStatusCounts, PaginatedInvites } from '@/types/invite'
|
||||
|
||||
export interface GetInvitesParams {
|
||||
page?: number
|
||||
@@ -20,6 +20,14 @@ export async function getInvitesApi(params: GetInvitesParams = {}): Promise<Pagi
|
||||
return response.json()
|
||||
}
|
||||
|
||||
export async function getInviteStatusApi(): Promise<InviteStatusCounts> {
|
||||
const response = await apiClient.get('/user/invites/status')
|
||||
if (!response.ok) {
|
||||
throw new Error(await extractErrorMessage(response, 'Failed to load invite counts'))
|
||||
}
|
||||
return response.json()
|
||||
}
|
||||
|
||||
export async function createInviteApi(): Promise<Invite> {
|
||||
const response = await apiClient.post('/user/invites')
|
||||
if (!response.ok) {
|
||||
|
||||
Reference in New Issue
Block a user