Skip to content

✅ test: add unit test for src/server/services/user/index.ts #6187

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

Merged
Merged
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
19 changes: 17 additions & 2 deletions src/server/services/user/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { UserItem } from '@/database/schemas';
import { UserModel } from '@/database/server/models/user';
import { pino } from '@/libs/logger';
import { AgentService } from '@/server/services/agent';

import { UserService } from './index';

// Mock dependencies
vi.mock('@/server/services/agent');
vi.mock('@/database/server/models/user', () => {
const MockUserModel = vi.fn();
// @ts-ignore
Expand All @@ -30,6 +30,12 @@ vi.mock('@/libs/logger', () => ({
},
}));

vi.mock('@/server/services/agent', () => ({
AgentService: vi.fn().mockImplementation(() => ({
createInbox: vi.fn().mockResolvedValue(undefined),
})),
}));

let service: UserService;
const mockUserId = 'test-user-id';

Expand Down Expand Up @@ -58,7 +64,7 @@ describe('UserService', () => {
// Mock user not found
vi.mocked(UserModel.findById).mockResolvedValue(null as any);

await service.createUser(mockUserId, mockUserJSON);
const result = await service.createUser(mockUserId, mockUserJSON);

expect(UserModel.findById).toHaveBeenCalledWith(expect.anything(), mockUserId);
expect(UserModel.createUser).toHaveBeenCalledWith(
Expand All @@ -74,6 +80,12 @@ describe('UserService', () => {
clerkCreatedAt: new Date('2023-01-01T00:00:00Z'),
}),
);
expect(AgentService).toHaveBeenCalledWith(expect.anything(), mockUserId);
expect(vi.mocked(AgentService).mock.results[0].value.createInbox).toHaveBeenCalled();
expect(result).toEqual({
message: 'user created',
success: true,
});
});

it('should not create user if already exists', async () => {
Expand All @@ -84,6 +96,7 @@ describe('UserService', () => {

expect(UserModel.findById).toHaveBeenCalledWith(expect.anything(), mockUserId);
expect(UserModel.createUser).not.toHaveBeenCalled();
expect(AgentService).not.toHaveBeenCalled();
expect(result).toEqual({
message: 'user not created due to user already existing in the database',
success: false,
Expand All @@ -107,6 +120,8 @@ describe('UserService', () => {
phone: '+1234567890', // Should use first phone number
}),
);
expect(AgentService).toHaveBeenCalledWith(expect.anything(), mockUserId);
expect(vi.mocked(AgentService).mock.results[0].value.createInbox).toHaveBeenCalled();
});
});

Expand Down