Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

test: added user update unit tests

Mir Arif Hasan9e304b947b62
diff --git a/packages/hoppscotch-backend/src/user/user.service.spec.ts b/packages/hoppscotch-backend/src/user/user.service.spec.ts
new file mode 100644
index 000000000..e5f293519
--- /dev/null
+++ b/packages/hoppscotch-backend/src/user/user.service.spec.ts
@@ -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: '[email protected]',
+ 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,
+ );
+ });
+ });
+});