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

Add unit tests for goal template types #3183

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
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
89 changes: 89 additions & 0 deletions packages/loot-core/src/server/budget/goals/goalsAverage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as actions from '../actions';

import { goalsAverage } from './goalsAverage';

jest.mock('../actions');

describe('goalsAverage', () => {
const mockGetSheetValue = actions.getSheetValue as jest.Mock;

beforeEach(() => {
jest.clearAllMocks();
});

it('should calculate the amount to budget based on the average of only one previous month of spending', async () => {
// Given
const template = { amount: 1 };
const month = '2024-07';
const category = { id: 1 };
const errors: string[] = [];
const to_budget = 0;

mockGetSheetValue.mockResolvedValueOnce(200);

// When
const result = await goalsAverage(
template,
month,
category,
errors,
to_budget,
);

// Then
expect(result.to_budget).toBe(-200);
expect(result.errors).toHaveLength(0);
});

it('should calculate the amount to budget based on the average of multiple previous months of spending', async () => {
// Given
const template = { amount: 4 };
const month = '2024-08';
const category = { id: 1 };
const errors: string[] = [];
const to_budget = 0;

mockGetSheetValue
.mockResolvedValueOnce(200)
.mockResolvedValueOnce(300)
.mockResolvedValueOnce(100)
.mockResolvedValueOnce(400);

// When
const result = await goalsAverage(
template,
month,
category,
errors,
to_budget,
);

// Then
expect(result.to_budget).toBe(-250);
expect(result.errors).toHaveLength(0);
});

it('should return error when template amount passed in is <= 0', async () => {
// Given
const template = { amount: 0 };
const month = '2024-08';
const category = { id: 1 };
const errors: string[] = [];
const to_budget = 1000;

// When
const result = await goalsAverage(
template,
month,
category,
errors,
to_budget,
);

// Then
expect(result.to_budget).toBe(1000);
expect(result.errors).toStrictEqual([
'Number of months to average is not valid',
]);
});
});
134 changes: 134 additions & 0 deletions packages/loot-core/src/server/budget/goals/goalsBy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import * as actions from '../actions';

import { goalsBy } from './goalsBy';

jest.mock('../actions');

describe('goalsBy', () => {
const mockIsReflectBudget = actions.isReflectBudget as jest.Mock;

beforeEach(() => {
jest.clearAllMocks();
});

it('should return correct budget amount with target in the future and no current balance', async () => {
// Given
const template = { amount: 100, month: '2024-12' };
const current_month = '2024-08';
const last_month_balance = 0;
const to_budget = 0;
const errors: string[] = [];
const template_lines = [template];
const l = 0;
const remainder = 0;
mockIsReflectBudget.mockReturnValue(false);

// When
const result = await goalsBy(
template_lines,
current_month,
template,
l,
remainder,
last_month_balance,
to_budget,
errors,
);

// Then
expect(result.to_budget).toBe(2000);
expect(result.errors).toHaveLength(0);
expect(result.remainder).toBe(0);
});

it('should return correct budget amount with target in the future and existing balance towards goal', async () => {
// Given
const template = { amount: 100, month: '2024-12' };
const current_month = '2024-08';
const last_month_balance = 5000;
const to_budget = 0;
const errors: string[] = [];
const template_lines = [template];
const l = 0;
const remainder = 0;
mockIsReflectBudget.mockReturnValue(false);

// When
const result = await goalsBy(
template_lines,
current_month,
template,
l,
remainder,
last_month_balance,
to_budget,
errors,
);

// Then
expect(result.to_budget).toBe(1000);
expect(result.errors).toHaveLength(0);
expect(result.remainder).toBe(0);
});

it('should return correct budget amount when target balance met early', async () => {
// Given
const template = { amount: 100, month: '2024-12' };
const current_month = '2024-08';
const last_month_balance = 10000;
const to_budget = 0;
const errors: string[] = [];
const template_lines = [template];
const l = 0;
const remainder = 0;
mockIsReflectBudget.mockReturnValue(false);

// When
const result = await goalsBy(
template_lines,
current_month,
template,
l,
remainder,
last_month_balance,
to_budget,
errors,
);

// Then
expect(result.to_budget).toBe(0);
expect(result.errors).toHaveLength(0);
expect(result.remainder).toBe(0);
});

it('should return error when budget is a reflect budget', async () => {
// Given
const template = { amount: -100, month: '2024-08', repeat: 1 };
const current_month = '2024-08';
const last_month_balance = 0;
const to_budget = 0;
const errors: string[] = [];
const template_lines = [template];
const l = 0;
const remainder = 0;
mockIsReflectBudget.mockReturnValue(true);

// When
const result = await goalsBy(
template_lines,
current_month,
template,
l,
remainder,
last_month_balance,
to_budget,
errors,
);

// Then
expect(result.to_budget).toBe(0);
expect(result.errors).toStrictEqual([
'by templates are not supported in Report budgets',
]);
});
});
Loading
Loading