Files
web/apps/scandic-web/stores/my-stay/helpers.test.ts
Anton Gunnarsson 16fbdb7ae0 Merged in fix/refactor-currency-display (pull request #3434)
fix(SW-3616): Handle EuroBonus point type everywhere

* Add tests to formatPrice

* formatPrice

* More work replacing config with api points type

* More work replacing config with api points type

* More fixing with currency

* maybe actually fixed it

* Fix MyStay

* Clean up

* Fix comments

* Merge branch 'master' into fix/refactor-currency-display

* Fix calculateTotalPrice for EB points + SF points + cash


Approved-by: Joakim Jäderberg
2026-01-15 09:32:17 +00:00

177 lines
4.1 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import { createIntl, createIntlCache } from "react-intl"
import { beforeAll, describe, expect, it, vi } from "vitest"
import { calculateTotalPrice } from "./helpers"
const cache = createIntlCache()
const createTestIntl = (locale: string = "en-US") =>
createIntl({ locale, messages: {}, onError: () => {} }, cache)
describe("calculateTotalPrice", () => {
const baseRoom: Parameters<typeof calculateTotalPrice>[0][0] = {
totalPrice: 0,
isCancelled: false,
cheques: 0,
roomPointType: null,
totalPoints: 0,
roomPoints: 0,
vouchers: 0,
}
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,
createTestIntl(),
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,
createTestIntl(),
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,
createTestIntl(),
false
)
expect(result).toContain("2 Vouchers")
})
it("should handle mixed Cash and Points", () => {
const result = calculateTotalPrice(
[
{
...baseRoom,
totalPrice: 100,
isCancelled: false,
totalPoints: 0,
},
{
...baseRoom,
totalPrice: 0,
totalPoints: 20000,
roomPointType: "Scandic",
isCancelled: false,
},
],
"SEK" as any,
createTestIntl(),
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,
createTestIntl(),
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,
createTestIntl(),
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,
createTestIntl(),
false
)
expect(result).toMatch(/1 EB Point \+ 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,
createTestIntl(),
false
)
expect(result).toMatch(/1 EB Point \+ 500 Points \+ 500 SEK/)
})
})