Spaces:
Running
Running
File size: 2,276 Bytes
b5d28fa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import { describe, it, expect, vi, beforeEach } from 'vitest';
import { authAPI, fetcher } from './api';
import api from './client';
vi.mock('./client', () => {
return {
default: {
get: vi.fn(),
post: vi.fn(),
patch: vi.fn(),
delete: vi.fn(),
}
};
});
describe('API Wrappers', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('fetcher', () => {
it('should call api.get and return data', async () => {
const mockData = { message: 'success' };
api.get.mockResolvedValueOnce({ data: mockData });
const result = await fetcher('/test-url');
expect(api.get).toHaveBeenCalledWith('/test-url');
expect(result).toEqual(mockData);
});
});
describe('authAPI', () => {
it('register should call POST /auth/register', async () => {
api.post.mockResolvedValueOnce({ data: 'registered' });
const userData = { email: 'test@example.com', password: 'password123' };
await authAPI.register(userData);
expect(api.post).toHaveBeenCalledWith('/auth/register', userData);
});
it('login should call POST /auth/login', async () => {
api.post.mockResolvedValueOnce({ data: 'logged_in' });
const credentials = { email: 'test@example.com', password: 'password123' };
await authAPI.login(credentials);
expect(api.post).toHaveBeenCalledWith('/auth/login', credentials);
});
it('resetPassword should call POST /auth/reset-password', async () => {
api.post.mockResolvedValueOnce({ data: 'reset_sent' });
const resetData = { email: 'test@example.com' };
await authAPI.resetPassword(resetData);
expect(api.post).toHaveBeenCalledWith('/auth/reset-password', resetData);
});
it('logout should call POST /auth/logout', async () => {
api.post.mockResolvedValueOnce({ data: 'logged_out' });
await authAPI.logout();
expect(api.post).toHaveBeenCalledWith('/auth/logout');
});
it('me should call GET /auth/me', async () => {
api.get.mockResolvedValueOnce({ data: { id: 1, name: 'Test User' } });
await authAPI.me();
expect(api.get).toHaveBeenCalledWith('/auth/me');
});
});
});
|