Files
web/packages/booking-flow/lib/components/EnterDetails/Payment/helpers.test.ts
Anton Gunnarsson 7faa9933a2 Merged in feat/sw-3642-inject-sas-eb-payment (pull request #3243)
feat(SW-3642): Enable SAS EB payments

* Wip add SAS eb payment

* Add validate payment call

* Check booking status payment method to determine validation

* Clean up getPaymentData

* Fix PartnerPoints casing

* Add comment for validatePartnerPayment error handling

* Remove comment


Approved-by: Joakim Jäderberg
2025-12-11 13:23:12 +00:00

282 lines
7.4 KiB
TypeScript

import { describe, expect, it } from "vitest"
import { Lang } from "@scandic-hotels/common/constants/language"
import { PaymentMethodEnum } from "@scandic-hotels/common/constants/paymentMethod"
import {
getPaymentData,
getPaymentMethod,
mustGuaranteeBooking,
} from "./helpers"
const buildRoom = (
overrides: Partial<{
memberMustBeGuaranteed: boolean
mustBeGuaranteed: boolean
guest: { join: boolean; membershipNo?: string }
}> = {}
) => ({
room: {
memberMustBeGuaranteed: false,
mustBeGuaranteed: false,
guest: { join: false, membershipNo: undefined },
...overrides,
},
})
describe("mustGuaranteeBooking", () => {
it("returns true when the first room requires a member guarantee for a logged-in user", () => {
const rooms = [
buildRoom({
memberMustBeGuaranteed: true,
mustBeGuaranteed: false,
}),
]
const booking = { rooms: [{}] }
expect(
mustGuaranteeBooking({
isUserLoggedIn: true,
booking,
rooms,
})
).toBe(true)
})
it("returns memberMustBeGuaranteed when guest has membership details and counter rate code", () => {
const rooms = [
buildRoom(),
buildRoom({
memberMustBeGuaranteed: true,
guest: { join: true },
}),
]
const booking = { rooms: [{}, { counterRateCode: "COUNTER" }] }
expect(
mustGuaranteeBooking({
isUserLoggedIn: false,
booking,
rooms,
})
).toBe(true)
})
it("returns false when member condition is not met despite counter rate code", () => {
const rooms = [
buildRoom(),
buildRoom({
memberMustBeGuaranteed: false,
guest: { join: true },
}),
]
const booking = { rooms: [{}, { counterRateCode: "COUNTER" }] }
expect(
mustGuaranteeBooking({
isUserLoggedIn: false,
booking,
rooms,
})
).toBe(false)
})
it("falls back to mustBeGuaranteed when no member-specific rules apply", () => {
const rooms = [
buildRoom({
memberMustBeGuaranteed: false,
mustBeGuaranteed: true,
}),
]
const booking = { rooms: [{}] }
expect(
mustGuaranteeBooking({
isUserLoggedIn: false,
booking,
rooms,
})
).toBe(true)
})
})
describe("getPaymentData", () => {
it("returns correct URLs and method when guarantee is true", () => {
const result = getPaymentData({
paymentMethod: PaymentMethodEnum.swish,
lang: Lang.en,
guarantee: true,
bookingMustBeGuaranteed: false,
hasOnlyFlexRates: true,
isRedemptionBooking: false,
})
expect(result).toEqual({
paymentMethod: PaymentMethodEnum.swish,
success: `/en/hotelreservation/payment-callback/success`,
error: `/en/hotelreservation/payment-callback/error`,
cancel: `/en/hotelreservation/payment-callback/cancel`,
})
})
it("returns correct URLs and method when bookingMustBeGuaranteed", () => {
const result = getPaymentData({
paymentMethod: PaymentMethodEnum.swish,
lang: Lang.en,
guarantee: false,
bookingMustBeGuaranteed: true,
hasOnlyFlexRates: true,
isRedemptionBooking: false,
})
expect(result).toEqual({
paymentMethod: PaymentMethodEnum.swish,
success: `/en/hotelreservation/payment-callback/success`,
error: `/en/hotelreservation/payment-callback/error`,
cancel: `/en/hotelreservation/payment-callback/cancel`,
})
})
it("returns correct URLs and method when has only flex rates is false", () => {
const result = getPaymentData({
paymentMethod: PaymentMethodEnum.swish,
lang: Lang.en,
guarantee: false,
bookingMustBeGuaranteed: false,
hasOnlyFlexRates: false,
isRedemptionBooking: false,
})
expect(result).toEqual({
paymentMethod: PaymentMethodEnum.swish,
success: `/en/hotelreservation/payment-callback/success`,
error: `/en/hotelreservation/payment-callback/error`,
cancel: `/en/hotelreservation/payment-callback/cancel`,
})
})
it("returns null when payment isn't required", () => {
const result = getPaymentData({
paymentMethod: PaymentMethodEnum.swish,
lang: Lang.en,
guarantee: false,
bookingMustBeGuaranteed: false,
hasOnlyFlexRates: true,
isRedemptionBooking: false,
})
expect(result).toBeNull()
})
it("returns saved credit card when provided", () => {
const result = getPaymentData({
paymentMethod: PaymentMethodEnum.card,
lang: Lang.en,
guarantee: false,
bookingMustBeGuaranteed: false,
hasOnlyFlexRates: false,
isRedemptionBooking: false,
savedCreditCard: {
alias: "My Visa",
expirationDate: "12/25",
cardType: "visa",
id: "",
type: "",
truncatedNumber: "",
},
})
expect(result).toEqual({
paymentMethod: PaymentMethodEnum.card,
success: `/en/hotelreservation/payment-callback/success`,
error: `/en/hotelreservation/payment-callback/error`,
cancel: `/en/hotelreservation/payment-callback/cancel`,
card: {
alias: "My Visa",
expiryDate: "12/25",
cardType: "visa",
},
})
})
it("returns correct URLs and method when isRedemptionBooking is true and type is PartnerPoints", () => {
const result = getPaymentData({
paymentMethod: PaymentMethodEnum.PartnerPoints,
lang: Lang.en,
guarantee: false,
bookingMustBeGuaranteed: false,
hasOnlyFlexRates: true,
isRedemptionBooking: true,
})
expect(result).toEqual({
paymentMethod: PaymentMethodEnum.PartnerPoints,
success: `/en/hotelreservation/payment-callback/success`,
error: `/en/hotelreservation/payment-callback/error`,
cancel: `/en/hotelreservation/payment-callback/cancel`,
})
})
})
describe("getPaymentMethod", () => {
it("returns card when hasFlexRates is true", () => {
const hasFlexRates = true
const isRedemptionBooking = false
const redemptionType = "scandic"
const method = getPaymentMethod({
paymentMethod: PaymentMethodEnum.swish,
hasFlexRates,
isRedemptionBooking,
redemptionType,
})
expect(method).toBe(PaymentMethodEnum.card)
})
it("returns PartnerPoints when is redemption and redemptionType is partner", () => {
const hasFlexRates = false
const isRedemptionBooking = true
const redemptionType = "partner"
const method = getPaymentMethod({
paymentMethod: PaymentMethodEnum.swish,
hasFlexRates,
isRedemptionBooking,
redemptionType,
})
expect(method).toBe(PaymentMethodEnum.PartnerPoints)
})
it("returns paymentMethod when not redemption but redemptionType is partner", () => {
const hasFlexRates = false
const isRedemptionBooking = false
const redemptionType = "partner"
const method = getPaymentMethod({
paymentMethod: PaymentMethodEnum.swish,
hasFlexRates,
isRedemptionBooking,
redemptionType,
})
expect(method).toBe(PaymentMethodEnum.swish)
})
it("returns card when payment method is string", () => {
const hasFlexRates = false
const isRedemptionBooking = false
const redemptionType = "scandic"
const method = getPaymentMethod({
paymentMethod: "something-else",
hasFlexRates,
isRedemptionBooking,
redemptionType,
})
expect(method).toBe(PaymentMethodEnum.card)
})
})