import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import AuthAPI from '../AuthAPI'; const mockAdapter = new MockAdapter(axios); const BASE_URL = 'https://test.cl'; const authAPI = new AuthAPI(BASE_URL); describe('AuthAPI tests', () => { it('should be defined', () => { expect(AuthAPI).toBeDefined(); }); it('should return a token', async () => { mockAdapter.onPost(`${BASE_URL}/api/auth/`).reply(200, { token: 'token', }); const token = await authAPI.auth({ username: 'username', password: 'password', rut:'rut', }); expect(token).toMatchObject({token: 'token'}); }); it('should throw an error if username is not provided', async () => { await expect( authAPI.auth({ password: 'password', username: '', rut:'rut', }), ).rejects.toThrow('username is required'); }); it('should throw an error if password is not provided', async () => { await expect( authAPI.auth({ username: 'username', password: '', rut:'rut', }), ).rejects.toThrow('password is required'); }); it('should return an error if it fails to request a token', async () => { mockAdapter.onPost(`${BASE_URL}/api/auth/`).reply(400, { error: 'error', }); await expect( authAPI.auth({ username: 'username', password: 'password', rut:'rut', }), ).rejects.toThrow('Request failed with status code 400'); }); });