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
461 lines
10 KiB
TypeScript
461 lines
10 KiB
TypeScript
import { describe, expect, it } from "vitest"
|
|
|
|
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
|
|
|
|
import {
|
|
getAdditionalPrice,
|
|
getRedemptionPrice,
|
|
getVoucherPrice,
|
|
} from "./helpers"
|
|
|
|
type GetAdditionalPriceParams = Parameters<typeof getAdditionalPrice>
|
|
describe("getAdditionalPrice", () => {
|
|
it("should calculate additional price correctly with only additional price", () => {
|
|
const total: GetAdditionalPriceParams[0] = {
|
|
local: {
|
|
additionalPrice: 0,
|
|
additionalPriceCurrency: CurrencyEnum.CC,
|
|
},
|
|
}
|
|
const adults = 1
|
|
const nights = 1
|
|
const breakfast = undefined
|
|
const packages = undefined
|
|
const additionalPrice = 100
|
|
const additionalPriceCurrency = CurrencyEnum.SEK
|
|
|
|
getAdditionalPrice(
|
|
total,
|
|
adults,
|
|
breakfast,
|
|
nights,
|
|
packages,
|
|
additionalPrice,
|
|
additionalPriceCurrency
|
|
)
|
|
|
|
expect(total.local.additionalPrice).toBe(100)
|
|
})
|
|
|
|
it("should set additional price currency correctly when missing in total", () => {
|
|
const total: GetAdditionalPriceParams[0] = {
|
|
local: {
|
|
additionalPrice: 0,
|
|
},
|
|
}
|
|
const adults = 1
|
|
const nights = 1
|
|
const breakfast = undefined
|
|
const packages = undefined
|
|
const additionalPrice = 100
|
|
const additionalPriceCurrency = CurrencyEnum.SEK
|
|
|
|
getAdditionalPrice(
|
|
total,
|
|
adults,
|
|
breakfast,
|
|
nights,
|
|
packages,
|
|
additionalPrice,
|
|
additionalPriceCurrency
|
|
)
|
|
|
|
expect(total.local.additionalPriceCurrency).toBe(CurrencyEnum.SEK)
|
|
})
|
|
|
|
it("should calculate price correctly with breakfast", () => {
|
|
const total: GetAdditionalPriceParams[0] = {
|
|
local: {
|
|
additionalPrice: 0,
|
|
},
|
|
}
|
|
const adults = 2
|
|
const nights = 2
|
|
const breakfast = { localPrice: { price: 50, currency: CurrencyEnum.SEK } }
|
|
const packages: never[] = []
|
|
|
|
getAdditionalPrice(total, adults, breakfast, nights, packages)
|
|
|
|
expect(total.local.additionalPrice).toBe(200)
|
|
})
|
|
|
|
it("should calculate price correctly with packages", () => {
|
|
const total: GetAdditionalPriceParams[0] = {
|
|
local: {
|
|
additionalPrice: 0,
|
|
},
|
|
}
|
|
const adults = 2
|
|
const nights = 2
|
|
const breakfast = undefined
|
|
const packages = [
|
|
{
|
|
localPrice: { totalPrice: 50, currency: CurrencyEnum.SEK },
|
|
},
|
|
{
|
|
localPrice: { totalPrice: 25, currency: CurrencyEnum.SEK },
|
|
},
|
|
]
|
|
|
|
getAdditionalPrice(total, adults, breakfast, nights, packages)
|
|
|
|
expect(total.local.additionalPrice).toBe(75)
|
|
})
|
|
|
|
it("should calculate price correctly with breakfast, packages and additionalPrice", () => {
|
|
const total: GetAdditionalPriceParams[0] = {
|
|
local: {
|
|
additionalPrice: 0,
|
|
},
|
|
}
|
|
const adults = 2
|
|
const nights = 2
|
|
const breakfast = { localPrice: { price: 50, currency: CurrencyEnum.SEK } }
|
|
const packages = [
|
|
{
|
|
localPrice: { totalPrice: 50, currency: CurrencyEnum.SEK },
|
|
},
|
|
{
|
|
localPrice: { totalPrice: 25, currency: CurrencyEnum.SEK },
|
|
},
|
|
]
|
|
const additionalPrice = 33
|
|
const additionalPriceCurrency = CurrencyEnum.SEK
|
|
|
|
getAdditionalPrice(
|
|
total,
|
|
adults,
|
|
breakfast,
|
|
nights,
|
|
packages,
|
|
additionalPrice,
|
|
additionalPriceCurrency
|
|
)
|
|
|
|
expect(total.local.additionalPrice).toBe(308)
|
|
})
|
|
})
|
|
|
|
describe("getRedemptionPrice", () => {
|
|
it("returns price 0 and default currency when rooms are empty", () => {
|
|
const result = getRedemptionPrice([], 1)
|
|
|
|
expect(result).toEqual({
|
|
local: { price: 0, currency: CurrencyEnum.POINTS },
|
|
requested: undefined,
|
|
})
|
|
})
|
|
|
|
it("returns price 0 and set currency when rooms are empty", () => {
|
|
const result = getRedemptionPrice([], 1, CurrencyEnum.EUROBONUS)
|
|
|
|
expect(result).toEqual({
|
|
local: { price: 0, currency: CurrencyEnum.EUROBONUS },
|
|
requested: undefined,
|
|
})
|
|
})
|
|
|
|
it("returns price for single room with redemption price", () => {
|
|
const nights = 1
|
|
const result = getRedemptionPrice(
|
|
[
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
redemption: {
|
|
localPrice: {
|
|
pointsPerStay: 100,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPricePerStay: 0,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
nights
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
local: {
|
|
price: 100,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPrice: 0,
|
|
additionalPriceCurrency: CurrencyEnum.POINTS,
|
|
},
|
|
requested: undefined,
|
|
})
|
|
})
|
|
|
|
it("returns price for single room with multiple nights", () => {
|
|
const nights = 3
|
|
const result = getRedemptionPrice(
|
|
[
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
redemption: {
|
|
localPrice: {
|
|
pointsPerStay: 100,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPricePerStay: 0,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
nights
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
local: {
|
|
price: 100,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPrice: 0,
|
|
additionalPriceCurrency: CurrencyEnum.POINTS,
|
|
},
|
|
requested: undefined,
|
|
})
|
|
})
|
|
|
|
it("returns price for multiple rooms with multiple nights", () => {
|
|
const nights = 3
|
|
const result = getRedemptionPrice(
|
|
[
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
redemption: {
|
|
localPrice: {
|
|
pointsPerStay: 100,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPricePerStay: 0,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
redemption: {
|
|
localPrice: {
|
|
pointsPerStay: 150,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPricePerStay: 0,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
nights
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
local: {
|
|
price: 250,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPrice: 0,
|
|
additionalPriceCurrency: CurrencyEnum.POINTS,
|
|
},
|
|
requested: undefined,
|
|
})
|
|
})
|
|
|
|
it("does not return price for room without redemption", () => {
|
|
const nights = 3
|
|
const result = getRedemptionPrice(
|
|
[
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
public: {
|
|
price: 150,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
redemption: {
|
|
localPrice: {
|
|
pointsPerStay: 150,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPricePerStay: 0,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
nights
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
local: {
|
|
price: 150,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPrice: 0,
|
|
additionalPriceCurrency: CurrencyEnum.POINTS,
|
|
},
|
|
requested: undefined,
|
|
})
|
|
})
|
|
})
|
|
|
|
describe("getVoucherPrice", () => {
|
|
it("returns price 0 and default currency when rooms are empty", () => {
|
|
const result = getVoucherPrice([], 1)
|
|
|
|
expect(result).toEqual({
|
|
local: { price: 0, currency: CurrencyEnum.Voucher },
|
|
requested: undefined,
|
|
})
|
|
})
|
|
|
|
it("returns price for single room with voucher price", () => {
|
|
const nights = 1
|
|
const result = getVoucherPrice(
|
|
[
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
voucher: {
|
|
numberOfVouchers: 1,
|
|
},
|
|
},
|
|
},
|
|
],
|
|
nights
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
local: {
|
|
price: 1,
|
|
currency: CurrencyEnum.Voucher,
|
|
additionalPrice: 0,
|
|
},
|
|
requested: undefined,
|
|
})
|
|
})
|
|
|
|
it("returns price for single room with multiple nights", () => {
|
|
const nights = 3
|
|
const result = getVoucherPrice(
|
|
[
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
voucher: {
|
|
numberOfVouchers: 3,
|
|
},
|
|
},
|
|
},
|
|
],
|
|
nights
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
local: {
|
|
price: 3,
|
|
currency: CurrencyEnum.Voucher,
|
|
additionalPrice: 0,
|
|
},
|
|
requested: undefined,
|
|
})
|
|
})
|
|
|
|
it("returns price for multiple rooms with multiple nights", () => {
|
|
const nights = 3
|
|
const result = getVoucherPrice(
|
|
[
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
voucher: {
|
|
numberOfVouchers: 3,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
voucher: {
|
|
numberOfVouchers: 2,
|
|
},
|
|
},
|
|
},
|
|
],
|
|
nights
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
local: {
|
|
price: 5,
|
|
currency: CurrencyEnum.Voucher,
|
|
additionalPrice: 0,
|
|
},
|
|
requested: undefined,
|
|
})
|
|
})
|
|
|
|
it("does not return price for room without voucher", () => {
|
|
const nights = 3
|
|
const result = getVoucherPrice(
|
|
[
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
redemption: {
|
|
localPrice: {
|
|
pointsPerStay: 150,
|
|
currency: CurrencyEnum.POINTS,
|
|
additionalPricePerStay: 0,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
adults: 1,
|
|
breakfast: false,
|
|
roomFeatures: [],
|
|
roomRate: {
|
|
voucher: {
|
|
numberOfVouchers: 2,
|
|
},
|
|
},
|
|
},
|
|
],
|
|
nights
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
local: {
|
|
price: 2,
|
|
currency: CurrencyEnum.Voucher,
|
|
additionalPrice: 0,
|
|
},
|
|
requested: undefined,
|
|
})
|
|
})
|
|
})
|