Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #7

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion test/journey.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
Expand All @@ -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,
);
});
});
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 },
);
});
});
Loading