|
|
@ -0,0 +1,33 @@ |
|
|
|
import axiosInstance from '../axios/axiosInstance'; |
|
|
|
import { loginApi } from './loginApi'; |
|
|
|
|
|
|
|
jest.mock('../axios/axiosInstance'); |
|
|
|
|
|
|
|
describe('login API', () => { |
|
|
|
it('should return token on successful login', async () => { |
|
|
|
const mockResponse = { |
|
|
|
data: { |
|
|
|
token: 'fake-token', |
|
|
|
}, |
|
|
|
}; |
|
|
|
(axiosInstance.post as jest.Mock).mockResolvedValue(mockResponse); |
|
|
|
|
|
|
|
const data = { username: 'emilys', password: 'emilyspass' }; |
|
|
|
const result = await loginApi(data); |
|
|
|
|
|
|
|
expect(result).toEqual(mockResponse.data); |
|
|
|
expect(axiosInstance.post).toHaveBeenCalledWith('/auth/login', data); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should throw an error on failed login', async () => { |
|
|
|
const errorMessage = 'Invalid credentials'; |
|
|
|
(axiosInstance.post as jest.Mock).mockRejectedValue( |
|
|
|
new Error(errorMessage) |
|
|
|
); |
|
|
|
|
|
|
|
const data = { username: 'test@example.com', password: 'wrongpassword' }; |
|
|
|
|
|
|
|
await expect(loginApi(data)).rejects.toThrow(errorMessage); |
|
|
|
expect(axiosInstance.post).toHaveBeenCalledWith('/auth/login', data); |
|
|
|
}); |
|
|
|
}); |