Skip to content

Commit

Permalink
Add tests for car model
Browse files Browse the repository at this point in the history
Signed-off-by: Giovanni Baratta <[email protected]>
  • Loading branch information
giovannibaratta committed May 20, 2024
1 parent b1ae520 commit c17d13a
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 3 deletions.
155 changes: 155 additions & 0 deletions src/model/car.test.ts
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({})
})
})
6 changes: 3 additions & 3 deletions src/model/car.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import {dateToPeriod, getFirstDayOfNextMonthsFrom, isPeriodBetweenStartAndEnd, i

export type CarExpenses = CarMonthlyRateOnly | CarUpfrontOnly | CarMonthlyRateAndUpfront

interface CarMonthlyRateOnly {
export interface CarMonthlyRateOnly {
type: "CarMonthlyRateOnly"
monthlyRate: number
duration: number
startDate: Date
}

interface CarUpfrontOnly {
export interface CarUpfrontOnly {
type: "CarUpfrontOnly"
date: Date
upfront: number
}

interface CarMonthlyRateAndUpfront {
export interface CarMonthlyRateAndUpfront {
type: "CarMonthlyRateAndUpfront"
monthlyRate: number
duration: number
Expand Down

0 comments on commit c17d13a

Please sign in to comment.