Skip to content

Commit

Permalink
fix: tests@
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseAlbDR committed Apr 3, 2024
1 parent 16d1e09 commit 738f435
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 189 deletions.
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PORT=3002
DB_PASSWORD=1234
DB_NAME=AUP-db
DB_HOST=localhost
DB_PORT=5432
DB_PORT=5433
DB_USERNAME=postgres

JWT_SEED=secret
Expand Down
2 changes: 1 addition & 1 deletion src/presentation/middlewares/error-handler.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ErrorHandlerMiddleware {
message = 'Unknown error';
}

if (err.name && err.name === 'TokenExpiredError') {
if (err?.name && err?.name === 'TokenExpiredError') {
statusCode = 401;
message = 'JWT token expired';
}
Expand Down
18 changes: 15 additions & 3 deletions src/test/domain/entities/animals.entity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ describe('animals.entity.ts', () => {
shelter: {
cif: '',
description: '',
facilities: 'foster_homes',
facilities: ['foster_homes'],
id: '1',
images: [],
legalForms: 'association',
ownVet: false,
veterinaryFacilities: false,
user: {
avatar: 'avatar.jpg',
avatar: ['avatar.jpg'],
username: 'shelter1',
isOnline: true,
},
Expand All @@ -57,17 +57,21 @@ describe('animals.entity.ts', () => {
slug: 'max',
name: 'Max',
age: 3,
description: 'A friendly dog',
gender: 'male',
size: 'medium',
type: 'dog',
numFavs: 0,
city: 'New York',
publishStatus: 'published',
status: 'available',
images: ['image1.jpg', 'image2.jpg'],
shelter: {
avatar: 'avatar.jpg',
avatar: ['avatar.jpg'],
username: 'shelter1',
isOnline: true,
},
userFavs: undefined,
};

const animalEntity = AnimalEntity.fromObject(animalResponse);
Expand Down Expand Up @@ -161,6 +165,10 @@ describe('animals.entity.ts', () => {
slug: 'max',
name: 'Max',
age: 3,
description: 'A friendly dog',
publishStatus: 'published',
status: 'available',
userFavs: undefined,
gender: 'male',
numFavs: 0,
size: 'large',
Expand All @@ -177,13 +185,17 @@ describe('animals.entity.ts', () => {
id: '2',
slug: 'lucy',
name: 'Lucy',
publishStatus: 'published',
age: 2,
gender: 'female',
size: 'small',
description: 'A playful cat',
numFavs: 0,
type: 'cat',
city: 'Los Angeles',
images: ['image3.jpg', 'image4.jpg'],
status: 'available',
userFavs: undefined,
shelter: {
avatar: 'avatar2.jpg',
username: 'shelter2',
Expand Down
24 changes: 9 additions & 15 deletions src/test/domain/entities/user.entity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ describe('user.entity.ts', () => {
createdAt: new Date(),
updatedAt: new Date(),
verifiedAt: null,
avatar: 'avatar.jpg',
avatar: ['avatar.jpg'],
isOnline: true,
shelter: null,
admin: null,
contactInfo: {
id: '1',
phoneNumber: '1234567890',
address: '123 Main St',
cityId: 1,
city: {
id: 1,
Expand All @@ -47,10 +46,9 @@ describe('user.entity.ts', () => {
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
verifiedAt: null,
avatar: 'avatar.jpg',
avatar: ['avatar.jpg'],
isOnline: true,
phoneNumber: '1234567890',
address: '123 Main St',
city: 'New York',
description: undefined,
cif: undefined,
Expand Down Expand Up @@ -83,15 +81,15 @@ describe('user.entity.ts', () => {
createdAt: new Date(),
updatedAt: new Date(),
verifiedAt: null,
avatar: 'avatar.jpg',
avatar: ['avatar.jpg'],
isOnline: true,
shelter: {
id: '1',
description: 'Shelter description',
cif: '12345678B',
legalForms: 'association',
veterinaryFacilities: true,
facilities: 'foster_homes',
facilities: ['foster_homes'],
ownVet: true,
images: ['image1.jpg', 'image2.jpg'],
socialMedia: [
Expand All @@ -103,7 +101,6 @@ describe('user.entity.ts', () => {
contactInfo: {
id: '1',
phoneNumber: '1234567890',
address: '123 Main St',
cityId: 1,
city: {
id: 1,
Expand All @@ -124,16 +121,15 @@ describe('user.entity.ts', () => {
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
verifiedAt: null,
avatar: 'avatar.jpg',
avatar: ['avatar.jpg'],
isOnline: true,
phoneNumber: '1234567890',
address: '123 Main St',
city: 'New York',
description: 'Shelter description',
cif: '12345678B',
legalForms: 'association',
veterinaryFacilities: true,
facilities: 'foster_homes',
facilities: ['foster_homes'],
ownVet: true,
images: ['image1.jpg', 'image2.jpg'],
socialMedia: [
Expand Down Expand Up @@ -162,15 +158,15 @@ describe('user.entity.ts', () => {
createdAt: new Date(),
updatedAt: new Date(),
verifiedAt: null,
avatar: 'avatar.jpg',
avatar: ['avatar.jpg'],
isOnline: true,
shelter: {
id: '1',
description: 'Shelter description',
cif: '12345678B',
legalForms: 'association',
veterinaryFacilities: true,
facilities: 'foster_homes',
facilities: ['foster_homes'],
ownVet: true,
images: ['image1.jpg', 'image2.jpg'],
socialMedia: [
Expand All @@ -185,7 +181,6 @@ describe('user.entity.ts', () => {
contactInfo: {
id: '1',
phoneNumber: '1234567890',
address: '123 Main St',
cityId: 1,
city: {
id: 1,
Expand All @@ -206,10 +201,9 @@ describe('user.entity.ts', () => {
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
verifiedAt: null,
avatar: 'avatar.jpg',
avatar: ['avatar.jpg'],
isOnline: true,
phoneNumber: '1234567890',
address: '123 Main St',
city: 'New York',
name: 'admin',
};
Expand Down
52 changes: 29 additions & 23 deletions src/test/presentation/animals/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { testServer } from '../../../presentation/test-server';
import { TestUser, cleanDB } from '../auth/routes.test';
import request from 'supertest';
import { CreateAnimalDto } from '../../../domain';
import { gender } from '../../../domain/interfaces';
import { gender, statusPet } from '../../../domain/interfaces';

jest.mock('../../../presentation/services/s3.service.ts');
jest.mock('../../../presentation/shared/services');

describe('Api animals routes testing', () => {
const loginRoute = '/api/auth/login';
Expand All @@ -20,17 +20,11 @@ describe('Api animals routes testing', () => {
email: 'test@test.com',
password: 'testtest',
role: 'shelter',
dni: '111111111',
firstName: 'test',
lastName: 'test',
phoneNumber: '11111111',
address: '13 rue del percebe',
cityId: 7,
};

const animal: CreateAnimalDto = {
type: 'cat',
name: 'Nero',
name: 'nero',
age: 14,
description: 'Cold as Hell',
breed: 'Unknown',
Expand All @@ -39,7 +33,8 @@ describe('Api animals routes testing', () => {
energyLevel: 'light',
moltingAmount: 'light',
gender: gender.M,
cityId: 7,
city: 'Granada',
status: statusPet.A,
};

const cat = {
Expand All @@ -55,7 +50,7 @@ describe('Api animals routes testing', () => {
bark: 'excessive',
};

const { phoneNumber, address, cityId, ...userRest } = user;
const { ...userRest } = user;

beforeAll(async () => {
prisma.$connect();
Expand Down Expand Up @@ -105,16 +100,16 @@ describe('Api animals routes testing', () => {
expect(body).toEqual({
id: expect.any(String),
gender: 'male',
name: 'Nero',
name: 'nero',
type: 'cat',
slug: 'test-nero',
age: 14,
description: 'Cold as Hell',
breed: 'Unknown',
size: 'medium',
numFavs: 0,
publishStatus: 'pending',
status: 'awaiting_home',
publishStatus: 'published',
status: 'adopted',
easyTrain: false,
energyLevel: 'light',
moltingAmount: 'light',
Expand All @@ -123,7 +118,7 @@ describe('Api animals routes testing', () => {
updatedAt: expect.any(String),
adoptedBy: null,
createdBy: expect.any(String),
cityId: 7,
cityId: 4,
});
});
test('Should create a new dog', async () => {
Expand All @@ -146,16 +141,16 @@ describe('Api animals routes testing', () => {
expect(body).toEqual({
id: expect.any(String),
gender: 'male',
name: 'Nero',
name: 'nero',
type: 'dog',
slug: 'test-nero',
age: 14,
description: 'Cold as Hell',
breed: 'Unknown',
size: 'medium',
numFavs: 0,
publishStatus: 'pending',
status: 'awaiting_home',
publishStatus: 'published',
status: 'adopted',
easyTrain: false,
energyLevel: 'light',
moltingAmount: 'light',
Expand All @@ -164,7 +159,7 @@ describe('Api animals routes testing', () => {
updatedAt: expect.any(String),
adoptedBy: null,
createdBy: expect.any(String),
cityId: 7,
cityId: 4,
});
});
});
Expand Down Expand Up @@ -206,6 +201,9 @@ describe('Api animals routes testing', () => {
expect(body).toEqual({
currentPage: 1,
maxPages: 1,
awaitingHome: 0,
fostered: 0,
adopted: 2,
limit: 10,
total: 2,
next: null,
Expand All @@ -214,26 +212,34 @@ describe('Api animals routes testing', () => {
{
id: expect.any(String),
slug: expect.any(String),
name: 'Nero',
name: 'nero',
age: 14,
gender: 'male',
size: 'medium',
description: 'Cold as Hell',
publishStatus: 'published',
status: 'adopted',
userFavs: expect.any(Array),
numFavs: 0,
type: expect.any(String),
city: 'Málaga',
city: 'Granada',
images: expect.any(Array),
shelter: expect.any(Object),
},
{
id: expect.any(String),
slug: expect.any(String),
name: 'Nero',
name: 'nero',
age: 14,
gender: 'male',
size: 'medium',
description: 'Cold as Hell',
publishStatus: 'published',
status: 'adopted',
userFavs: expect.any(Array),
numFavs: 0,
type: expect.any(String),
city: 'Málaga',
city: 'Granada',
images: expect.any(Array),
shelter: expect.any(Object),
},
Expand Down
18 changes: 3 additions & 15 deletions src/test/presentation/auth/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ export interface TestUser {
email: string;
password: string;
username: string;
firstName: string;
lastName: string;
dni: string;
role: UserRoles;
phoneNumber: string;
address: string;
cityId: number;
}

describe('Api auth routes testing', () => {
Expand All @@ -37,15 +31,9 @@ describe('Api auth routes testing', () => {
email: 'test@test.com',
password: 'testtest',
role: 'shelter',
dni: '22222222',
firstName: 'test',
lastName: 'test',
phoneNumber: '2222222',
address: '13 rue del percebe',
cityId: 7,
};

const { phoneNumber, address, cityId, ...rest } = user;
const { ...rest } = user;

beforeAll(async () => {
prisma.$connect();
Expand Down Expand Up @@ -187,10 +175,10 @@ describe('Api auth routes testing', () => {
.expect(200);

expect(response.headers['set-cookie'].at(-2)?.split(';').at(0)).toEqual(
'refreshToken=logout'
'refreshToken=s%3Alogout.eKYOqxJ5ugYbHiqYOYYa1c4bbviCHMDRDkkF042aXzs'
);
expect(response.headers['set-cookie'].at(-1)?.split(';').at(0)).toEqual(
'accessToken=logout'
'accessToken=s%3Alogout.eKYOqxJ5ugYbHiqYOYYa1c4bbviCHMDRDkkF042aXzs'
);
});
});
Expand Down
Loading

0 comments on commit 738f435

Please sign in to comment.