Add unit test
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||||
|
import login from './login';
|
||||||
|
|
||||||
|
vi.mock('../getXSRF.js', () => ({
|
||||||
|
default: vi.fn(() => Promise.resolve('fake-csrf-token'))
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('login()', () => {
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
global.fetch = vi.fn();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('envoie la requête de login et retourne les données utilisateur', async () => {
|
||||||
|
global.fetch.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
json: async () => ({ id: 1, name: 'John Doe' })
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await login('test@example.com', 'secret');
|
||||||
|
|
||||||
|
expect(global.fetch).toHaveBeenCalledOnce();
|
||||||
|
expect(global.fetch).toHaveBeenCalledWith(
|
||||||
|
`http://${import.meta.env.VITE_API_URL}/api/users/login`,
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-XSRF-TOKEN': 'fake-csrf-token',
|
||||||
|
'Accept': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ email: 'test@example.com', password: 'secret' })
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
status: 200,
|
||||||
|
data: { id: 1, name: 'John Doe' }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('retourne une erreur quand la réponse HTTP est mauvaise', async () => {
|
||||||
|
global.fetch.mockResolvedValue({
|
||||||
|
ok: false,
|
||||||
|
status: 401
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await login('x', 'y');
|
||||||
|
|
||||||
|
expect(result).toBeInstanceOf(Error);
|
||||||
|
expect(result.message).toBe('Erreur HTTP 401');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||||
|
import logout from './logout';
|
||||||
|
|
||||||
|
vi.mock('../getXSRF.js', () => {
|
||||||
|
return {
|
||||||
|
default: vi.fn(() => Promise.resolve('fake-csrf-token'))
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('logout()', () => {
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
global.fetch = vi.fn();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('envoie bien la requête de logout', async () => {
|
||||||
|
global.fetch.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
status: 200
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await logout();
|
||||||
|
|
||||||
|
expect(global.fetch).toHaveBeenCalledOnce();
|
||||||
|
expect(global.fetch).toHaveBeenCalledWith(
|
||||||
|
`http://${import.meta.env.VITE_API_URL}/api/users/logout`,
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'X-XSRF-TOKEN': 'fake-csrf-token'
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual({ status: 200 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renvoie une erreur quand la réponse HTTP est mauvaise', async () => {
|
||||||
|
global.fetch.mockResolvedValue({
|
||||||
|
ok: false,
|
||||||
|
status: 500
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await logout();
|
||||||
|
|
||||||
|
expect(result).toBeInstanceOf(Error);
|
||||||
|
expect(result.message).toBe('Erreur HTTP 500');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user