SmartStopAPK/src/infraestructure/api/clients/__tests__/AuthAPI.spec.ts

60 lines
1.4 KiB
TypeScript
Raw Normal View History

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',
});
expect(token).toMatchObject({token: 'token'});
});
it('should throw an error if username is not provided', async () => {
await expect(
authAPI.auth({
password: 'password',
username: '',
}),
).rejects.toThrow('username is required');
});
it('should throw an error if password is not provided', async () => {
await expect(
authAPI.auth({
username: 'username',
password: '',
}),
).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',
}),
).rejects.toThrow('Request failed with status code 400');
});
});