-
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.
Signed-off-by: Giovanni Baratta <[email protected]>
- Loading branch information
1 parent
b1ae520
commit c17d13a
Showing
2 changed files
with
158 additions
and
3 deletions.
There are no files selected for viewing
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,155 @@ | ||
import {buildCarExpensesCalculator, CarMonthlyRateAndUpfront, CarMonthlyRateOnly, CarUpfrontOnly} from "./car.ts" | ||
import {Period} from "./monthly-report.ts" | ||
import {dateToPeriod} from "../utils/date.ts" | ||
|
||
describe("buildCarExpensesCalculator", () => { | ||
it("should calculate the monthly report for CarMonthlyRateOnly when the loan is still ongoing", () => { | ||
// Given | ||
const config: CarMonthlyRateOnly = { | ||
type: "CarMonthlyRateOnly", | ||
monthlyRate: 100, | ||
duration: 12, | ||
startDate: new Date(2023, 10, 1) | ||
} | ||
const calculator = buildCarExpensesCalculator(config) | ||
const period: Period = dateToPeriod(new Date(2023, 10, 1)) | ||
|
||
// When | ||
const monthlyReport = calculator.computeMonthlyReport(period) | ||
|
||
// Then | ||
expect(monthlyReport.totalIncome).toBe(0) | ||
expect(monthlyReport.period).toEqual(period) | ||
expect(monthlyReport.totalExpenses).toBe(100) | ||
expect(monthlyReport.detailedExpenses).toEqual({monthlyRate: 100}) | ||
}) | ||
|
||
it("should calculate the monthly report for CarMonthlyRateOnly when the loan is terminated", () => { | ||
// Given | ||
const config: CarMonthlyRateOnly = { | ||
type: "CarMonthlyRateOnly", | ||
monthlyRate: 100, | ||
duration: 12, | ||
startDate: new Date(2023, 10, 1) | ||
} | ||
const calculator = buildCarExpensesCalculator(config) | ||
const period: Period = dateToPeriod(new Date(2024, 10, 1)) | ||
|
||
// When | ||
const monthlyReport = calculator.computeMonthlyReport(period) | ||
|
||
// Then | ||
expect(monthlyReport.totalIncome).toBe(0) | ||
expect(monthlyReport.period).toEqual(period) | ||
expect(monthlyReport.totalExpenses).toBe(0) | ||
expect(monthlyReport.detailedExpenses).toEqual({}) | ||
}) | ||
|
||
it("should calculate monthly report for CarUpfrontOnly when period is the same as the date", () => { | ||
// Given | ||
const config: CarUpfrontOnly = { | ||
type: "CarUpfrontOnly", | ||
date: new Date(2023, 10, 10), | ||
upfront: 1000 | ||
} | ||
const calculator = buildCarExpensesCalculator(config) | ||
const period: Period = dateToPeriod(new Date(2023, 10, 1)) | ||
|
||
// When | ||
const monthlyReport = calculator.computeMonthlyReport(period) | ||
|
||
// Then | ||
expect(monthlyReport.totalIncome).toBe(0) | ||
expect(monthlyReport.period).toEqual(period) | ||
expect(monthlyReport.totalExpenses).toBe(1000) | ||
expect(monthlyReport.detailedExpenses).toEqual({upfront: 1000}) | ||
}) | ||
|
||
it("should calculate monthly report for CarUpfrontOnly when period is not the same as the date", () => { | ||
// Given | ||
const config: CarUpfrontOnly = { | ||
type: "CarUpfrontOnly", | ||
date: new Date(2023, 10, 10), | ||
upfront: 1000 | ||
} | ||
const calculator = buildCarExpensesCalculator(config) | ||
const period: Period = dateToPeriod(new Date(2023, 11, 1)) | ||
|
||
// When | ||
const monthlyReport = calculator.computeMonthlyReport(period) | ||
|
||
// Then | ||
expect(monthlyReport.totalIncome).toBe(0) | ||
expect(monthlyReport.period).toEqual(period) | ||
expect(monthlyReport.totalExpenses).toBe(0) | ||
expect(monthlyReport.detailedExpenses).toEqual({}) | ||
}) | ||
|
||
it("should calculate monthly report for CarMonthlyRateAndUpfront for the first period", () => { | ||
// Given | ||
const config: CarMonthlyRateAndUpfront = { | ||
type: "CarMonthlyRateAndUpfront", | ||
monthlyRate: 100, | ||
duration: 12, | ||
startDate: new Date(2023, 10, 1), | ||
upfront: 1000 | ||
} | ||
const calculator = buildCarExpensesCalculator(config) | ||
const period: Period = dateToPeriod(new Date(2023, 10, 1)) | ||
|
||
// When | ||
const monthlyReport = calculator.computeMonthlyReport(period) | ||
|
||
// Then | ||
expect(monthlyReport.totalIncome).toBe(0) | ||
expect(monthlyReport.period).toEqual(period) | ||
expect(monthlyReport.totalExpenses).toBe(1100) | ||
expect(monthlyReport.detailedExpenses).toEqual({upfront: 1000, monthlyRate: 100}) | ||
}) | ||
|
||
it("should calculate monthly report for CarMonthlyRateAndUpfront for the " | ||
+ "subsequents periods when the period is within the loan duration", () => { | ||
// Given | ||
const config: CarMonthlyRateAndUpfront = { | ||
type: "CarMonthlyRateAndUpfront", | ||
monthlyRate: 100, | ||
duration: 12, | ||
startDate: new Date(2023, 10, 1), | ||
upfront: 1000 | ||
} | ||
const calculator = buildCarExpensesCalculator(config) | ||
const period: Period = dateToPeriod(new Date(2023, 11, 1)) | ||
|
||
// When | ||
const monthlyReport = calculator.computeMonthlyReport(period) | ||
|
||
// Then | ||
expect(monthlyReport.totalIncome).toBe(0) | ||
expect(monthlyReport.period).toEqual(period) | ||
expect(monthlyReport.totalExpenses).toBe(100) | ||
expect(monthlyReport.detailedExpenses).toEqual({monthlyRate: 100}) | ||
}) | ||
|
||
it("should calculate monthly report for CarMonthlyRateAndUpfront when the period is outside the loan duration", () => { | ||
// Given | ||
const config: CarMonthlyRateAndUpfront = { | ||
type: "CarMonthlyRateAndUpfront", | ||
monthlyRate: 100, | ||
duration: 12, | ||
startDate: new Date(2023, 10, 1), | ||
upfront: 1000 | ||
} | ||
const calculator = buildCarExpensesCalculator(config) | ||
const period: Period = dateToPeriod(new Date(2024, 10, 1)) | ||
|
||
// When | ||
const monthlyReport = calculator.computeMonthlyReport(period) | ||
|
||
// Then | ||
expect(monthlyReport.component).toBe("Car") | ||
expect(monthlyReport.totalIncome).toBe(0) | ||
expect(monthlyReport.period).toEqual(period) | ||
expect(monthlyReport.totalExpenses).toBe(0) | ||
expect(monthlyReport.detailedExpenses).toEqual({}) | ||
}) | ||
}) |
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