Skip to content

Commit

Permalink
test: additional cases for SplidClient.getBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusBolls committed Nov 7, 2024
1 parent 1910470 commit 0f262d3
Showing 1 changed file with 137 additions and 1 deletion.
138 changes: 137 additions & 1 deletion src/getBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,90 @@ describe('getBalance', () => {
});
});

it('de-duplicates expenses with the same GlobalId', () => {
expect(
getBalance(
[{ GlobalId: 'linus' }, { GlobalId: 'laurin' }],
[
{
GlobalId: '#1',
isDeleted: false,
primaryPayer: 'linus',
currencyCode: 'EUR',
items: [
{
AM: 10,
P: {
PT: 0,
P: {
laurin: 1,
},
},
},
],
},
{
GlobalId: '#1',
isDeleted: false,
primaryPayer: 'linus',
currencyCode: 'EUR',
items: [
{
AM: 10,
P: {
PT: 0,
P: {
laurin: 1,
},
},
},
],
},
]
)
).toEqual({
linus: balance(10, 0),
laurin: balance(0, 10),
});
});

it('ignores deleted expenses', () => {
expect(
getBalance(
[{ GlobalId: 'linus' }, { GlobalId: 'laurin' }],
[
{
GlobalId: '#1',
isDeleted: true,
primaryPayer: 'linus',
currencyCode: 'EUR',
items: [
{
AM: 10,
P: {
PT: 0,
P: {
laurin: 1,
},
},
},
],
},
]
)
).toEqual({
linus: balance(0, 0),
laurin: balance(0, 0),
});
});

it('handles basic examples', () => {
expect(
getBalance(
[{ GlobalId: 'linus' }, { GlobalId: 'laurin' }],
[
{
GlobalId: '#',
GlobalId: '#1',
isDeleted: false,
primaryPayer: 'linus',
currencyCode: 'EUR',
Expand All @@ -49,4 +126,63 @@ describe('getBalance', () => {
laurin: balance(0, 10),
});
});

it('handles currency conversion', () => {
const expenses = [
{
GlobalId: '#1',
isDeleted: false,
primaryPayer: 'linus',
currencyCode: 'EUR',
items: [
{
AM: 10,
P: {
PT: 0 as const,
P: {
laurin: 1,
},
},
},
],
},
{
GlobalId: '#2',
isDeleted: false,
primaryPayer: 'linus',
currencyCode: 'USD',
items: [
{
AM: 10,
P: {
PT: 0 as const,
P: {
laurin: 1,
},
},
},
],
},
];

expect(
getBalance([{ GlobalId: 'linus' }, { GlobalId: 'laurin' }], expenses, {
currencyRates: { EUR: 1.1, USD: 1 },
defaultCurrencyCode: 'USD',
})
).toEqual({
linus: balance(21, 0),
laurin: balance(0, 21),
});

expect(
getBalance([{ GlobalId: 'linus' }, { GlobalId: 'laurin' }], expenses, {
currencyRates: { EUR: 1.1, USD: 1 },
defaultCurrencyCode: 'EUR',
})
).toEqual({
linus: balance(19.09090909090909, 0),
laurin: balance(0, 19.09090909090909),
});
});
});

0 comments on commit 0f262d3

Please sign in to comment.