Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/andreasalstrup/CoShare into…
Browse files Browse the repository at this point in the history
… GroupScreen
  • Loading branch information
emilnormann committed Dec 8, 2023
2 parents b867707 + a821579 commit ec3c779
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
60 changes: 60 additions & 0 deletions __tests__/unitTests/CalculateBalance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { calculateBalance, Expense } from '../../helpers/calculateBalance';

describe('calculateBalance', () => {
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 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.

0 comments on commit ec3c779

Please sign in to comment.