From f8ab4b77d412634542e2f94f2b2fcd456d02c8a1 Mon Sep 17 00:00:00 2001 From: DaviMatheus Date: Tue, 27 Aug 2024 11:16:28 -0300 Subject: [PATCH 1/2] [tests]adding_tests --- test/journey.service.spec.ts | 50 +++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/test/journey.service.spec.ts b/test/journey.service.spec.ts index 2f9cb00..7579832 100644 --- a/test/journey.service.spec.ts +++ b/test/journey.service.spec.ts @@ -2,7 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { JourneyService } from '../src/journey/journey.service'; import { getModelToken } from '@nestjs/mongoose'; import { HttpService } from '@nestjs/axios'; -import { Model } from 'mongoose'; +import { Model, Types } from 'mongoose'; import { Journey } from '../src/journey/journey.schema'; import { CreateJourneyDto } from '../src/journey/dtos/create-journey.dto'; import { @@ -152,6 +152,43 @@ describe('JourneyService', () => { expect(result).toBe('userId123'); }); + + + it('should add a trail to the journey and return the updated journey', async () => { + const journeyId = '605c72efc1d6f812a8e90b7a'; // Use um ObjectId válido + const trailId = '605c72efc1d6f812a8e90b7b'; // Use um ObjectId válido + + // Mock da jornada atual + const mockJourneyWithTrail = { + ...mockJourney, + trails: [new Types.ObjectId(trailId)], + }; + + jest.spyOn(model, 'findById').mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(mockJourney), + } as any); + jest + .spyOn(mockJourney, 'save') + .mockResolvedValue(mockJourneyWithTrail as any); + + const result = await service.addTrailToJourney(journeyId, trailId); + + expect(result).toEqual(mockJourneyWithTrail); + expect(model.findById).toHaveBeenCalledWith(journeyId); + expect(mockJourney.save).toHaveBeenCalled(); + }); + + it('should throw NotFoundException if journey is not found', async () => { + const journeyId = 'invalidJourneyId'; + const trailId = 'mockTrailId'; + + jest.spyOn(model, 'findById').mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(null), + } as any); + + await expect(service.addTrailToJourney(journeyId, trailId)).rejects.toThrow(NotFoundException); + }); + it('should return null when token is invalid', async () => { const token = 'invalidToken'; const mockError = new Error('Token invalid'); @@ -164,4 +201,15 @@ describe('JourneyService', () => { expect(result).toBeNull(); }); + + it('should handle error when adding journey to uset', async () => { + const userId = '605c72efc1d6f812a8e90b7a'; + const journeyId = '605c72efc1d6f812a8e90b7b'; + + jest.spyOn(mockLogger, 'error').mockImplementation(() => {}); + + await expect(service.addJourneyToUser(userId, journeyId)).rejects.toThrow(NotFoundException); + + }); + }); From b02b16b990a8ce4fba6559fc56b499f169148fbc Mon Sep 17 00:00:00 2001 From: DaviMatheus Date: Tue, 27 Aug 2024 11:36:00 -0300 Subject: [PATCH 2/2] [tests]adding_tests --- test/journey.service.spec.ts | 18 ++++---- test/trail.service.spec.ts | 89 +++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 10 deletions(-) diff --git a/test/journey.service.spec.ts b/test/journey.service.spec.ts index 7579832..1fd209f 100644 --- a/test/journey.service.spec.ts +++ b/test/journey.service.spec.ts @@ -152,8 +152,6 @@ describe('JourneyService', () => { expect(result).toBe('userId123'); }); - - it('should add a trail to the journey and return the updated journey', async () => { const journeyId = '605c72efc1d6f812a8e90b7a'; // Use um ObjectId válido const trailId = '605c72efc1d6f812a8e90b7b'; // Use um ObjectId válido @@ -186,7 +184,9 @@ describe('JourneyService', () => { exec: jest.fn().mockResolvedValue(null), } as any); - await expect(service.addTrailToJourney(journeyId, trailId)).rejects.toThrow(NotFoundException); + await expect(service.addTrailToJourney(journeyId, trailId)).rejects.toThrow( + NotFoundException, + ); }); it('should return null when token is invalid', async () => { @@ -203,13 +203,13 @@ describe('JourneyService', () => { }); it('should handle error when adding journey to uset', async () => { - const userId = '605c72efc1d6f812a8e90b7a'; - const journeyId = '605c72efc1d6f812a8e90b7b'; + const userId = '605c72efc1d6f812a8e90b7a'; + const journeyId = '605c72efc1d6f812a8e90b7b'; - jest.spyOn(mockLogger, 'error').mockImplementation(() => {}); + jest.spyOn(mockLogger, 'error').mockImplementation(() => {}); - await expect(service.addJourneyToUser(userId, journeyId)).rejects.toThrow(NotFoundException); - + await expect(service.addJourneyToUser(userId, journeyId)).rejects.toThrow( + NotFoundException, + ); }); - }); diff --git a/test/trail.service.spec.ts b/test/trail.service.spec.ts index 331acd3..fd06f33 100644 --- a/test/trail.service.spec.ts +++ b/test/trail.service.spec.ts @@ -1,6 +1,6 @@ import { Test, TestingModule } from '@nestjs/testing'; import { getModelToken } from '@nestjs/mongoose'; -import { Model } from 'mongoose'; +import { Model, Types } from 'mongoose'; import { NotFoundException } from '@nestjs/common'; import { JourneyService } from 'src/journey/journey.service'; import { Journey } from 'src/journey/journey.schema'; @@ -153,4 +153,91 @@ describe('TrailService', () => { NotFoundException, ); }); + + it('should handle error when adding content to trail', async () => { + const mockContentId = '605c72efc1d6f812a8e90b7c'; + const mockTrailWithContents = { + ...mockTrail, + contents: [new Types.ObjectId(mockContentId)], + }; + + jest.spyOn(trailModel, 'findById').mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(mockTrailWithContents), + } as any); + + const saveMock = jest.fn().mockResolvedValue(mockTrailWithContents); + jest.spyOn(mockTrailWithContents, 'save').mockImplementation(saveMock); + + await expect( + service.addContentToTrail('mockTrailId', mockContentId), + ).resolves.toEqual(mockTrailWithContents); + expect(trailModel.findById).toHaveBeenCalledWith('mockTrailId'); + expect(saveMock).toHaveBeenCalled(); + }); + + it('should handle error when removing content from trail', async () => { + const mockContentId = '605c72efc1d6f812a8e90b7c'; + const mockTrailWithContents = { + ...mockTrail, + contents: [new Types.ObjectId(mockContentId)], + }; + + jest.spyOn(trailModel, 'findById').mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(mockTrailWithContents), + } as any); + + const saveMock = jest.fn().mockResolvedValue(mockTrailWithContents); + jest.spyOn(mockTrailWithContents, 'save').mockImplementation(saveMock); + + await expect( + service.removeContentFromTrail('mockTrailId', mockContentId), + ).resolves.toEqual(mockTrailWithContents); + expect(trailModel.findById).toHaveBeenCalledWith('mockTrailId'); + expect(saveMock).toHaveBeenCalled(); + }); + + it('should throw NotFoundException if journey is not found when finding trails by journey ID', async () => { + jest.spyOn(journeyModel, 'findById').mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(null), + } as any); + + await expect( + service.findTrailsByJourneyId('invalidJourneyId'), + ).rejects.toThrow(NotFoundException); + }); + + it('should return trails for a valid journey ID', async () => { + jest.spyOn(trailModel, 'find').mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(mockTrail), + } as any); + + const result = await service.findTrailsByJourneyId('mockJourneyId'); + expect(result).toEqual(mockTrail); + expect(trailModel.find).toHaveBeenCalledWith({ journey: 'mockJourneyId' }); + }); + + it('should throw NotFoundException if trail is not found when updating', async () => { + jest.spyOn(trailModel, 'findByIdAndUpdate').mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(null), + } as any); + + await expect( + service.updateTrail('invalidTrailId', { name: 'Updated Trail' }), + ).rejects.toThrow(NotFoundException); + }); + + it('should update a trail and return the updated trail', async () => { + const updateData = { name: 'Updated Trail' }; + jest.spyOn(trailModel, 'findByIdAndUpdate').mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(mockTrail), + } as any); + + const result = await service.updateTrail('mockTrailId', updateData); + expect(result).toEqual(mockTrail); + expect(trailModel.findByIdAndUpdate).toHaveBeenCalledWith( + 'mockTrailId', + updateData, + { new: true }, + ); + }); });