Files
web/packages/booking-flow/lib/utils/SelectRate/index.test.ts
Anton Gunnarsson 7fc49428c7 Merged in chore/price-calculation-tests (pull request #3072)
chore: Add more price calculation tests

* Add tests and refactor types for getVoucherPrice

* Add tests for sumPackagesRequestedPrice

* Add tests for calculateVat


Approved-by: Joakim Jäderberg
2025-11-04 12:50:51 +00:00

145 lines
4.0 KiB
TypeScript

import { describe, expect, it } from "vitest"
import { AlertTypeEnum } from "@scandic-hotels/common/constants/alert"
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import { dt } from "@scandic-hotels/common/dt"
import {
calculateVat,
filterOverlappingDates,
sumPackages,
sumPackagesRequestedPrice,
} from "./index"
import type { specialAlertsSchema } from "@scandic-hotels/trpc/routers/hotels/schemas/hotel/specialAlerts"
import type { z } from "zod"
type Alert = z.infer<typeof specialAlertsSchema>[number]
function makeAlert(start: string, end: string): Alert {
return {
id: "test-id",
name: "Test Alert",
heading: "Test Heading",
text: "Some text",
type: AlertTypeEnum.Alarm,
displayInBookingFlow: true,
startDate: start,
endDate: end,
}
}
describe("filterOverlappingDates", () => {
const alert = makeAlert("2025-09-01", "2025-09-10")
it("shows alert if booking starts inside alert", () => {
const result = filterOverlappingDates(
[alert],
dt("2025-09-05"),
dt("2025-09-12")
)
expect(result).toHaveLength(1)
})
it("shows alert if booking ends inside alert", () => {
const result = filterOverlappingDates(
[alert],
dt("2025-08-28"),
dt("2025-09-05")
)
expect(result).toHaveLength(1)
})
it("shows alert if booking fully contains alert", () => {
const result = filterOverlappingDates(
[alert],
dt("2025-08-28"),
dt("2025-09-15")
)
expect(result).toHaveLength(1)
})
it("shows alert if alert fully contains booking", () => {
const result = filterOverlappingDates(
[alert],
dt("2025-09-03"),
dt("2025-09-05")
)
expect(result).toHaveLength(1)
})
it("does not show alert if no overlap", () => {
const result = filterOverlappingDates(
[alert],
dt("2025-08-01"),
dt("2025-08-05")
)
expect(result).toHaveLength(0)
})
})
describe("sumPackages", () => {
it("returns 0 price for null packages", () => {
const result = sumPackages(null)
expect(result).toEqual({ currency: undefined, price: 0 })
})
it("returns 0 price for undefined packages", () => {
const result = sumPackages(undefined)
expect(result).toEqual({ currency: undefined, price: 0 })
})
it("returns 0 price for empty packages", () => {
const result = sumPackages([])
expect(result).toEqual({ currency: undefined, price: 0 })
})
it("sums prices of packages", () => {
const result = sumPackages([
{ localPrice: { totalPrice: 100, currency: CurrencyEnum.SEK } },
{ localPrice: { totalPrice: 200, currency: CurrencyEnum.SEK } },
{ localPrice: { totalPrice: 50, currency: CurrencyEnum.SEK } },
])
expect(result).toEqual({ currency: CurrencyEnum.SEK, price: 350 })
})
})
describe("sumPackagesRequestedPrice", () => {
it("returns 0 price for null packages", () => {
const result = sumPackagesRequestedPrice(null)
expect(result).toEqual({ currency: undefined, price: 0 })
})
it("returns 0 price for undefined packages", () => {
const result = sumPackagesRequestedPrice(undefined)
expect(result).toEqual({ currency: undefined, price: 0 })
})
it("returns 0 price for empty packages", () => {
const result = sumPackagesRequestedPrice([])
expect(result).toEqual({ currency: undefined, price: 0 })
})
it("sums prices of packages", () => {
const result = sumPackagesRequestedPrice([
{ requestedPrice: { totalPrice: 100, currency: CurrencyEnum.SEK } },
{ requestedPrice: { totalPrice: 200, currency: CurrencyEnum.SEK } },
{ requestedPrice: { totalPrice: 50, currency: CurrencyEnum.SEK } },
])
expect(result).toEqual({ currency: CurrencyEnum.SEK, price: 350 })
})
})
describe("calculateVat", () => {
it("calculates VAT correctly", () => {
expect(calculateVat(100, 25)).toEqual({
priceExclVat: 80,
vatAmount: 20,
})
expect(calculateVat(5, 100)).toEqual({
priceExclVat: 2.5,
vatAmount: 2.5,
})
})
})