test: added user update unit tests
This commit is contained in:
53
packages/hoppscotch-backend/src/user/user.service.spec.ts
Normal file
53
packages/hoppscotch-backend/src/user/user.service.spec.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { DeepMockProxy, mockDeep, mockReset } from 'jest-mock-extended';
|
||||||
|
import { PrismaService } from 'src/prisma/prisma.service';
|
||||||
|
import { PubSubService } from 'src/pubsub/pubsub.service';
|
||||||
|
import { UserService } from './user.service';
|
||||||
|
|
||||||
|
const mockPrisma = mockDeep<PrismaService>();
|
||||||
|
const mockPubSub = mockDeep<PubSubService>();
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
const userService = new UserService(mockPrisma, mockPubSub as any);
|
||||||
|
|
||||||
|
const user = {
|
||||||
|
uid: '123',
|
||||||
|
displayName: 'John Doe',
|
||||||
|
email: 'test@hoppscotch.io',
|
||||||
|
photoURL: 'https://example.com/avatar.png',
|
||||||
|
currentRESTSession: JSON.stringify({}),
|
||||||
|
currentGQLSession: JSON.stringify({}),
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mockReset(mockPrisma);
|
||||||
|
mockPubSub.publish.mockClear();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('UserService', () => {
|
||||||
|
describe('updateUser', () => {
|
||||||
|
test('should update user', async () => {
|
||||||
|
mockPrisma.user.update.mockResolvedValue(user);
|
||||||
|
|
||||||
|
const result = await userService.updateUser(user, {
|
||||||
|
currentGQLSession: user.currentGQLSession,
|
||||||
|
currentRESTSession: user.currentRESTSession,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqualRight(user);
|
||||||
|
});
|
||||||
|
test('should publish user update subscription', async () => {
|
||||||
|
mockPrisma.user.update.mockResolvedValue(user);
|
||||||
|
|
||||||
|
await userService.updateUser(user, {
|
||||||
|
currentGQLSession: user.currentGQLSession,
|
||||||
|
currentRESTSession: user.currentRESTSession,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockPubSub.publish).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
||||||
|
`user/${user.uid}/updated`,
|
||||||
|
user,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user