fix(SW-3442) getLowestRoomPrice - cannot read property of undefined
* fix: getLowestRoomPrice throws when given unexpected data * dont track lowestRoomPrice if unavailable Approved-by: Hrishikesh Vaipurkar
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
|
||||
|
||||
import { getLowestRoomPrice } from "./getLowestRoomPrice"
|
||||
|
||||
import type { AvailabilityWithRoomInfo } from "../types"
|
||||
|
||||
describe("getLowestRoomPrice", () => {
|
||||
it("returns null when roomAvailability is empty", () => {
|
||||
const res = getLowestRoomPrice([])
|
||||
expect(res).toBeNull()
|
||||
})
|
||||
|
||||
it("returns null when first row has no rooms (only nulls)", () => {
|
||||
const roomAvailability = [[null, null], [{ products: [] }]]
|
||||
const res = getLowestRoomPrice(
|
||||
roomAvailability as (AvailabilityWithRoomInfo | null)[][]
|
||||
)
|
||||
expect(res).toBeNull()
|
||||
})
|
||||
|
||||
it("returns undefined when first room has no price products", () => {
|
||||
const mixedProduct = {
|
||||
public: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 50 },
|
||||
},
|
||||
}
|
||||
const firstRoom = { products: [mixedProduct] }
|
||||
const roomAvailability = [
|
||||
[
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
{ brokenData: 1 } as any,
|
||||
firstRoom,
|
||||
],
|
||||
]
|
||||
const res = getLowestRoomPrice(roomAvailability)
|
||||
expect(res).toEqual({ currency: CurrencyEnum.SEK, price: 50 })
|
||||
})
|
||||
|
||||
it("returns the first price product converted to { currency, price }", () => {
|
||||
const publicProduct = {
|
||||
public: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 1000 },
|
||||
},
|
||||
}
|
||||
const memberProduct = {
|
||||
member: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 900 },
|
||||
},
|
||||
}
|
||||
const firstRoom = { products: [publicProduct, memberProduct] }
|
||||
const roomAvailability = [[firstRoom], [{ products: [] }]]
|
||||
const res = getLowestRoomPrice(
|
||||
roomAvailability as unknown as (AvailabilityWithRoomInfo | null)[][]
|
||||
)
|
||||
expect(res).toEqual({ currency: CurrencyEnum.SEK, price: 900 })
|
||||
})
|
||||
|
||||
it("prefers member/public values from the product object when present", () => {
|
||||
// product with both member and public: member values should be used for currency/price resolution
|
||||
const mixedProduct = {
|
||||
public: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 1000 },
|
||||
},
|
||||
member: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 800 },
|
||||
},
|
||||
}
|
||||
const firstRoom = { products: [mixedProduct] }
|
||||
const roomAvailability = [[null, firstRoom]]
|
||||
const res = getLowestRoomPrice(
|
||||
roomAvailability as (AvailabilityWithRoomInfo | null)[][]
|
||||
)
|
||||
expect(res).toEqual({ currency: CurrencyEnum.SEK, price: 800 })
|
||||
})
|
||||
|
||||
it("prefers member/public values from the product object when present", () => {
|
||||
// product with both member and public: member values should be used for currency/price resolution
|
||||
const mixedProduct = {
|
||||
public: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 1200 },
|
||||
},
|
||||
member: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 800 },
|
||||
},
|
||||
}
|
||||
const firstRate = { products: [mixedProduct] }
|
||||
const firstRoom = [null, firstRate]
|
||||
const res = getLowestRoomPrice([
|
||||
firstRoom,
|
||||
] as (AvailabilityWithRoomInfo | null)[][])
|
||||
expect(res).toEqual({ currency: CurrencyEnum.SEK, price: 800 })
|
||||
})
|
||||
|
||||
it("gets the lowest price even if the order is mixed", () => {
|
||||
const firstRate = {
|
||||
products: [
|
||||
{
|
||||
public: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 1200 },
|
||||
},
|
||||
member: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 800 },
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
const secondRate = {
|
||||
products: [
|
||||
{
|
||||
public: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 1000 },
|
||||
},
|
||||
member: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 700 },
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
const firstRoom = [firstRate, secondRate]
|
||||
const secondRoom = [null, firstRate, secondRate]
|
||||
const roomAvailability = [
|
||||
null,
|
||||
firstRoom,
|
||||
secondRoom,
|
||||
] as (AvailabilityWithRoomInfo | null)[][]
|
||||
const res = getLowestRoomPrice(roomAvailability)
|
||||
expect(res).toEqual({ currency: CurrencyEnum.SEK, price: 700 })
|
||||
})
|
||||
|
||||
it("gets the lowest price even if the public price is lower than member price", () => {
|
||||
const firstRate = {
|
||||
products: [
|
||||
{
|
||||
public: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 700 },
|
||||
},
|
||||
member: {
|
||||
localPrice: { currency: CurrencyEnum.SEK, pricePerNight: 800 },
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const firstRoom = [firstRate]
|
||||
const roomAvailability = [
|
||||
firstRoom,
|
||||
] as (AvailabilityWithRoomInfo | null)[][]
|
||||
const res = getLowestRoomPrice(roomAvailability)
|
||||
expect(res).toEqual({ currency: CurrencyEnum.SEK, price: 700 })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user