Add tests for durationFromNow
This commit is contained in:
parent
2462b80991
commit
ab55788df4
|
@ -1,11 +1,15 @@
|
||||||
|
import tk from "timekeeper"
|
||||||
import * as date from "../../src/helpers/date"
|
import * as date from "../../src/helpers/date"
|
||||||
|
|
||||||
|
const frozenDate = new Date("2025-03-06T11:38:41.000Z")
|
||||||
|
tk.freeze(frozenDate)
|
||||||
|
|
||||||
describe("date helper", () => {
|
describe("date helper", () => {
|
||||||
describe("difference", () => {
|
describe("difference", () => {
|
||||||
it("should return the difference between two dates", () => {
|
it("should return the difference between two dates", () => {
|
||||||
const result = date.difference(
|
const result = date.difference(
|
||||||
"2021-01-02T12:34:56.789",
|
"2021-01-02T12:34:56.789Z",
|
||||||
"2021-01-01T01:00:00"
|
"2021-01-01T01:00:00.000Z"
|
||||||
)
|
)
|
||||||
const expected =
|
const expected =
|
||||||
1 * 24 * 60 * 60 * 1000 + // 1 day
|
1 * 24 * 60 * 60 * 1000 + // 1 day
|
||||||
|
@ -25,4 +29,26 @@ describe("date helper", () => {
|
||||||
expect(result).toEqual(1)
|
expect(result).toEqual(1)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("durationFromNow", () => {
|
||||||
|
it("should return the difference between two close dates", () => {
|
||||||
|
const result = date.durationFromNow("2025-03-06T11:38:43.000Z")
|
||||||
|
expect(result).toEqual("a few seconds")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return the difference between two days hours apart", () => {
|
||||||
|
const result = date.durationFromNow("2025-03-06T01:00:00.000Z")
|
||||||
|
expect(result).toEqual("11 hours")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("accepts days in the past", () => {
|
||||||
|
const result = date.durationFromNow("2025-03-01")
|
||||||
|
expect(result).toEqual("5 days")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("accepts days in the future", () => {
|
||||||
|
const result = date.durationFromNow("2025-03-08")
|
||||||
|
expect(result).toEqual("2 days")
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue