-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelpers.test.ts
More file actions
94 lines (82 loc) · 4.05 KB
/
Helpers.test.ts
File metadata and controls
94 lines (82 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { formatDistanceStrict, sub } from "date-fns"
import { describe, it, expect } from "vitest"
import type { SharedConfigDomain } from "../types"
import { formatDistanceDate, getFormatedFee } from "./Helpers"
describe("Helpers", () => {
describe("getFormatedFee", () => {
it("parses ERC20 fee and returns fee with native token", () => {
const formatedFee = getFormatedFee(
{
amount: "1000000000000000",
tokenAddress: "0x0000000000000000000000000000000000000000",
tokenSymbol: "eth",
},
{
nativeTokenDecimals: 18,
nativeTokenSymbol: "eth",
} as SharedConfigDomain,
)
expect(formatedFee).toEqual("0.001 ETH")
})
it("parses PHA transfer fee and return fee with native token", () => {
const formatedFee = getFormatedFee(
{
amount: "100000000000",
tokenAddress: '{"Concrete":{"parents":"0","interior":"Here"}}',
tokenSymbol: "PHA",
},
{
nativeTokenDecimals: 12,
nativeTokenSymbol: "PHA",
} as SharedConfigDomain,
)
expect(formatedFee).toEqual("0.1 PHA")
})
})
describe("formatDistanceDate", () => {
const wholeYear = sub(new Date(), { years: 1, days: 1 })
const yearAndSomeDays = sub(new Date(), { years: 1, days: 14, hours: 7 })
const yearAndSomeMonths = sub(new Date(), { years: 1, months: 2, hours: 10 })
it.only("should return formated date days + hours", () => {
const formattedDate = formatDistanceDate(wholeYear.toISOString())
const distanceInDays = formatDistanceStrict(new Date(), wholeYear, { unit: "day" })
const formattedDate2 = formatDistanceDate(yearAndSomeDays.toISOString())
const distanceInDays2 = formatDistanceStrict(new Date(), yearAndSomeDays, { unit: "day" })
const formattedDate3 = formatDistanceDate(yearAndSomeMonths.toISOString())
const distanceInDays3 = formatDistanceStrict(new Date(), yearAndSomeMonths, { unit: "day" })
expect(formattedDate.includes(distanceInDays)).toBe(true)
expect(formattedDate.includes("hours ago")).toBe(false)
expect(formattedDate2.includes(distanceInDays2)).toBe(true)
expect(formattedDate2.includes("hours ago")).toBe(true)
expect(formattedDate3.includes(distanceInDays3)).toBe(true)
expect(formattedDate3.includes("hours ago")).toBe(true)
})
it("should return just days if date is between one month", () => {
const monthAgo = sub(new Date(), { months: 1 })
const formattedDate = formatDistanceDate(monthAgo.toISOString())
const distanceInDays = formatDistanceStrict(new Date(), monthAgo, { unit: "day" })
expect(formattedDate.includes(distanceInDays)).toBe(true)
})
it("should return days + hours if date is between one month and one year", () => {
const twoMonthsAgo = sub(new Date(), { months: 2, hours: 6 })
const formattedDate = formatDistanceDate(twoMonthsAgo.toISOString())
const distanceInDays = formatDistanceStrict(new Date(), twoMonthsAgo, { unit: "day" })
expect(formattedDate.includes(distanceInDays)).toBe(true)
expect(formattedDate.includes("hours ago")).toBe(true)
})
it("should return hours + minutes if date is less than a day", () => {
const twoHoursAgo = sub(new Date(), { hours: 2, minutes: 6 })
const formattedDate = formatDistanceDate(twoHoursAgo.toISOString())
const distanceInHours = formatDistanceStrict(new Date(), twoHoursAgo, { unit: "hour" })
expect(formattedDate.includes(distanceInHours)).toBe(true)
expect(formattedDate.includes("minutes ago")).toBe(true)
})
it("should return minutes ago if date is less than an hour", () => {
const twoMinutesAgo = sub(new Date(), { minutes: 2, seconds: 6 })
const formattedDate = formatDistanceDate(twoMinutesAgo.toISOString())
const distanceInMinutes = formatDistanceStrict(new Date(), twoMinutesAgo, { unit: "minute" })
expect(formattedDate.includes(distanceInMinutes)).toBe(true)
expect(formattedDate.includes("minutes ago")).toBe(true)
})
})
})