Skip to content
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

Unit test: calculateBalance() #72

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
73 changes: 73 additions & 0 deletions __tests__/unitTests/CalculateBalance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { calculateBalance, Expense } from '../../helpers/calculateBalance';

describe('calculateExpenses', () => {
mikejensen0 marked this conversation as resolved.
Show resolved Hide resolved
it('should handle no balances', () => {
const result = calculateBalance([]);
expect(result).toEqual([]);
});

it('should handle a single user balance', () => {
const expenses: Expense[] = [
{ user: 'Alice', amount: 50 },
];
const expectedBalance: Expense[] = [
{ user: 'Alice', amount: 0 }
];
const result = calculateBalance(expenses);
expect(result).toEqual(expect.arrayContaining(expectedBalance));
});

it('should handle multiple user balances', () => {
const expenses: Expense[] = [
{ user: 'Alice', amount: 50 },
{ user: 'Bob', amount: 0 },
];
const expectedBalance: Expense[] = [
{ user: 'Alice', amount: 25 },
{ user: 'Bob', amount: -25 },
];
const result = calculateBalance(expenses);
expect(result).toEqual(expect.arrayContaining(expectedBalance));
});

it('should handle multiple users with no balance', () => {
const expenses: Expense[] = [
{ user: 'Alice', amount: 0 },
{ user: 'Bob', amount: 0 },
{ user: 'Charlie', amount: 0 },
];
const expectedBalance: Expense[] = [
{ user: 'Alice', amount: 0 },
{ user: 'Bob', amount: 0 },
{ user: 'Charlie', amount: 0 },
];
const result = calculateBalance(expenses);
expect(result).toEqual(expect.arrayContaining(expectedBalance));
});

it('should handle multiple user balances', () => {
mikejensen0 marked this conversation as resolved.
Show resolved Hide resolved
const expenses: Expense[] = [
{ user: 'Alice', amount: 50 },
{ user: 'Bob', amount: 0 },
];
const expectedBalance: Expense[] = [
{ user: 'Alice', amount: 25 },
{ user: 'Bob', amount: -25 },
];
const result = calculateBalance(expenses);
expect(result).toEqual(expect.arrayContaining(expectedBalance));
});

it('should handle large numbers', () => {
const expenses: Expense[] = [
{ user: 'Alice', amount: Number.MAX_VALUE },
{ user: 'Bob', amount: 0 },
];
const expectedBalance: Expense[] = [
{ user: 'Alice', amount: Number.MAX_VALUE/2 },
{ user: 'Bob', amount: -Number.MAX_VALUE/2 },
];
const result = calculateBalance(expenses);
expect(result).toEqual(expect.arrayContaining(expectedBalance));
});
});
2 changes: 1 addition & 1 deletion helpers/calculateBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Expense = {
export type Expense = {
user: string;
amount: number;
};
Expand Down
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.