-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ivanleekk/feat/add-test
Feat/add test
- Loading branch information
Showing
16 changed files
with
13,006 additions
and
3,481 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
import { addHandler } from './addHandler'; | ||
import { sendMessage, findUser_byUsername } from '../utils/utils'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import resetAllMocks = jest.resetAllMocks; | ||
|
||
vi.mock('../utils/utils', () => ({ | ||
sendMessage: vi.fn(), | ||
findUser_byUsername: vi.fn(), | ||
})); | ||
vi.mock('@prisma/client', () => { | ||
const mPrismaClient = { | ||
group: { | ||
findUnique: vi.fn(), | ||
update: vi.fn(), | ||
}, | ||
user: { | ||
findUnique: vi.fn(), | ||
create: vi.fn(), | ||
}, | ||
userGroupBalance: { | ||
findFirst: vi.fn(), | ||
create: vi.fn(), | ||
update: vi.fn(), | ||
findMany: vi.fn(), | ||
}, | ||
transaction: { | ||
create: vi.fn(), | ||
}, | ||
}; | ||
return { PrismaClient: vi.fn(() => mPrismaClient) }; | ||
}); | ||
|
||
describe('addHandler', () => { | ||
let prisma: any; | ||
let chatId: string; | ||
let messageSender: string; | ||
|
||
beforeEach(() => { | ||
prisma = new PrismaClient(); | ||
chatId = '1'; | ||
messageSender = 'testuser'; | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should return an error message if the format is invalid', async () => { | ||
const messageArray = ['/add', '10']; | ||
await addHandler(messageArray, chatId, messageSender); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'Invalid format! Please use /add [amount] [description] [people]'); | ||
}); | ||
|
||
it('should return an error message if the group does not exist', async () => { | ||
prisma.group.findUnique.mockResolvedValue(null); | ||
const messageArray = ['/add', '10', 'lunch', '@user1']; | ||
await addHandler(messageArray, chatId, messageSender); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'Cashshare Bot is not initialized for this group! Use /start to initialize.'); | ||
}); | ||
|
||
it('should create a new user if a user is not found', async () => { | ||
prisma.group.findUnique.mockResolvedValue({ id: chatId, members: [] }); | ||
prisma.user.findUnique.mockResolvedValue(null); | ||
findUser_byUsername.mockResolvedValue(null); | ||
prisma.user.create.mockResolvedValue({ id: 'newUser' }); | ||
|
||
const messageArray = ['/add', '10', 'lunch', '@newUser']; | ||
await addHandler(messageArray, chatId, messageSender); | ||
expect(prisma.user.create).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should add the expense and update balances correctly', async () => { | ||
prisma.group.findUnique.mockResolvedValue({ id: chatId, members: [] }); | ||
findUser_byUsername.mockResolvedValue({ id: 'user1' }); | ||
prisma.user.findUnique.mockResolvedValue({ id: 'user1' }); | ||
prisma.userGroupBalance.findFirst.mockResolvedValue(null); | ||
prisma.userGroupBalance.findMany.mockResolvedValue([{ userId: 'user1', balance: 0 }]); | ||
|
||
const messageArray = ['/add', '10', 'lunch', '@user1']; | ||
await addHandler(messageArray, chatId, messageSender); | ||
|
||
expect(prisma.transaction.create).toHaveBeenCalled(); | ||
expect(prisma.userGroupBalance.update).toHaveBeenCalled(); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'Added expense of $10 for lunch for @testuser, @user1!'); | ||
}); | ||
|
||
|
||
it('should handle multiple users correctly', async () => { | ||
prisma.group.findUnique.mockResolvedValue({ id: chatId, members: [] }); | ||
findUser_byUsername.mockResolvedValueOnce({ id: 'user1' }).mockResolvedValueOnce({ id: 'user2' }); | ||
prisma.user.findUnique.mockResolvedValueOnce({ id: 'user1' }).mockResolvedValueOnce({ id: 'user2' }); | ||
prisma.userGroupBalance.findFirst.mockResolvedValue(null); | ||
prisma.userGroupBalance.findMany.mockResolvedValue([{ userId: 'user1', balance: 0 }, { userId: 'user2', balance: 0 }]); | ||
|
||
const messageArray = ['/add', '20', 'dinner', '@user1', '@user2']; | ||
await addHandler(messageArray, chatId, messageSender); | ||
|
||
expect(prisma.transaction.create).toHaveBeenCalled(); | ||
expect(prisma.userGroupBalance.update).toHaveBeenCalledTimes(3); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'Added expense of $20 for dinner for @testuser, @user1, @user2!'); | ||
}); | ||
|
||
// it('should handle database errors gracefully', async () => { | ||
// prisma.group.findUnique.mockRejectedValue(new Error('Database error')); | ||
// const messageArray = ['/add', '10', 'lunch', '@user1']; | ||
// await addHandler(messageArray, chatId, messageSender); | ||
// expect(sendMessage).toHaveBeenCalledWith(chatId, 'Error adding expense! Please try again.'); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
import { individualBalanceHandler, groupBalanceHandler } from './BalanceHandler'; | ||
import { sendMessage, findUser_byUsername } from '../utils/utils'; | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
vi.mock('../utils/utils', () => ({ | ||
sendMessage: vi.fn(), | ||
findUser_byUsername: vi.fn(), | ||
})); | ||
|
||
vi.mock('@prisma/client', () => { | ||
const mPrismaClient = { | ||
group: { | ||
findUnique: vi.fn(), | ||
}, | ||
userGroupBalance: { | ||
findUnique: vi.fn(), | ||
}, | ||
}; | ||
return { PrismaClient: vi.fn(() => mPrismaClient) }; | ||
}); | ||
|
||
describe('BalanceHandler', () => { | ||
let prisma: any; | ||
let chatId: string; | ||
let messageSender: string; | ||
|
||
beforeEach(() => { | ||
prisma = new PrismaClient(); | ||
chatId = '1'; | ||
messageSender = 'testuser'; | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('individualBalanceHandler', () => { | ||
it('should return an error message if the user is not part of the group', async () => { | ||
findUser_byUsername.mockResolvedValue(null); | ||
await individualBalanceHandler(chatId, messageSender); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'You are not part of this group!'); | ||
}); | ||
|
||
it('should return an error message if the user has no balance in the group', async () => { | ||
findUser_byUsername.mockResolvedValue({ id: 'user1' }); | ||
prisma.userGroupBalance.findUnique.mockResolvedValue(null); | ||
await individualBalanceHandler(chatId, messageSender); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'You have no balance in this group!'); | ||
}); | ||
|
||
it('should return the user\'s balance in the group', async () => { | ||
findUser_byUsername.mockResolvedValue({ id: 'user1' }); | ||
prisma.userGroupBalance.findUnique.mockResolvedValue({ balance: 100 }); | ||
await individualBalanceHandler(chatId, messageSender); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'Your balance in this group is $100.00'); | ||
}); | ||
}); | ||
|
||
describe('groupBalanceHandler', () => { | ||
it('should return an error message if the group is not found', async () => { | ||
prisma.group.findUnique.mockResolvedValue(null); | ||
await groupBalanceHandler(chatId); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'Group not found!'); | ||
}); | ||
|
||
it('should return the group balance', async () => { | ||
prisma.group.findUnique.mockResolvedValue({ | ||
id: chatId, | ||
members: [{ id: 'user1', username: 'user1' }, { id: 'user2', username: 'user2' }], | ||
}); | ||
prisma.userGroupBalance.findUnique | ||
.mockResolvedValueOnce({ balance: 50 }) | ||
.mockResolvedValueOnce({ balance: 75 }); | ||
|
||
await groupBalanceHandler(chatId); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'The group balance is \nuser1: $50.00\nuser2: $75.00'); | ||
}); | ||
|
||
it('should handle members with no balance', async () => { | ||
prisma.group.findUnique.mockResolvedValue({ | ||
id: chatId, | ||
members: [{ id: 'user1', username: 'user1' }, { id: 'user2', username: 'user2' }], | ||
}); | ||
prisma.userGroupBalance.findUnique | ||
.mockResolvedValueOnce(null) | ||
.mockResolvedValueOnce({ balance: 75 }); | ||
|
||
await groupBalanceHandler(chatId); | ||
expect(sendMessage).toHaveBeenCalledWith(chatId, 'The group balance is \nuser1: $0.00\nuser2: $75.00'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.