Skip to content

Commit

Permalink
[tests]adding_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviMatheus committed Aug 27, 2024
1 parent f8ab4b7 commit b02b16b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
18 changes: 9 additions & 9 deletions test/journey.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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,
);
});

});
89 changes: 88 additions & 1 deletion test/trail.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 },
);
});
});

0 comments on commit b02b16b

Please sign in to comment.