Files
web/apps/scandic-web/stores/my-stay/helpers.test.ts
Joakim Jäderberg 488a396cfa Merged in feature/SW-3616-partner-points-my-stay (pull request #3407)
Feature/SW-3616 partner points my stay

* feat(SW-3616): display partner points in my stays

* null check roomPointType

* Lowercase POINTS in my stay

* include other than Scandic points when displaying price details modal


Approved-by: Anton Gunnarsson
2026-01-12 09:24:04 +00:00

181 lines
4.2 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import { beforeAll, describe, expect, it, vi } from "vitest"
import { calculateTotalPrice } from "./helpers"
describe("calculateTotalPrice", () => {
const baseRoom: Parameters<typeof calculateTotalPrice>[0][0] = {
totalPrice: 0,
isCancelled: false,
cheques: 0,
roomPoints: 0,
roomPointType: null,
totalPoints: 0,
vouchers: 0,
}
const mockIntlSimple = {
formatMessage: vi.fn(({}, values) => {
if (values?.numberOfVouchers === 1) return "Voucher"
return "Vouchers"
}),
formatNumber: vi.fn((num) => String(num)),
} as any
vi.mock("@scandic-hotels/common/utils/numberFormatting", () => ({
formatPrice: (_intl: any, price: number, currency: string) =>
`${price} ${currency}`,
}))
beforeAll(() => {
vi.clearAllMocks()
})
it("should return correct price for conflicting rooms (cash)", () => {
const rooms = [
{ totalPrice: 1000, isCancelled: false },
{ totalPrice: 500, isCancelled: false },
] as any
const result = calculateTotalPrice(
rooms,
"SEK" as any,
mockIntlSimple,
false
)
expect(result).toBe("1500 SEK")
})
it("should ignore cancelled rooms if not all are cancelled", () => {
vi.mock("@scandic-hotels/common/utils/numberFormatting", () => ({
formatPrice: (_intl: any, price: number, currency: string) =>
`${price} ${currency}`,
}))
const rooms = [
{ totalPrice: 1000, isCancelled: false },
{ totalPrice: 500, isCancelled: true },
] as any
const result = calculateTotalPrice(
rooms,
"SEK" as any,
mockIntlSimple,
false
)
expect(result).toBe("1000 SEK")
})
it("should format string for Vouchers", () => {
const result = calculateTotalPrice(
[{ ...baseRoom, vouchers: 2, totalPrice: -1, isCancelled: false }],
"SEK" as any,
mockIntlSimple,
false
)
expect(result).toContain("2 Vouchers")
})
it("should handle mixed Cash and Points", () => {
const result = calculateTotalPrice(
[
{
...baseRoom,
totalPrice: 100,
isCancelled: false,
totalPoints: 0,
roomPoints: 0,
},
{
...baseRoom,
totalPrice: 0,
totalPoints: 20000,
roomPoints: 20000,
roomPointType: "Scandic",
isCancelled: false,
},
],
"SEK" as any,
mockIntlSimple,
false
)
expect(result).toMatch(/20000 Points \+ 100 SEK/)
})
it("should sum up Cheques correctly", () => {
const rooms = [{ cheques: 2, isCancelled: false }] as any
// CurrencyEnum.CC is usually imported, assuming it resolves or we check specific output
const result = calculateTotalPrice(
rooms,
"SEK" as any,
mockIntlSimple,
false
)
expect(result).toContain("2 CC")
})
it("should combine Vouchers and Cash", () => {
const result = calculateTotalPrice(
[
{
...baseRoom,
vouchers: 1,
totalPrice: -1,
isCancelled: false,
},
{
...baseRoom,
totalPrice: 500,
isCancelled: false,
},
],
"SEK" as any,
mockIntlSimple,
false
)
expect(result).toMatch(/1 Voucher \+ 500 SEK/)
})
it("should combine Eurobonus points and Cash", () => {
const result = calculateTotalPrice(
[
{
...baseRoom,
roomPoints: 1,
roomPointType: "EuroBonus",
},
{
...baseRoom,
totalPrice: 500,
},
],
"SEK" as any,
mockIntlSimple,
false
)
expect(result).toMatch(/1 EuroBonus \+ 500 SEK/)
})
it("should combine Eurobonus points, Scandic Friends points and Cash", () => {
const result = calculateTotalPrice(
[
{
...baseRoom,
roomPoints: 1,
roomPointType: "EuroBonus",
totalPoints: 500,
},
{
...baseRoom,
totalPrice: 500,
},
],
"SEK" as any,
mockIntlSimple,
false
)
expect(result).toMatch(/500 Points \+ 1 EuroBonus \+ 500 SEK/)
})
})