LOY-493/Sidepeek upcoming stays * chore(LOY-493): Add icon to next stay card cta * chore(LOY-493): better folder org for stays * chore(LOY-494): more folder reorg * feat(LOY-493): Implement Sidepeek for Upcoming Stays Approved-by: Matilda Landström
243 lines
8.3 KiB
TypeScript
243 lines
8.3 KiB
TypeScript
import { describe, expect, it } from "vitest"
|
|
|
|
import { dt } from "@scandic-hotels/common/dt"
|
|
|
|
import { getTimeAgoText } from "./getTimeAgoText"
|
|
|
|
import type { IntlShape, MessageDescriptor } from "react-intl"
|
|
|
|
const mockIntl = {
|
|
formatMessage: (
|
|
descriptor: MessageDescriptor,
|
|
values?: Record<string, string | number | boolean | Date>
|
|
) => {
|
|
const messages: Record<string, string> = {
|
|
"common.nrDaysAgo": `{count, plural, one {# day ago} other {# days ago}}`,
|
|
"common.nrMonthsAgo": `{count, plural, one {# month ago} other {# months ago}}`,
|
|
"common.nrYearsAgo": `{count, plural, one {# year ago} other {# years ago}}`,
|
|
}
|
|
|
|
let message: string =
|
|
messages[descriptor.id as string] ||
|
|
(typeof descriptor.defaultMessage === "string"
|
|
? descriptor.defaultMessage
|
|
: "") ||
|
|
""
|
|
|
|
if (values) {
|
|
// Handle plural forms first
|
|
if (message.includes("{count, plural")) {
|
|
const count = values.count as number
|
|
if (count === 1) {
|
|
// Extract the singular form (between "one {" and "}")
|
|
const singularMatch = message.match(/one {(.*?)}/)
|
|
if (singularMatch) {
|
|
message = singularMatch[1].replace("#", String(count))
|
|
}
|
|
} else {
|
|
// Extract the plural form (between "other {" and "}")
|
|
const pluralMatch = message.match(/other {(.*?)}/)
|
|
if (pluralMatch) {
|
|
message = pluralMatch[1].replace("#", String(count))
|
|
}
|
|
}
|
|
}
|
|
|
|
// Replace any remaining placeholders
|
|
Object.entries(values).forEach(([key, value]) => {
|
|
message = message.replace(`{${key}}`, String(value))
|
|
})
|
|
}
|
|
|
|
return message
|
|
},
|
|
} as IntlShape
|
|
|
|
describe("getTimeAgoText", () => {
|
|
describe("days ago (1-30 days)", () => {
|
|
it("should return '1 day ago' for yesterday", () => {
|
|
const yesterday = dt().subtract(1, "day").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(yesterday, mockIntl)
|
|
|
|
expect(result).toBe("1 day ago")
|
|
})
|
|
|
|
it("should return '2 days ago' for 2 days ago", () => {
|
|
const twoDaysAgo = dt().subtract(2, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(twoDaysAgo, mockIntl)
|
|
|
|
expect(result).toBe("2 days ago")
|
|
})
|
|
|
|
it("should return '15 days ago' for 15 days ago", () => {
|
|
const fifteenDaysAgo = dt().subtract(15, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(fifteenDaysAgo, mockIntl)
|
|
|
|
expect(result).toBe("15 days ago")
|
|
})
|
|
|
|
it("should return '30 days ago' for exactly 30 days ago (boundary)", () => {
|
|
const thirtyDaysAgo = dt().subtract(30, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(thirtyDaysAgo, mockIntl)
|
|
|
|
expect(result).toBe("30 days ago")
|
|
})
|
|
|
|
it("should handle the full range from 1 to 30 days ago", () => {
|
|
for (let days = 1; days <= 30; days++) {
|
|
const pastDate = dt().subtract(days, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(pastDate, mockIntl)
|
|
|
|
if (days === 1) {
|
|
expect(result).toBe("1 day ago")
|
|
} else {
|
|
expect(result).toBe(`${days} days ago`)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
describe("months ago (31-364 days)", () => {
|
|
it("should return '1 month ago' for 31 days ago", () => {
|
|
const thirtyOneDaysAgo = dt().subtract(31, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(thirtyOneDaysAgo, mockIntl)
|
|
|
|
expect(result).toBe("1 month ago")
|
|
})
|
|
|
|
it("should return '1 month ago' for 45 days ago", () => {
|
|
const fortyFiveDaysAgo = dt().subtract(45, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(fortyFiveDaysAgo, mockIntl)
|
|
|
|
expect(result).toBe("1 month ago")
|
|
})
|
|
|
|
it("should return '2 months ago' for 60 days ago", () => {
|
|
const sixtyDaysAgo = dt().subtract(60, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(sixtyDaysAgo, mockIntl)
|
|
|
|
expect(result).toBe("2 months ago")
|
|
})
|
|
|
|
it("should return '6 months ago' for 180 days ago", () => {
|
|
const sixMonthsAgo = dt().subtract(180, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(sixMonthsAgo, mockIntl)
|
|
|
|
expect(result).toBe("6 months ago")
|
|
})
|
|
|
|
it("should return '11 months ago' for 330 days ago", () => {
|
|
const elevenMonthsAgo = dt().subtract(330, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(elevenMonthsAgo, mockIntl)
|
|
|
|
expect(result).toBe("11 months ago")
|
|
})
|
|
|
|
it("should return '12 months ago' for 364 days ago (boundary)", () => {
|
|
const twelveMonthsAgo = dt().subtract(364, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(twelveMonthsAgo, mockIntl)
|
|
|
|
expect(result).toBe("12 months ago")
|
|
})
|
|
})
|
|
|
|
describe("years ago (365+ days)", () => {
|
|
it("should return '1 year ago' for exactly 365 days ago", () => {
|
|
const oneYearAgo = dt().subtract(365, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(oneYearAgo, mockIntl)
|
|
|
|
expect(result).toBe("1 year ago")
|
|
})
|
|
|
|
it("should return '1 year ago' for 400 days ago", () => {
|
|
const fourHundredDaysAgo = dt().subtract(400, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(fourHundredDaysAgo, mockIntl)
|
|
|
|
expect(result).toBe("1 year ago")
|
|
})
|
|
|
|
it("should return '2 years ago' for 730 days ago", () => {
|
|
const twoYearsAgo = dt().subtract(730, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(twoYearsAgo, mockIntl)
|
|
|
|
expect(result).toBe("2 years ago")
|
|
})
|
|
|
|
it("should return '5 years ago' for 5 years ago", () => {
|
|
const fiveYearsAgo = dt().subtract(5, "years").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(fiveYearsAgo, mockIntl)
|
|
|
|
expect(result).toBe("5 years ago")
|
|
})
|
|
})
|
|
|
|
describe("edge cases", () => {
|
|
it("should handle dates with different time components consistently", () => {
|
|
// The function calculates exact day differences, so time components matter
|
|
// Using start of day to ensure consistent results
|
|
const dateWithTime1 = dt()
|
|
.subtract(5, "days")
|
|
.startOf("day")
|
|
.format("YYYY-MM-DD HH:mm")
|
|
const dateWithTime2 = dt()
|
|
.subtract(5, "days")
|
|
.startOf("day")
|
|
.format("YYYY-MM-DD HH:mm")
|
|
|
|
const result1 = getTimeAgoText(dateWithTime1, mockIntl)
|
|
const result2 = getTimeAgoText(dateWithTime2, mockIntl)
|
|
|
|
expect(result1).toBe("5 days ago")
|
|
expect(result2).toBe("5 days ago")
|
|
})
|
|
|
|
it("should handle ISO date strings with timezone", () => {
|
|
const isoDate = dt().subtract(7, "days").toISOString()
|
|
const result = getTimeAgoText(isoDate, mockIntl)
|
|
|
|
expect(result).toBe("7 days ago")
|
|
})
|
|
|
|
it("should return empty string for future dates", () => {
|
|
// Add 2 days to ensure it's definitely in the future regardless of time of day
|
|
const futureDate = dt().add(2, "days").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(futureDate, mockIntl)
|
|
|
|
expect(result).toBe("")
|
|
})
|
|
|
|
it("should return empty string for dates far in the future", () => {
|
|
const farFutureDate = dt().add(1, "year").format("YYYY-MM-DD")
|
|
const result = getTimeAgoText(farFutureDate, mockIntl)
|
|
|
|
expect(result).toBe("")
|
|
})
|
|
})
|
|
|
|
describe("boundary transitions", () => {
|
|
it("should transition correctly from days to months at 31 days", () => {
|
|
const date30 = dt().subtract(30, "days").format("YYYY-MM-DD")
|
|
const date31 = dt().subtract(31, "days").format("YYYY-MM-DD")
|
|
|
|
expect(getTimeAgoText(date30, mockIntl)).toBe("30 days ago")
|
|
expect(getTimeAgoText(date31, mockIntl)).toBe("1 month ago")
|
|
})
|
|
|
|
it("should transition correctly from months to years at 365 days", () => {
|
|
const date364 = dt().subtract(364, "days").format("YYYY-MM-DD")
|
|
const date365 = dt().subtract(365, "days").format("YYYY-MM-DD")
|
|
|
|
expect(getTimeAgoText(date364, mockIntl)).toBe("12 months ago")
|
|
expect(getTimeAgoText(date365, mockIntl)).toBe("1 year ago")
|
|
})
|
|
|
|
it("should handle the transition from 1 day to multiple days", () => {
|
|
const date1 = dt().subtract(1, "day").format("YYYY-MM-DD")
|
|
const date2 = dt().subtract(2, "days").format("YYYY-MM-DD")
|
|
|
|
expect(getTimeAgoText(date1, mockIntl)).toBe("1 day ago")
|
|
expect(getTimeAgoText(date2, mockIntl)).toBe("2 days ago")
|
|
})
|
|
})
|
|
})
|