Skip to content

Commit

Permalink
feat(fga-eps-mds/2024.2-ARANDU-DOC#69): testing sub and unsub to subj…
Browse files Browse the repository at this point in the history
…ect routes and svcs

Co-authored-by: dartmol203 <[email protected]>
Co-authored-by: Yasm1nNasc1mento <[email protected]>
Co-authored-by: isabellachoukaira <[email protected]>
  • Loading branch information
4 people committed Jan 28, 2025
1 parent e0ee256 commit e3b7473
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test/user.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,34 @@ describe('UsersController', () => {
role: UserRole.ALUNO,
}

const mockSubscribedSubject = {
_id: 'mocked-id',
email: 'mocked-email',
name: 'mocked-name',
username: 'mocked-username',
subscribedSubjects: ['mocked-subject']
}

const mockUnsubscribedSubject = {
id: 'mocked-id',
email: 'mocked-email',
name: 'mocked-name',
username: 'mocked-username',
subscribedSubjects: []
}

const mockUserService = {
createUser: jest.fn().mockResolvedValue(mockUser),
verifyUser: jest.fn().mockResolvedValue(mockUser),
updateUser: jest.fn().mockResolvedValue(mockUpdatedUser),
getSubscribedJourneys: jest.fn().mockResolvedValue([]),
getSubscribedSubjects: jest.fn().mockResolvedValue([]),
getUsers: jest.fn().mockResolvedValue([mockUser]),
addSubjectToUser: jest.fn().mockResolvedValue(mockUser),
subscribeJourney: jest.fn().mockResolvedValue(mockUser),
unsubscribeJourney: jest.fn().mockResolvedValue(mockUser),
subscribeSubject: jest.fn().mockResolvedValue(mockSubscribedSubject),
unsubscribeSubject: jest.fn().mockResolvedValue(mockUnsubscribedSubject),
getUserById: jest.fn().mockResolvedValue(mockUser),
deleteUserById: jest.fn().mockResolvedValue(undefined),
updateUserRole: jest.fn().mockResolvedValue(mockUser),
Expand Down Expand Up @@ -89,6 +108,38 @@ describe('UsersController', () => {
})
})

it('should subscribe to subject', async () => {
const userId = 'mocked-id';
const subjectId = 'mocked-subject';

expect(await controller.subscribeSubject(userId, subjectId)).toBe(mockSubscribedSubject);
})

it('should return error when trying to subscribe to subject', async () => {
const userId = 'false-id';
const subjectId = 'mocked-subject';

mockUserService.subscribeSubject.mockRejectedValueOnce(new NotFoundException(`Couldn't find user with ID ${userId}`));

await expect(controller.subscribeSubject(userId, subjectId)).rejects.toBeInstanceOf(NotFoundException);
})

it('should unsubscribe to subject', async () => {
const userId = 'mocked-id';
const subjectId = 'mocked-subject';

expect(await controller.unsubscribeSubject(userId, subjectId)).toBe(mockUnsubscribedSubject);
})

it('should return error when trying to unsubscribe to subject', async () => {
const userId = 'false-id';
const subjectId = 'mocked-subject';

mockUserService.unsubscribeSubject.mockRejectedValueOnce(new NotFoundException(`Couldn't find user with ID ${userId}`));

await expect(controller.unsubscribeSubject(userId, subjectId)).rejects.toBeInstanceOf(NotFoundException);
})

it('should return an error while trying to update user', async () => {
const updateUserDto: UpdateUserDto = {
name: 'Mock User',
Expand All @@ -115,6 +166,19 @@ describe('UsersController', () => {
await expect(controller.getSubscribedJourneys(userId)).resolves.toEqual([]);
});

it('should return error when trying to return subscribed subjects', async () => {
const userId = 'false-id';

mockUserService.getSubscribedSubjects.mockRejectedValueOnce(new NotFoundException(`User with ID ${userId} not found`));

await expect(controller.getSubscribedSubjects(userId)).rejects.toBeInstanceOf(NotFoundException);
});

it('should get subscribed subjects', async () => {
const userId = 'mockUserId';
await expect(controller.getSubscribedSubjects(userId)).resolves.toEqual([]);
});

it('should get all users', async () => {
await expect(controller.getUsers()).resolves.toEqual([mockUser]);
});
Expand Down
41 changes: 41 additions & 0 deletions test/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('UsersService', () => {
verificationToken: 'mockToken',
isVerified: false,
subscribedJourneys: [new Types.ObjectId(), new Types.ObjectId()],
subscribedSubjects: [new Types.ObjectId(), new Types.ObjectId()],
completedTrails: [new Types.ObjectId(), new Types.ObjectId()],
save: jest.fn().mockResolvedValue(this),
};
Expand All @@ -37,6 +38,7 @@ describe('UsersService', () => {
verificationToken: 'mockToken',
isVerified: false,
subscribedJourneys: [new Types.ObjectId(), new Types.ObjectId()],
subscribedSubjects: [new Types.ObjectId(), new Types.ObjectId()],
completedTrails: [new Types.ObjectId(), new Types.ObjectId()],
save: jest.fn().mockResolvedValue(this),
};
Expand Down Expand Up @@ -274,6 +276,45 @@ describe('UsersService', () => {
).rejects.toThrow(NotFoundException);
});

it('should add a subject to user subscribedSubjects if not already subscribed', async () => {
const subjectId = new Types.ObjectId().toHexString();

jest.spyOn(model, 'findById').mockReturnValueOnce({
exec: jest.fn().mockResolvedValue(mockUser),
} as any);

const result = await service.subscribeSubject('mockId', subjectId);

expect(result.subscribedSubjects).toContainEqual(
new Types.ObjectId(subjectId),
);
expect(result.subscribedSubjects.length).toBe(3);
});

it('should throw NotFoundException if user is not found when subscribing in a subject', async () => {
const subjectId = new Types.ObjectId().toHexString();

jest.spyOn(model, 'findById').mockReturnValueOnce({
exec: jest.fn().mockResolvedValue(null),
} as any);

await expect(
service.subscribeSubject('invalidId', subjectId),
).rejects.toThrow(NotFoundException);
});

it('should throw NotFoundException if user is not found when unsubscribing from a subject', async () => {
const subjectId = new Types.ObjectId().toHexString();

jest.spyOn(model, 'findById').mockReturnValueOnce({
exec: jest.fn().mockResolvedValue(null),
} as any);

await expect(
service.unsubscribeSubject('invalidId', subjectId),
).rejects.toThrow(NotFoundException);
});

it('should add a journey to user subscribedJourneys if not already subscribed', async () => {
const journeyId = new Types.ObjectId().toHexString();

Expand Down

0 comments on commit e3b7473

Please sign in to comment.